Browse Source

new rotary encoder implementation

main
Hendrik Langer 7 years ago
parent
commit
8da18eeb9f
  1. 12
      src/rotary.cpp
  2. 5
      src/rotary.h

12
src/rotary.cpp

@ -44,9 +44,15 @@ void Rotary::task(void *pvParameters) {
uint32_t ulNotificationValue; uint32_t ulNotificationValue;
while(true) { while(true) {
ulNotificationValue = ulTaskNotifyTake( pdFALSE, portMAX_DELAY ); ulNotificationValue = ulTaskNotifyTake( pdFALSE, portMAX_DELAY );
int newPos = encoderPos/2; debouncePulses++;
if (lastPos != newPos) if (debouncePulses > 3) {
if (callback) callback(newPos); debouncePulses = 0;
if (encoderPos > encoderPosOld+1) value++;
else if (encoderPos < encoderPosOld-1) value--;
else continue;
encoderPosOld = encoderPos;
if (callback) callback(value);
}
} }
vTaskDelete(NULL); vTaskDelete(NULL);
} }

5
src/rotary.h

@ -20,9 +20,12 @@ class Rotary {
static void IRAM_ATTR doEncoder(void); static void IRAM_ATTR doEncoder(void);
static Rotary* instance; static Rotary* instance;
volatile int encoderPos = 0; volatile int encoderPos = 0;
int lastPos = 0; int encoderPosOld = 0;
int value = 0;
uint32_t debounceMillis; uint32_t debounceMillis;
uint32_t debounceDelay = 1; uint32_t debounceDelay = 1;
uint32_t debouncePulses = 0;
int countDirection = 0;
TaskHandle_t xTaskToNotify; TaskHandle_t xTaskToNotify;
private: private:
uint8_t pinA; uint8_t pinA;

Loading…
Cancel
Save