Improved button handling
This commit is contained in:
parent
ac306cb473
commit
94c2a7e43b
@ -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
|
||||||
|
|
||||||
|
@ -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:
|
|
||||||
}
|
}
|
||||||
|
@ -3,71 +3,70 @@
|
|||||||
* Button 2: Next Screen
|
* Button 2: Next Screen
|
||||||
* Button 3: Previous Screen
|
* Button 3: Previous Screen
|
||||||
* Button 4: Queue full EPD update
|
* Button 4: Queue full EPD update
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#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)
|
||||||
{
|
{
|
||||||
if (!digitalRead(MCP_INT_PIN))
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||||
|
TickType_t currentTime = xTaskGetTickCount();
|
||||||
|
if ((currentTime - lastDebounceTime) >= debounceDelay)
|
||||||
{
|
{
|
||||||
uint pin = mcp.getLastInterruptPin();
|
lastDebounceTime = currentTime;
|
||||||
if (pin == 3) {
|
|
||||||
// xTaskCreate(fullRefresh, "FullRefresh", 2048, NULL, 1, NULL);
|
|
||||||
toggleScreenTimer();
|
|
||||||
}
|
|
||||||
else if (pin == 1)
|
|
||||||
{
|
|
||||||
previousScreen();
|
|
||||||
}
|
|
||||||
else if (pin == 2)
|
|
||||||
{
|
|
||||||
nextScreen();
|
|
||||||
}
|
|
||||||
else if (pin == 0)
|
|
||||||
{
|
|
||||||
showNetworkSettings();
|
|
||||||
}
|
|
||||||
|
|
||||||
vTaskDelay(250); // debounce
|
if (!digitalRead(MCP_INT_PIN))
|
||||||
mcp.clearInterrupts(); // clear
|
{
|
||||||
|
uint pin = mcp.getLastInterruptPin();
|
||||||
|
|
||||||
|
switch (pin)
|
||||||
|
{
|
||||||
|
case 3:
|
||||||
|
toggleScreenTimer();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
nextScreen();
|
||||||
|
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(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)
|
||||||
|
Loading…
Reference in New Issue
Block a user