2025-12-28 12:46:10 -06:00
|
|
|
#ifndef ENCODER_H
|
|
|
|
|
#define ENCODER_H
|
2025-12-28 15:31:20 -06:00
|
|
|
#include "driver/gpio.h"
|
2025-12-28 20:52:11 -06:00
|
|
|
#include <atomic>
|
2025-12-28 12:46:10 -06:00
|
|
|
|
2025-12-28 15:31:20 -06:00
|
|
|
class Encoder {
|
|
|
|
|
public:
|
|
|
|
|
// Shared between ISR and main code
|
2025-12-28 20:52:11 -06:00
|
|
|
std::atomic<int32_t> count;
|
2025-12-28 15:31:20 -06:00
|
|
|
|
|
|
|
|
// ISR-only state
|
|
|
|
|
uint8_t last_state_a;
|
|
|
|
|
uint8_t last_state_b;
|
|
|
|
|
int8_t last_count_base;
|
|
|
|
|
|
|
|
|
|
// Configuration
|
|
|
|
|
gpio_num_t pin_a;
|
|
|
|
|
gpio_num_t pin_b;
|
|
|
|
|
|
|
|
|
|
// Static ISR that receives instance pointer via arg
|
|
|
|
|
static void isr_handler(void* arg);
|
|
|
|
|
|
|
|
|
|
// Constructor and methods
|
|
|
|
|
Encoder(gpio_num_t pinA, gpio_num_t pinB);
|
|
|
|
|
void init();
|
|
|
|
|
int32_t getCount() const { return count; }
|
|
|
|
|
void setCount(int32_t value) { count = value; }
|
|
|
|
|
void deinit();
|
|
|
|
|
};
|
2025-12-28 12:46:10 -06:00
|
|
|
|
|
|
|
|
#endif
|