Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.vscode
.idea
build
build/***
core-*

18 changes: 18 additions & 0 deletions include/mgos_pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

#include "mgos_event.h"

#ifndef CS_MOS_LIBS_PWM_SRC_MGOS_PWM_H_
#define CS_MOS_LIBS_PWM_SRC_MGOS_PWM_H_

Expand All @@ -24,6 +26,22 @@
extern "C" {
#endif /* __cplusplus */

#define MGOS_PWM_BASE MGOS_EVENT_BASE('P', 'W', 'M')

enum mgos_pwm_event
{
MGOS_PWM_CHANNEL = MGOS_PWM_BASE,
MGOS_PWM_OTHER

};

/* ev_data for MGOS_PWM_CHANNEL event. */
struct mgos_pwm_channel_data
{
int pin;
int channel;
};

/*
*
* Set and control the PWM.
Expand Down
77 changes: 72 additions & 5 deletions include/mgos_pwm_rgb_led.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,71 @@
* PWM-controlled RGB LED.
* Example:
* struct mgos_pwm_rgb_led led;
* mgos_pwm_rgb_led_init(&led, 16, 17, 18);
* mgos_pwm_rgb_led_init(&led, 16, 17, 18, LED_FREQ, COMMON_CATHODE);
* mgos_pwm_rgb_led_set(&led, 255, 255, 255, 255); // White, max brightness
* mgos_pwm_rgb_led_set(&led, 255, 0, 0, 127); // Red, half brightness
*/

#ifndef MGOS_PWM_RGB_LED_h
#define MGOS_PWM_RGB_LED_h
#pragma once

#include "driver/ledc.h"
#include "mgos_freertos.h"
#include "mgos_timers.h"
#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

enum mgos_pwm_fade_direction
{
FADE_BLINK = 0,
FADE_BLINKRAPID,
FADE_OFF,
FADE_UP,
FADE_DOWN,
FADE_LOOP,


};
enum mgos_pwm_fade_colors
{
RED = 0,
GREEN,
BLUE,
ALL

};

struct mgos_pwm_rgb_led {
int freq;
int freq, chan;
uint8_t r, g, b, br; /* Current values or R, G, B and brightness. */
int gpio_r, gpio_g, gpio_b;
};
int gpio_r_chan, gpio_g_chan, gpio_b_chan; /* what ledc channel these become associated with */
float gpio_r_pct, gpio_g_pct, gpio_b_pct; /* _apply function calculated 0-1 brightness result */
bool common_cathode;
bool fade_installed;
enum mgos_pwm_fade_direction fade_direction;
int time_on, time_off; // off is only for blink
int fade_max, fade_min; /* time in ms, fade max/min in 0-255 */
TaskHandle_t xHandle;
mgos_timer_id led_timer_id;
}; // mgos_pwm_rgb_led_t, *p_mgos_pwm_rgb_led_t;

//struct mgos_pwm_rgb_led led;

#define MGOS_PWM_RGB_LED_DEFAULT_FREQ 400 /* Hz */

/* Init the LED pins. */
/*
* Init the LED pins.
* Common Cathode the diodes arrow symbol point to a shared GND.
* Common Anode the diodes arrow symbol point to a shared VIN.
*/
bool mgos_pwm_rgb_led_init(struct mgos_pwm_rgb_led *led, int gpio_r, int gpio_g,
int gpio_b);
int gpio_b, int freq, bool common_cathode);

/* Set color and brightenss, 1-255. */
void mgos_pwm_rgb_led_set(struct mgos_pwm_rgb_led *led, uint8_t r, uint8_t g,
Expand All @@ -62,9 +102,36 @@ void mgos_pwm_rgb_led_set_brightness(struct mgos_pwm_rgb_led *led, uint8_t br);
/* Set PWM frequency. */
bool mgos_pwm_rgb_led_set_freq(struct mgos_pwm_rgb_led *led, int freq);

/*
* Register a system event handler to maintain a mapping between pin and ledc channel
* Whereever you initialised the led object, add this:
* mgos_event_add_handler(MGOS_PWM_CHANNEL, mgos_pwm_rgb_channel_update_cb, &led);
*/
void mgos_pwm_rgb_channel_update_cb(int ev, void* ev_data, void* userdata);

/* Enable LEDC hardware fading
* resetToStartingPosition flags whether the led should be set to the logical brightness that the direction would start at
* ie 0 if fading up
*/
void mgos_pwm_rgb_fade_start(struct mgos_pwm_rgb_led* led, int ms, enum mgos_pwm_fade_direction direction,
bool resetToStartingPosition, uint8_t max, uint8_t min);

/* Enable LEDC hardware fading */
void mgos_pwm_rgb_fade_stop(struct mgos_pwm_rgb_led* led);

/* Private TaskCreate process to avoid interruptions */
void mgos_pwm_rgb_blink_task(void* ledArg);

/* client callable function to start blink */
void mgos_pwm_rgb_blink_start(struct mgos_pwm_rgb_led* led, int ms_on, int ms_off);

void mgos_pwm_rgb_blink_stop(struct mgos_pwm_rgb_led* led);

/* Disable PWM and release the pins. */
void mgos_pwm_rgb_led_deinit(struct mgos_pwm_rgb_led *led);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
13 changes: 12 additions & 1 deletion src/esp32/esp32_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "common/cs_dbg.h"
#include "driver/ledc.h"
#include "mgos_pwm.h"
#include "mgos_event.h"

#define LEDC_MODE LEDC_HIGH_SPEED_MODE
#define LEDC_DEPTH LEDC_TIMER_10_BIT
Expand All @@ -31,6 +32,8 @@ struct ledc_info {
int timer;
};



static struct ledc_info s_ledc_ch[LEDC_NUM_CHANS] = {{.pin = -1, .timer = -1},
{.pin = -1, .timer = -1},
{.pin = -1, .timer = -1},
Expand Down Expand Up @@ -86,13 +89,14 @@ static bool esp32_pwm_config_timer(int timer, int freq) {
return true;
}


static bool esp32_pwm_add(int pin, int timer, int freq, int duty) {
int ch;
esp_err_t rc;
ledc_channel_config_t ledc_channel = {
.gpio_num = pin,
.speed_mode = LEDC_MODE,
.intr_type = LEDC_INTR_DISABLE,
.intr_type = LEDC_INTR_FADE_END,
.duty = duty,
};

Expand Down Expand Up @@ -122,6 +126,8 @@ static bool esp32_pwm_add(int pin, int timer, int freq, int duty) {
LOG(LL_DEBUG, ("LEDC channel %d added: pin=%d, timer=%d, freq=%d, duty=%d",
(ch), (pin), (timer), (freq), (duty)));



return true;
}

Expand Down Expand Up @@ -193,5 +199,10 @@ bool mgos_pwm_set(int pin, int freq, float duty) {
}
}

// Announce it so on ESP32 systems we can track pin to LEDC Channel mappings for fade effects etc
struct mgos_pwm_channel_data data = { .channel = ch,
.pin = pin };
mgos_event_trigger(MGOS_PWM_CHANNEL, &data);

return ret;
}
4 changes: 2 additions & 2 deletions src/esp8266/esp_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ bool mgos_pwm_set(int pin, int freq, float duty) {
*/
if (freq > 0 && freq < 10) return false;

p = find_or_create_pwm_info(pin, (freq > 0) && (duty > 0));
p = find_or_create_pwm_info(pin, (freq > 0));
if (p == NULL) {
return false;
}

if (freq <= 0 || duty <= 0) {
if (freq <= 0) {
remove_pwm_info(p);
pwm_configure_timer();
mgos_gpio_write(pin, 0);
Expand Down
5 changes: 4 additions & 1 deletion src/mgos_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
* [mgos_pwm.c](https://github.com/mongoose-os-libs/pwm/blob/master/src/mgos_pwm.c)
*/

#include <stdbool.h>
#include "mgos_pwm.h"

#include <stdbool.h>

bool mgos_pwm_init(void) {
mgos_event_register_base(MGOS_PWM_BASE, "mgos_PWM");

return true;
}
Loading