From 531cb9b0a95b9666010ecfa2db9bf0bae2013189 Mon Sep 17 00:00:00 2001 From: rkcf Date: Wed, 8 Feb 2023 12:51:12 -0600 Subject: [PATCH] Refactor: Move pulse control initialization to seperate function --- main/esp32_rgb_rotary_encoder.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/main/esp32_rgb_rotary_encoder.c b/main/esp32_rgb_rotary_encoder.c index 75d763d..50c2add 100644 --- a/main/esp32_rgb_rotary_encoder.c +++ b/main/esp32_rgb_rotary_encoder.c @@ -22,6 +22,8 @@ static QueueHandle_t pcnt_queue = NULL; 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; @@ -30,16 +32,14 @@ static bool pcnt_cb(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edat return (high_task_wakeup == pdTRUE); } -/* Main Loop */ -void app_main(void){ - +/* Initialize the pulse counter peripheral */ +static void pcnt_init() { /* Configure the PCNT unit and get a handle */ ESP_LOGI(TAG, "Setting up Pulse Counter Unit"); pcnt_unit_config_t pcnt_unit_config = { .high_limit = PCNT_HIGH_LIMIT, .low_limit = PCNT_LOW_LIMIT }; - pcnt_unit_handle_t pcnt_unit = NULL; ESP_ERROR_CHECK(pcnt_new_unit(&pcnt_unit_config, &pcnt_unit)); /* Setup glitch filter */ @@ -91,6 +91,14 @@ void app_main(void){ ESP_ERROR_CHECK(pcnt_unit_enable(pcnt_unit)); ESP_ERROR_CHECK(pcnt_unit_clear_count(pcnt_unit)); ESP_ERROR_CHECK(pcnt_unit_start(pcnt_unit)); +} + + +/* Main Loop */ +void app_main(void){ + + /* Initialize pulse counter */ + pcnt_init(); /* Log counter value */ int pulse_count = 0;