Improved button handling

This commit is contained in:
Djuri Baars 2023-11-05 14:15:52 +01:00
parent ac306cb473
commit 94c2a7e43b
3 changed files with 44 additions and 46 deletions

View file

@ -85,7 +85,7 @@ void setupComponents()
pixels.show(); pixels.show();
// delay(200); // delay(200);
pinMode(MCP_INT_PIN, INPUT); pinMode(MCP_INT_PIN, INPUT);
mcp.setupInterrupts(true, false, LOW); mcp.setupInterrupts(false, false, LOW);
} }
#endif #endif

View file

@ -62,7 +62,6 @@ void setup()
BlockHeightScreen::init(); BlockHeightScreen::init();
HalvingCountdownScreen::init(); HalvingCountdownScreen::init();
TickerScreen::init(); TickerScreen::init();
// SatsPerDollarScreen::init();
#ifdef WITH_BUTTONS #ifdef WITH_BUTTONS
setupButtonTask(); setupButtonTask();
@ -77,15 +76,15 @@ void setup()
registerNewBlockCallback(BlockHeightScreen::onNewBlock); registerNewBlockCallback(BlockHeightScreen::onNewBlock);
registerNewBlockCallback(HalvingCountdownScreen::onNewBlock); registerNewBlockCallback(HalvingCountdownScreen::onNewBlock);
registerNewPriceCallback(TickerScreen::onPriceUpdate); registerNewPriceCallback(TickerScreen::onPriceUpdate);
// registerNewPriceCallback(SatsPerDollarScreen::onPriceUpdate);
setupDisplays(); setupDisplays();
} else { }
else
{
setupI2C(); setupI2C();
} }
} }
void loop() void loop()
{ {
// put your main code here, to run repeatedly:
} }

View file

@ -8,66 +8,65 @@
#include "button.hpp" #include "button.hpp"
#ifndef NO_MCP #ifndef NO_MCP
TaskHandle_t buttonTaskHandle = NULL; TaskHandle_t buttonTaskHandle = NULL;
// Define a type for the event callback
std::vector<EventCallback> buttonEventCallbacks; // Define a vector to hold multiple event callbacks std::vector<EventCallback> buttonEventCallbacks; // Define a vector to hold multiple event callbacks
volatile boolean buttonPressed = false; const TickType_t debounceDelay = pdMS_TO_TICKS(50);
TickType_t lastDebounceTime = 0;
void buttonTask(void *parameter) void buttonTask(void *parameter)
{ {
while (1) while (1)
{ {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
TickType_t currentTime = xTaskGetTickCount();
if ((currentTime - lastDebounceTime) >= debounceDelay)
{
lastDebounceTime = currentTime;
if (!digitalRead(MCP_INT_PIN)) if (!digitalRead(MCP_INT_PIN))
{ {
uint pin = mcp.getLastInterruptPin(); uint pin = mcp.getLastInterruptPin();
if (pin == 3) {
// xTaskCreate(fullRefresh, "FullRefresh", 2048, NULL, 1, NULL); switch (pin)
{
case 3:
toggleScreenTimer(); toggleScreenTimer();
} break;
else if (pin == 1) case 2:
{
previousScreen();
}
else if (pin == 2)
{
nextScreen(); nextScreen();
} break;
else if (pin == 0) case 1:
{ previousScreen();
break;
case 0:
showNetworkSettings(); showNetworkSettings();
break;
}
}
mcp.clearInterrupts();
// Very ugly, but for some reason this is necessary
while (!digitalRead(MCP_INT_PIN))
{
mcp.clearInterrupts();
} }
vTaskDelay(250); // debounce
mcp.clearInterrupts(); // clear
} }
vTaskDelay(pdMS_TO_TICKS(250));
} }
} }
void IRAM_ATTR handleButtonInterrupt() void IRAM_ATTR handleButtonInterrupt()
{ {
buttonPressed = true; BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// Serial.println(F("ISR")); xTaskNotifyFromISR(buttonTaskHandle, 0, eNoAction, &xHigherPriorityTaskWoken);
// uint pin = mcp.getLastInterruptPin(); if (xHigherPriorityTaskWoken == pdTRUE)
{
// if (pin == 1) portYIELD_FROM_ISR();
// { }
// nextScreen();
// }
// else if (pin == 2)
// {
// previousScreen();
// }
// vTaskDelay(pdMS_TO_TICKS(250));
// mcp.clearInterrupts();
} }
void setupButtonTask() void setupButtonTask()
{ {
xTaskCreate(buttonTask, "ButtonTask", 4096, NULL, 1, &buttonTaskHandle); // Create the FreeRTOS task xTaskCreate(buttonTask, "ButtonTask", 4096, NULL, 1, &buttonTaskHandle); // Create the FreeRTOS task
// Use interrupt instead of task // Use interrupt instead of task
// attachInterrupt(MCP_INT_PIN, handleButtonInterrupt, FALLING); attachInterrupt(MCP_INT_PIN, handleButtonInterrupt, CHANGE);
} }
void registerNewButtonCallback(const EventCallback cb) void registerNewButtonCallback(const EventCallback cb)