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;
|
|
|
|
|
|
|
|
void buttonTask(void *parameter)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
2023-11-13 19:02:58 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mcpMutex);
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
TickType_t currentTime = xTaskGetTickCount();
|
|
|
|
if ((currentTime - lastDebounceTime) >= debounceDelay)
|
|
|
|
{
|
|
|
|
lastDebounceTime = currentTime;
|
|
|
|
|
|
|
|
if (!digitalRead(MCP_INT_PIN))
|
|
|
|
{
|
2023-11-25 23:51:54 +00:00
|
|
|
uint pin = mcp1.getLastInterruptPin();
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
switch (pin)
|
|
|
|
{
|
|
|
|
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-25 23:51:54 +00:00
|
|
|
mcp1.clearInterrupts();
|
2023-11-13 19:02:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
}
|
|
|
|
// Very ugly, but for some reason this is necessary
|
|
|
|
while (!digitalRead(MCP_INT_PIN))
|
|
|
|
{
|
2023-11-25 23:51:54 +00:00
|
|
|
mcp1.clearInterrupts();
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRAM_ATTR handleButtonInterrupt()
|
|
|
|
{
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
xTaskNotifyFromISR(buttonTaskHandle, 0, eNoAction, &xHigherPriorityTaskWoken);
|
|
|
|
if (xHigherPriorityTaskWoken == pdTRUE)
|
|
|
|
{
|
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setupButtonTask()
|
|
|
|
{
|
|
|
|
xTaskCreate(buttonTask, "ButtonTask", 4096, NULL, tskIDLE_PRIORITY, &buttonTaskHandle); // Create the FreeRTOS task
|
|
|
|
// Use interrupt instead of task
|
|
|
|
attachInterrupt(MCP_INT_PIN, handleButtonInterrupt, CHANGE);
|
|
|
|
}
|