Feat: add in ledc control

main
rkcf 2023-02-08 15:26:02 -06:00
parent 3d9281a537
commit aedbb7ef94
1 changed files with 59 additions and 2 deletions

View File

@ -8,23 +8,76 @@
#include "freertos/queue.h" #include "freertos/queue.h"
#include "esp_log.h" #include "esp_log.h"
#include "driver/pulse_cnt.h" #include "driver/pulse_cnt.h"
#include "driver/ledc.h"
#include "hal/ledc_types.h"
#include "hal/pcnt_types.h" #include "hal/pcnt_types.h"
/* Pinouts */ /* Pinouts */
#define PCNT_A_PIN 4 /* Rotary Encoder A Pin */ #define PCNT_A_PIN 4 /* Rotary Encoder A Pin */
#define PCNT_B_PIN 5 /* Rotary Encoder B Pin */ #define PCNT_B_PIN 5 /* Rotary Encoder B Pin */
#define GPIO_BTN_PIN 6 /* Rotary Encoder switch */ #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 */ /* Pulse Count Settings */
#define PCNT_HIGH_LIMIT 2 /* 100 total steps */ #define PCNT_HIGH_LIMIT 2 /* 100 total steps */
#define PCNT_LOW_LIMIT -2 #define PCNT_LOW_LIMIT -2
pcnt_unit_handle_t pcnt_unit;
static QueueHandle_t pcnt_queue = NULL; static QueueHandle_t pcnt_queue = NULL;
static const char *TAG = "RE Demo"; /* logging tag */ 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 */ /* Pulse Counter Callback */
static bool pcnt_cb(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx) { 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 */ /* Initialize pulse counter */
pcnt_init(); 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 */ /* Log counter value */
int event_count = 0; int event_count = 0;