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();
// delay(200);
pinMode(MCP_INT_PIN, INPUT);
mcp.setupInterrupts(true, false, LOW);
mcp.setupInterrupts(false, false, LOW);
}
#endif

View file

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

View file

@ -3,71 +3,70 @@
* Button 2: Next Screen
* Button 3: Previous Screen
* Button 4: Queue full EPD update
*/
*/
#include "button.hpp"
#ifndef NO_MCP
TaskHandle_t buttonTaskHandle = NULL;
// Define a type for the event callback
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)
{
while (1)
{
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
TickType_t currentTime = xTaskGetTickCount();
if ((currentTime - lastDebounceTime) >= debounceDelay)
{
lastDebounceTime = currentTime;
if (!digitalRead(MCP_INT_PIN))
{
uint pin = mcp.getLastInterruptPin();
if (pin == 3) {
// xTaskCreate(fullRefresh, "FullRefresh", 2048, NULL, 1, NULL);
switch (pin)
{
case 3:
toggleScreenTimer();
}
else if (pin == 1)
{
previousScreen();
}
else if (pin == 2)
{
break;
case 2:
nextScreen();
}
else if (pin == 0)
{
break;
case 1:
previousScreen();
break;
case 0:
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()
{
buttonPressed = true;
// Serial.println(F("ISR"));
// uint pin = mcp.getLastInterruptPin();
// if (pin == 1)
// {
// nextScreen();
// }
// else if (pin == 2)
// {
// previousScreen();
// }
// vTaskDelay(pdMS_TO_TICKS(250));
// mcp.clearInterrupts();
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(buttonTaskHandle, 0, eNoAction, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE)
{
portYIELD_FROM_ISR();
}
}
void setupButtonTask()
{
xTaskCreate(buttonTask, "ButtonTask", 4096, NULL, 1, &buttonTaskHandle); // Create the FreeRTOS task
// Use interrupt instead of task
// attachInterrupt(MCP_INT_PIN, handleButtonInterrupt, FALLING);
attachInterrupt(MCP_INT_PIN, handleButtonInterrupt, CHANGE);
}
void registerNewButtonCallback(const EventCallback cb)