|
|
@ -16,8 +16,11 @@ |
|
|
|
//using namespace std;
|
|
|
|
|
|
|
|
TaskHandle_t Rotary::xTaskToNotify = NULL; |
|
|
|
Rotary* Rotary::instance = NULL; |
|
|
|
|
|
|
|
Rotary::Rotary() {} |
|
|
|
Rotary::Rotary() { |
|
|
|
instance = this; |
|
|
|
} |
|
|
|
|
|
|
|
bool Rotary::begin(uint8_t pinA, uint8_t pinB, uint8_t pinButton) { |
|
|
|
this->pinA = pinA; |
|
|
@ -44,82 +47,46 @@ void Rotary::cTaskWrapper(void* parameters) { |
|
|
|
void Rotary::button_task(void *pvParameters) { |
|
|
|
xTaskToNotify = xTaskGetCurrentTaskHandle(); |
|
|
|
|
|
|
|
attachInterrupt(digitalPinToInterrupt(pinA), doEncoderA, CHANGE); |
|
|
|
attachInterrupt(digitalPinToInterrupt(pinB), doEncoderB, CHANGE); |
|
|
|
// attachInterrupt(digitalPinToInterrupt(pinButton), doButton, CHANGE);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(pinA), doEncoderA, RISING); |
|
|
|
attachInterrupt(digitalPinToInterrupt(pinB), doEncoderB, RISING); |
|
|
|
// attachInterrupt(digitalPinToInterrupt(pinButton), doButton, FALLING);
|
|
|
|
|
|
|
|
uint32_t ulNotificationValue; |
|
|
|
uint32_t ulStatusValue; |
|
|
|
|
|
|
|
while(true) { |
|
|
|
rotating = true; // reset the debouncer
|
|
|
|
|
|
|
|
ulNotificationValue = xTaskNotifyWait(0x00, ULONG_MAX, &ulNotificationValue, portMAX_DELAY ); |
|
|
|
Serial.printf("interrupt %d, rotating: %d\n", ulNotificationValue, rotating); |
|
|
|
|
|
|
|
if( ulNotificationValue == 1 ) { |
|
|
|
// debounce
|
|
|
|
if ( rotating ) delay (1); // wait a little until the bouncing is done
|
|
|
|
ulNotificationValue = xTaskNotifyWait(0x00, ULONG_MAX, &ulStatusValue, portMAX_DELAY ); |
|
|
|
|
|
|
|
// Test transition, did things really change?
|
|
|
|
if ( digitalRead(pinA) != A_set ) { // debounce once more
|
|
|
|
A_set = !A_set; |
|
|
|
// if ( rotating ) delay (1); // wait a little until the bouncing is done
|
|
|
|
|
|
|
|
if( ulStatusValue == 1 ) { |
|
|
|
// adjust counter + if A leads B
|
|
|
|
if ( A_set && !B_set ) |
|
|
|
encoderPos += 1; |
|
|
|
|
|
|
|
changed(); |
|
|
|
rotating = false; // no more debouncing until loop() hits again
|
|
|
|
changed(); |
|
|
|
} |
|
|
|
|
|
|
|
} else if ( ulNotificationValue == 2 ) { |
|
|
|
if ( rotating ) delay (1); |
|
|
|
if ( digitalRead(pinB) != B_set ) { |
|
|
|
B_set = !B_set; |
|
|
|
// adjust counter - 1 if B leads A
|
|
|
|
if ( B_set && !A_set ) |
|
|
|
} else if ( ulStatusValue == 2 ) { |
|
|
|
// adjust counter -1 if B leads A
|
|
|
|
encoderPos -= 1; |
|
|
|
|
|
|
|
rotating = false; |
|
|
|
changed(); |
|
|
|
} |
|
|
|
} else if ( ulNotificationValue == 4 ) { |
|
|
|
delay(1); |
|
|
|
if ( digitalRead(pinButton) != Button_set ) { |
|
|
|
Button_set = !Button_set; |
|
|
|
changed(); |
|
|
|
rotating = false; // no more debouncing until loop() hits again
|
|
|
|
} else if ( ulStatusValue == 4 ) { |
|
|
|
Button_set = digitalRead(pinButton); |
|
|
|
|
|
|
|
if (! Button_set ) { |
|
|
|
encoderPos = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
// main loop, work is done by interrupt service routines, this one only prints stuff
|
|
|
|
void loop() { |
|
|
|
rotating = true; // reset the debouncer
|
|
|
|
|
|
|
|
if (lastReportedPos != encoderPos) { |
|
|
|
Serial.print("Index:"); |
|
|
|
Serial.println(encoderPos, DEC); |
|
|
|
lastReportedPos = encoderPos; |
|
|
|
} |
|
|
|
if (digitalRead(clearButton) == LOW ) { |
|
|
|
encoderPos = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
void Rotary::changed() { |
|
|
|
if (lastReportedPos != encoderPos) { |
|
|
|
Serial.print("encoder: "); |
|
|
|
Serial.println(encoderPos); |
|
|
|
// if (callback) callback(encoderPos);
|
|
|
|
if (callback) callback(encoderPos); |
|
|
|
lastReportedPos = encoderPos; |
|
|
|
} |
|
|
|
rotating = true; // reset the debouncer
|
|
|
|
} |
|
|
|
|
|
|
|
bool Rotary::registerCallback(std::function<void(int)> callback) { |
|
|
@ -132,27 +99,43 @@ void Rotary::doButton() { |
|
|
|
|
|
|
|
configASSERT( xTaskToNotify != NULL ); |
|
|
|
uint32_t intrBits = 4; |
|
|
|
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithOverwrite, &xHigherPriorityTaskWoken ); |
|
|
|
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithoutOverwrite, &xHigherPriorityTaskWoken ); |
|
|
|
portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
|
|
|
} |
|
|
|
|
|
|
|
// Interrupt on A changing state
|
|
|
|
void Rotary::doEncoderA() { |
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE; |
|
|
|
|
|
|
|
configASSERT( xTaskToNotify != NULL ); |
|
|
|
uint32_t intrBits = 1; |
|
|
|
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithOverwrite, &xHigherPriorityTaskWoken ); |
|
|
|
portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
|
|
|
if (instance->skipA) { |
|
|
|
instance->skipA = false; |
|
|
|
instance->debounceA = millis(); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (!digitalRead(instance->pinB) && ((millis() - instance->debounceA) > instance->debounceDelay)) { |
|
|
|
instance->debounceA = millis(); |
|
|
|
instance->skipB = true; |
|
|
|
configASSERT( xTaskToNotify != NULL ); |
|
|
|
uint32_t intrBits = 1; |
|
|
|
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithoutOverwrite, &xHigherPriorityTaskWoken ); |
|
|
|
portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Interrupt on B changing state, same as A above
|
|
|
|
void Rotary::doEncoderB() { |
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE; |
|
|
|
|
|
|
|
configASSERT( xTaskToNotify != NULL ); |
|
|
|
uint32_t intrBits = 2; |
|
|
|
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithOverwrite, &xHigherPriorityTaskWoken ); |
|
|
|
portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
|
|
|
if (instance->skipB) { |
|
|
|
instance->skipB = false; |
|
|
|
instance->debounceB = millis(); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (!digitalRead(instance->pinA) && ((millis() - instance->debounceB) > instance->debounceDelay)) { |
|
|
|
instance->debounceB = millis(); |
|
|
|
instance->skipA = true; |
|
|
|
configASSERT( xTaskToNotify != NULL ); |
|
|
|
uint32_t intrBits = 2; |
|
|
|
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithoutOverwrite, &xHigherPriorityTaskWoken ); |
|
|
|
portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|