btclock_v3/src/lib/button_handler.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

2023-11-07 20:26:15 +00:00
#include "button_handler.hpp"
TaskHandle_t buttonTaskHandle = NULL;
const TickType_t debounceDelay = pdMS_TO_TICKS(50);
TickType_t lastDebounceTime = 0;
2023-11-30 21:38:01 +00:00
void buttonTask(void *parameter) {
while (1) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
std::lock_guard<std::mutex> lock(mcpMutex);
2023-11-13 19:02:58 +00:00
2023-11-30 21:38:01 +00:00
TickType_t currentTime = xTaskGetTickCount();
if ((currentTime - lastDebounceTime) >= debounceDelay) {
lastDebounceTime = currentTime;
2023-11-07 20:26:15 +00:00
2023-11-30 21:38:01 +00:00
if (!digitalRead(MCP_INT_PIN)) {
uint pin = mcp1.getLastInterruptPin();
2023-11-07 20:26:15 +00:00
2023-11-30 21:38:01 +00:00
switch (pin) {
2023-11-30 21:56:50 +00:00
case 3:
toggleTimerActive();
break;
case 2:
nextScreen();
break;
case 1:
previousScreen();
break;
case 0:
showSystemStatusScreen();
break;
2023-11-07 20:26:15 +00:00
}
2023-11-30 21:38:01 +00:00
}
mcp1.clearInterrupts();
} else {
2023-11-07 20:26:15 +00:00
}
2023-11-30 21:38:01 +00:00
// Very ugly, but for some reason this is necessary
while (!digitalRead(MCP_INT_PIN)) {
mcp1.clearInterrupts();
}
}
2023-11-07 20:26:15 +00:00
}
2023-11-30 21:38:01 +00:00
void IRAM_ATTR handleButtonInterrupt() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(buttonTaskHandle, 0, eNoAction, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
2023-11-07 20:26:15 +00:00
}
2023-11-30 21:38:01 +00:00
void setupButtonTask() {
xTaskCreate(buttonTask, "ButtonTask", 4096, NULL, tskIDLE_PRIORITY,
2023-11-30 21:56:50 +00:00
&buttonTaskHandle); // Create the FreeRTOS task
2023-11-30 21:38:01 +00:00
// Use interrupt instead of task
attachInterrupt(MCP_INT_PIN, handleButtonInterrupt, CHANGE);
2023-11-07 20:26:15 +00:00
}