From aedbb7ef943afd52c0ce3b5eb3d1d73cbb5ffecc Mon Sep 17 00:00:00 2001 From: rkcf Date: Wed, 8 Feb 2023 15:26:02 -0600 Subject: [PATCH] Feat: add in ledc control --- main/esp32_rgb_rotary_encoder.c | 61 +++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/main/esp32_rgb_rotary_encoder.c b/main/esp32_rgb_rotary_encoder.c index c94c3e0..b64cf9a 100644 --- a/main/esp32_rgb_rotary_encoder.c +++ b/main/esp32_rgb_rotary_encoder.c @@ -8,23 +8,76 @@ #include "freertos/queue.h" #include "esp_log.h" #include "driver/pulse_cnt.h" +#include "driver/ledc.h" +#include "hal/ledc_types.h" #include "hal/pcnt_types.h" /* Pinouts */ #define PCNT_A_PIN 4 /* Rotary Encoder A Pin */ #define PCNT_B_PIN 5 /* Rotary Encoder B Pin */ #define GPIO_BTN_PIN 6 /* Rotary Encoder switch */ +#define LEDC_R_PIN 7 /* Red LED */ +#define LEDC_G_PIN 15 /* Green LED */ +#define LEDC_B_PIN 16 /* Blue LED */ /* Pulse Count Settings */ #define PCNT_HIGH_LIMIT 2 /* 100 total steps */ #define PCNT_LOW_LIMIT -2 - +pcnt_unit_handle_t pcnt_unit; static QueueHandle_t pcnt_queue = NULL; static const char *TAG = "RE Demo"; /* logging tag */ -pcnt_unit_handle_t pcnt_unit; +/* Initialize LEDC for RGB LEDS in encoder */ +static void ledc_init(void) { + ESP_LOGI(TAG, "Initializing LEDC Driver"); + /* Setup LEDC PWM timer configuration */ + 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)); + + /* Setup Clock LED */ + ledc_channel_config_t ledc_R_channel = { + .channel = LEDC_CHANNEL_0, + .gpio_num = LEDC_R_PIN, + .speed_mode = LEDC_LOW_SPEED_MODE, + .timer_sel = LEDC_TIMER_0, + .intr_type = LEDC_INTR_DISABLE, + .duty = 0, + .hpoint = 0 + }; + ESP_ERROR_CHECK(ledc_channel_config(&ledc_R_channel)); + + /* Setup Gate LED */ + ledc_channel_config_t ledc_G_channel = { + .channel = LEDC_CHANNEL_1, + .gpio_num = LEDC_G_PIN, + .speed_mode = LEDC_LOW_SPEED_MODE, + .timer_sel = LEDC_TIMER_0, + .intr_type = LEDC_INTR_DISABLE, + .duty = 0, + .hpoint = 0 + }; + ESP_ERROR_CHECK(ledc_channel_config(&ledc_G_channel)); + + /* Setup Gate LED */ + ledc_channel_config_t ledc_B_channel = { + .channel = LEDC_CHANNEL_2, + .gpio_num = LEDC_B_PIN, + .speed_mode = LEDC_LOW_SPEED_MODE, + .timer_sel = LEDC_TIMER_0, + .intr_type = LEDC_INTR_DISABLE, + .duty = 0, + .hpoint = 0 + }; + ESP_ERROR_CHECK(ledc_channel_config(&ledc_B_channel)); +} /* Pulse Counter Callback */ static bool pcnt_cb(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx) { @@ -100,6 +153,10 @@ void app_main(void){ /* Initialize pulse counter */ pcnt_init(); + ledc_init(); + + /* ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 4095)); */ + /* ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0)); */ /* Log counter value */ int event_count = 0;