initial button manager

This commit is contained in:
JQ 2025-05-27 19:10:56 -07:00
parent d8c2b3ab47
commit e7761dc9dc
4 changed files with 288 additions and 49 deletions

View file

@ -57,6 +57,24 @@ void UITask::begin(DisplayDriver* display, NodePrefs* node_prefs, const char* bu
#ifdef PIN_BUZZER
buzzer.begin();
#endif
// Initialize button with appropriate configuration
#if defined(PIN_USER_BTN) || defined(PIN_USER_BTN_ANA)
#ifdef PIN_USER_BTN
_userButton = new Button(PIN_USER_BTN, USER_BTN_PRESSED);
#else
_userButton = new Button(PIN_USER_BTN_ANA, USER_BTN_PRESSED, true, 20);
#endif
_userButton->begin();
// Set up button callbacks
_userButton->onShortPress([this]() { handleButtonShortPress(); });
_userButton->onDoublePress([this]() { handleButtonDoublePress(); });
_userButton->onTriplePress([this]() { handleButtonTriplePress(); });
_userButton->onLongPress([this]() { handleButtonLongPress(); });
_userButton->onAnyPress([this]() { handleButtonAnyPress(); });
#endif
}
void UITask::soundBuzzer(UIEventType bet) {
@ -233,53 +251,8 @@ void UITask::userLedHandler() {
#endif
}
void UITask::buttonHandler() {
#if defined(PIN_USER_BTN) || defined(PIN_USER_BTN_ANA)
static int prev_btn_state = !USER_BTN_PRESSED;
static int prev_btn_state_ana = !USER_BTN_PRESSED;
static unsigned long btn_state_change_time = 0;
static unsigned long next_read = 0;
int cur_time = millis();
if (cur_time >= next_read) {
int btn_state = 0;
int btn_state_ana = 0;
#ifdef PIN_USER_BTN
btn_state = digitalRead(PIN_USER_BTN);
#endif
#ifdef PIN_USER_BTN_ANA
btn_state_ana = (analogRead(PIN_USER_BTN_ANA) < 20); // analogRead returns a value hopefully below 20 when button is pressed.
#endif
if (btn_state != prev_btn_state || btn_state_ana != prev_btn_state_ana) { // check for either digital or analogue button change of state
if (btn_state == USER_BTN_PRESSED || btn_state_ana == USER_BTN_PRESSED) { // pressed?
if (_display != NULL) {
if (_display->isOn()) {
clearMsgPreview();
} else {
_display->turnOn();
_need_refresh = true;
}
_auto_off = cur_time + AUTO_OFF_MILLIS; // extend auto-off timer
}
} else { // unpressed ? check pressed time ...
if ((cur_time - btn_state_change_time) > 5000) {
#ifdef PIN_STATUS_LED
digitalWrite(PIN_STATUS_LED, LOW);
delay(10);
#endif
shutdown(); // without restart
}
}
btn_state_change_time = millis();
prev_btn_state = btn_state;
prev_btn_state_ana = btn_state_ana;
}
next_read = millis() + 100; // 10 reads per second
}
#endif
}
/* hardware-agnostic pre-shutdown activity should be done here
/*
hardware-agnostic pre-shutdown activity should be done here
*/
void UITask::shutdown(bool restart){
@ -303,7 +276,11 @@ void UITask::shutdown(bool restart){
}
void UITask::loop() {
buttonHandler();
#if defined(PIN_USER_BTN) || defined(PIN_USER_BTN_ANA)
if (_userButton) {
_userButton->update();
}
#endif
userLedHandler();
#ifdef PIN_BUZZER
@ -328,3 +305,48 @@ void UITask::loop() {
}
}
}
void UITask::handleButtonAnyPress() {
MESH_DEBUG_PRINTLN("UITask: any press triggered");
if (_display != NULL) {
if (!_display->isOn()) {
_display->turnOn();
_need_refresh = true;
} else {
// Turn on display
_display->turnOn();
_need_refresh = true;
}
_auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
}
}
void UITask::handleButtonShortPress() {
MESH_DEBUG_PRINTLN("UITask: short press triggered");
if (_display != NULL) {
if (_display->isOn()) {
// If display is on and showing message preview, clear it
if (_origin[0] && _msg[0]) {
clearMsgPreview();
}
}
}
}
void UITask::handleButtonDoublePress() {
MESH_DEBUG_PRINTLN("UITask: double press triggered");
// Not implemented. TODO: possibly send an advert here?
}
void UITask::handleButtonTriplePress() {
MESH_DEBUG_PRINTLN("UITask: triple press triggered");
// Toggle buzzer quiet mode
#ifdef PIN_BUZZER
buzzer.quiet(!buzzer.isQuiet());
#endif
}
void UITask::handleButtonLongPress() {
MESH_DEBUG_PRINTLN("UITask: long press triggered");
shutdown();
}