From 3d9281a53733f4fdf4d87a5a4e515d4e00e867b4 Mon Sep 17 00:00:00 2001 From: rkcf Date: Wed, 8 Feb 2023 14:25:31 -0600 Subject: [PATCH] Fix: pulse count now correctly interrupts on one turn --- main/esp32_rgb_rotary_encoder.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/main/esp32_rgb_rotary_encoder.c b/main/esp32_rgb_rotary_encoder.c index 50c2add..c94c3e0 100644 --- a/main/esp32_rgb_rotary_encoder.c +++ b/main/esp32_rgb_rotary_encoder.c @@ -11,12 +11,13 @@ #include "hal/pcnt_types.h" /* Pinouts */ -#define PCNT_A_PIN 5 /* Rotary Encoder A Pin */ -#define PCNT_B_PIN 4 /* Rotary Encoder B Pin */ +#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 */ /* Pulse Count Settings */ -#define PCNT_HIGH_LIMIT 100 /* 100 total steps */ -#define PCNT_LOW_LIMIT -100 +#define PCNT_HIGH_LIMIT 2 /* 100 total steps */ +#define PCNT_LOW_LIMIT -2 static QueueHandle_t pcnt_queue = NULL; @@ -24,6 +25,7 @@ static const char *TAG = "RE Demo"; /* logging tag */ pcnt_unit_handle_t pcnt_unit; + /* Pulse Counter Callback */ static bool pcnt_cb(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx) { BaseType_t high_task_wakeup; @@ -72,7 +74,7 @@ static void pcnt_init() { /* Install watch points */ ESP_LOGI(TAG, "Installing watch points and callback handler"); - int watch_points[] = {-10, 10}; + int watch_points[] = {-2, 2}; /* This encoder has detents which change in steps of 2 */ for (size_t i = 0; i < sizeof(watch_points) / sizeof(watch_points[0]); i++) { ESP_ERROR_CHECK(pcnt_unit_add_watch_point(pcnt_unit, watch_points[i])); } @@ -93,7 +95,6 @@ static void pcnt_init() { ESP_ERROR_CHECK(pcnt_unit_start(pcnt_unit)); } - /* Main Loop */ void app_main(void){ @@ -101,15 +102,11 @@ void app_main(void){ pcnt_init(); /* Log counter value */ - int pulse_count = 0; int event_count = 0; while (1) { - if (xQueueReceive(pcnt_queue, &event_count, pdMS_TO_TICKS(1000))) { + if (xQueueReceive(pcnt_queue, &event_count, portMAX_DELAY)) { ESP_LOGI(TAG, "Watch point event, count: %d", event_count); - } else { - ESP_ERROR_CHECK(pcnt_unit_get_count(pcnt_unit, &pulse_count)); - ESP_LOGI(TAG, "Pulse count: %d", pulse_count); } } }