From 12b38f15f1cdcf9cf78e91167e0043016da84754 Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Sun, 28 Dec 2025 12:46:10 -0600 Subject: [PATCH] pure encoder test working on pins d5 and d6 --- include/defines.h | 4 +++ include/encoder.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++ include/encoder.hpp | 11 +++++++ src/main.cpp | 2 +- 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 include/encoder.cpp create mode 100644 include/encoder.hpp diff --git a/include/defines.h b/include/defines.h index 6c3072d..67b9b16 100644 --- a/include/defines.h +++ b/include/defines.h @@ -1,5 +1,6 @@ #ifndef DEFINES_H #define DEFINES_H +#include "driver/gpio.h" #define ccwSpeed 6500 #define cwSpeed 3300 @@ -15,6 +16,9 @@ #define nvsAuth "AUTH" #define tokenTag "TOKEN" +#define ENCODER_PIN_A GPIO_NUM_23 +#define ENCODER_PIN_B GPIO_NUM_16 + #define getMovingCW(port) ((movingCW & (1 << port)) >> port) #define setMovingCW(port) (movingCW |= (1 << port)) #define clearMovingCW(port) (movingCW &= ~(1 << port)) diff --git a/include/encoder.cpp b/include/encoder.cpp new file mode 100644 index 0000000..4a43792 --- /dev/null +++ b/include/encoder.cpp @@ -0,0 +1,76 @@ +#include "encoder.hpp" +#include "defines.h" +#include "driver/gpio.h" +#include "esp_log.h" +#include "esp_pm.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_sleep.h" +#include "soc/gpio_struct.h" // <--- REQUIRED for GPIO.in.val + +volatile int32_t encoder_count = 0; +static const char *TAG = "ENCODER_DEMO"; + +// --- THE SAFE ISR (RAM ONLY) --- +static void IRAM_ATTR encoder_isr_handler(void* arg) +{ + static uint8_t last_state_a = 0; + static uint8_t last_state_b = 0; + static int8_t last_count_base = 0; + + // READ HARDWARE DIRECTLY: This effectively bypasses the Flash crash risk + uint32_t gpio_levels = GPIO.in.val; + uint8_t current_a = (gpio_levels >> ENCODER_PIN_A) & 0x1; + uint8_t current_b = (gpio_levels >> ENCODER_PIN_B) & 0x1; + + // LOGIC + if (current_a != last_state_a) { + if (!current_a) { + if (current_b) last_count_base++; + else last_count_base--; + } + else { + if (current_b) last_count_base--; + else last_count_base++; + } + } + else if (current_b != last_state_b) { + if (!current_b) { + if (current_a) last_count_base--; + else last_count_base++; + } + else { + if (current_a) last_count_base++; + else last_count_base--; + } + } + if (last_count_base > 3) { + encoder_count += 1; + last_count_base -= 4; + } + else if (last_count_base < 0) { + encoder_count -= 1; + last_count_base += 4; + } + last_state_a = current_a; + last_state_b = current_b; +} + +void encoder_init(void) +{ + gpio_config_t io_conf = {}; + io_conf.intr_type = GPIO_INTR_ANYEDGE; + io_conf.pin_bit_mask = (1ULL << ENCODER_PIN_A) | (1ULL << ENCODER_PIN_B); + io_conf.mode = GPIO_MODE_INPUT; + // Internal Pull-ups prevent "floating" inputs waking the chip randomly + io_conf.pull_up_en = GPIO_PULLUP_ENABLE; + io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; + gpio_config(&io_conf); + + gpio_install_isr_service(ESP_INTR_FLAG_IRAM); // IRAM is vital for Sleep + + gpio_isr_handler_add(ENCODER_PIN_A, encoder_isr_handler, NULL); + gpio_isr_handler_add(ENCODER_PIN_B, encoder_isr_handler, NULL); + + ESP_LOGI(TAG, "Encoder initialized with Sleep Wakeup support."); +} \ No newline at end of file diff --git a/include/encoder.hpp b/include/encoder.hpp new file mode 100644 index 0000000..9bf4d19 --- /dev/null +++ b/include/encoder.hpp @@ -0,0 +1,11 @@ +#ifndef ENCODER_H +#define ENCODER_H +#include +#include "esp_pm.h" + +extern volatile int32_t encoder_count; +extern esp_pm_lock_handle_t encoder_pm_lock; + +void encoder_init(); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fdc94a7..a6bf92c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,7 @@ #include "socketIO.hpp" #include "encoder.hpp" -extern "C" void app_main() { +void mainApp() { printf("Hello "); esp_err_t ret = nvs_flash_init(); // change to secure init logic soon!! // 2. If NVS is full or corrupt (common after flashing new code), erase and retry