1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include <stdio.h>
#include "rgbledc.h" #include "sdkconfig.h"
#define LED_RED_CH LEDC_CHANNEL_0 #define LED_GREEN_CH LEDC_CHANNEL_1 #define LED_BLUE_CH LEDC_CHANNEL_2
#define LED_RED_PIN CONFIG_LED_RED_PIN #define LED_GREEN_PIN CONFIG_LED_GREEN_PIN #define LED_BLUE_PIN CONFIG_LED_BLUE_PIN
void rgbLED_init() { ledc_timer_config_t ledc_timer = { .speed_mode = LEDC_LOW_SPEED_MODE, .timer_num = LEDC_TIMER_0, .duty_resolution = LEDC_TIMER_13_BIT, .freq_hz = 5000, .clk_cfg = LEDC_AUTO_CLK };
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
ledc_channel_config_t ledc_red_chan = { .speed_mode = LEDC_LOW_SPEED_MODE, .channel = LED_RED_CH, .timer_sel = LEDC_TIMER_0, .intr_type = LEDC_INTR_DISABLE, .gpio_num = LED_RED_PIN, .duty = 0, .hpoint = 0 }; ESP_ERROR_CHECK(ledc_channel_config(&ledc_red_chan));
ledc_channel_config_t ledc_green_chan = { .speed_mode = LEDC_LOW_SPEED_MODE, .channel = LED_GREEN_CH, .timer_sel = LEDC_TIMER_0, .intr_type = LEDC_INTR_DISABLE, .gpio_num = LED_GREEN_PIN, .duty = 0, .hpoint = 0 }; ESP_ERROR_CHECK(ledc_channel_config(&ledc_green_chan));
ledc_channel_config_t ledc_blue_chan = { .speed_mode = LEDC_LOW_SPEED_MODE, .channel = LED_BLUE_CH, .timer_sel = LEDC_TIMER_0, .intr_type = LEDC_INTR_DISABLE, .gpio_num = LED_BLUE_PIN, .duty = 0, .hpoint = 0 }; ESP_ERROR_CHECK(ledc_channel_config(&ledc_blue_chan));
ledc_fade_func_install(0); }
void rgbLED_on(uint8_t r, uint8_t g, uint8_t b) { uint32_t r_duty = r << 5; uint32_t g_duty = g << 5; uint32_t b_duty = b << 5;
ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_RED_CH,r_duty,0); ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_GREEN_CH,g_duty,0); ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_BLUE_CH,b_duty,0); }
void rgbLED_off() { ledc_stop(LEDC_LOW_SPEED_MODE,LED_RED_CH,0); ledc_stop(LEDC_LOW_SPEED_MODE,LED_GREEN_CH,0); ledc_stop(LEDC_LOW_SPEED_MODE,LED_BLUE_CH,0); }
void rgbLED_fade(uint8_t r, uint8_t g, uint8_t b, bool up, int time) { uint32_t red_duty = r << 5; uint32_t green_duty = g << 5; uint32_t blue_duty = b << 5;
if(!up){ ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_RED_CH,red_duty,0); ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_GREEN_CH,green_duty,0); ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_BLUE_CH,blue_duty,0);
red_duty = 0; green_duty = 0; blue_duty = 0; }else{ ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_RED_CH,0,0); ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_GREEN_CH,0,0); ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE,LED_BLUE_CH,0,0); }
printf("dudy:%d %d %d\r\n", red_duty,green_duty,blue_duty);
ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE,LED_RED_CH,red_duty,time,LEDC_FADE_WAIT_DONE); ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE,LED_GREEN_CH,green_duty,time,LEDC_FADE_WAIT_DONE); ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE,LED_BLUE_CH,blue_duty,time,LEDC_FADE_WAIT_DONE); }
|