-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton_handler.cpp
More file actions
77 lines (64 loc) · 2.61 KB
/
button_handler.cpp
File metadata and controls
77 lines (64 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "button_handler.h"
#include "bindicator.h"
#include "config_manager.h"
#include "display_handler.h"
ButtonHandler::ButtonHandler(uint8_t pin, unsigned long longPressTime)
: buttonPin(pin), longPressTime(longPressTime) {}
void ButtonHandler::begin() {
pinMode(buttonPin, INPUT_PULLUP);
state.mutex = xSemaphoreCreateMutex();
xTaskCreate(
buttonTask, // Task function
"ButtonTask", // Name for debugging
2048, // Stack size (bytes)
this, // Pass the instance pointer as parameter
1, // Priority
NULL // Task handle
);
}
void ButtonHandler::buttonTask(void *parameter) {
ButtonHandler* handler = static_cast<ButtonHandler*>(parameter);
TickType_t lastWakeTime = xTaskGetTickCount();
const TickType_t interval = pdMS_TO_TICKS(20);
while (true) {
if (xSemaphoreTake(handler->state.mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
handler->state.currentState = digitalRead(handler->buttonPin);
// Button press detected
if (handler->state.currentState == LOW && handler->state.lastState == HIGH) {
handler->state.pressedTime = millis();
handler->state.isLongPressHandled = false;
}
// Button is being held
else if (handler->state.currentState == LOW && !handler->state.isLongPressHandled) {
if (millis() - handler->state.pressedTime >= handler->longPressTime) {
handler->onLongPress();
handler->state.isLongPressHandled = true;
}
}
// Button release detected
else if (handler->state.currentState == HIGH && handler->state.lastState == LOW) {
if (!handler->state.isLongPressHandled &&
(millis() - handler->state.pressedTime < handler->longPressTime)) {
handler->onShortPress();
}
}
handler->state.lastState = handler->state.currentState;
xSemaphoreGive(handler->state.mutex);
}
vTaskDelayUntil(&lastWakeTime, interval);
}
}
// Main button
void BindicatorButton::onShortPress() {
Serial.println("Short press detected");
Bindicator::handleButtonPress();
}
void BindicatorButton::onLongPress() {
Serial.println("Long press detected - entering setup mode");
ConfigManager::setForcedSetupFlag("restart-in-setup-mode");
extern DisplayHandler display;
display.matrix.clear();
display.matrix.show();
delay(50);
ESP.restart();
}