diff --git a/src/rotary.cpp b/src/rotary.cpp index 99c2386..21797ab 100644 --- a/src/rotary.cpp +++ b/src/rotary.cpp @@ -44,9 +44,15 @@ void Rotary::task(void *pvParameters) { uint32_t ulNotificationValue; while(true) { ulNotificationValue = ulTaskNotifyTake( pdFALSE, portMAX_DELAY ); - int newPos = encoderPos/2; - if (lastPos != newPos) - if (callback) callback(newPos); + debouncePulses++; + if (debouncePulses > 3) { + debouncePulses = 0; + if (encoderPos > encoderPosOld+1) value++; + else if (encoderPos < encoderPosOld-1) value--; + else continue; + encoderPosOld = encoderPos; + if (callback) callback(value); + } } vTaskDelete(NULL); } diff --git a/src/rotary.h b/src/rotary.h index 916602e..60a4325 100644 --- a/src/rotary.h +++ b/src/rotary.h @@ -20,9 +20,12 @@ class Rotary { static void IRAM_ATTR doEncoder(void); static Rotary* instance; volatile int encoderPos = 0; - int lastPos = 0; + int encoderPosOld = 0; + int value = 0; uint32_t debounceMillis; uint32_t debounceDelay = 1; + uint32_t debouncePulses = 0; + int countDirection = 0; TaskHandle_t xTaskToNotify; private: uint8_t pinA;