diff --git a/src/helpers/ui/companion-new/UITask.cpp b/src/helpers/ui/companion-new/UITask.cpp index f99d8e7e..605bf1bf 100644 --- a/src/helpers/ui/companion-new/UITask.cpp +++ b/src/helpers/ui/companion-new/UITask.cpp @@ -211,6 +211,18 @@ class HomeScreen : public UIScreen { display.drawXbm(iconX - (charging ? 18 : 9), iconY + 1, muted_icon, 8, 8); } #endif + +#if UI_HAS_JOYSTICK + // show lock icon left of the other icons when buttons are locked + if (_task->isLocked()) { + int x = iconX - (charging ? 18 : 9); + #ifdef PIN_BUZZER + if (_task->isBuzzerQuiet()) x -= 9; + #endif + display.setColor(UIColor::title_txt); + display.drawXbm(x, iconY + 1, lock_icon, 8, 8); + } +#endif } CayenneLPP sensors_lpp; @@ -1003,25 +1015,39 @@ void UITask::loop() { char c = 0; #if UI_HAS_JOYSTICK int ev = user_btn.check(); + if (_locked) ev = handleLocked(ev, KEY_ENTER); if (ev == BUTTON_EVENT_CLICK) { c = checkDisplayOn(KEY_ENTER); } else if (ev == BUTTON_EVENT_LONG_PRESS) { c = handleLongPress(KEY_ENTER); // REVISIT: could be mapped to different key code } ev = joystick_left.check(); + if (_locked) ev = handleLocked(ev, KEY_LEFT); if (ev == BUTTON_EVENT_CLICK) { c = checkDisplayOn(KEY_LEFT); } else if (ev == BUTTON_EVENT_LONG_PRESS) { c = handleLongPress(KEY_LEFT); } ev = joystick_right.check(); + if (_locked) ev = handleLocked(ev, KEY_RIGHT); if (ev == BUTTON_EVENT_CLICK) { c = checkDisplayOn(KEY_RIGHT); } else if (ev == BUTTON_EVENT_LONG_PRESS) { c = handleLongPress(KEY_RIGHT); } ev = back_btn.check(); - if (ev == BUTTON_EVENT_TRIPLE_CLICK) { + if (_locked && ev != BUTTON_EVENT_NONE + && ev != BUTTON_EVENT_TRIPLE_CLICK) { // buzzer toggle still works while locked + checkDisplayOn(0); + if (ev == BUTTON_EVENT_DOUBLE_CLICK) { // unlock needs a joystick press within 3 sec + _unlock_until = millis() + 3000; + showAlert("Now press joystick", 3000); + } else { + showAlert("Locked: Menu x2", 1200); + } + } else if (ev == BUTTON_EVENT_DOUBLE_CLICK) { + lockButtons(true); + } else if (ev == BUTTON_EVENT_TRIPLE_CLICK) { c = handleTripleClick(KEY_SELECT); } #elif defined(PIN_USER_BTN) @@ -1236,6 +1262,31 @@ char UITask::handleTripleClick(char c) { return c; } +#if UI_HAS_JOYSTICK +// buttons locked: ignore the joystick, except a press just after Menu double-click (unlock) +int UITask::handleLocked(int ev, char key) { + if (ev == BUTTON_EVENT_NONE) return ev; + if (ev == BUTTON_EVENT_LONG_PRESS && millis() - ui_started_at < 8000) return ev; // keep CLI/rescue + + if (_display != NULL && _display->isOn()) { // if screen is off, leave it off + if (key == KEY_ENTER && ev == BUTTON_EVENT_CLICK && millis() < _unlock_until) { + lockButtons(false); + } else { + checkDisplayOn(0); + showAlert("Locked: Menu x2", 1200); + } + } + return BUTTON_EVENT_NONE; +} + +void UITask::lockButtons(bool lock) { + _locked = lock; + _unlock_until = 0; + checkDisplayOn(0); + showAlert(lock ? "Keys locked" : "Unlocked", 800); +} +#endif + bool UITask::getGPSState() { if (_sensors != NULL) { int num = _sensors->getNumSettings(); diff --git a/src/helpers/ui/companion-new/UITask.h b/src/helpers/ui/companion-new/UITask.h index a0f2398e..09d2e598 100644 --- a/src/helpers/ui/companion-new/UITask.h +++ b/src/helpers/ui/companion-new/UITask.h @@ -75,6 +75,10 @@ class UITask : public AbstractUITask { #ifdef PIN_USER_BTN_ANA unsigned long _analogue_pin_read_millis = millis(); #endif +#if UI_HAS_JOYSTICK + bool _locked = false; + unsigned long _unlock_until = 0; +#endif UIScreen* splash; UIScreen* home; @@ -88,6 +92,10 @@ class UITask : public AbstractUITask { char handleLongPress(char c); char handleDoubleClick(char c); char handleTripleClick(char c); +#if UI_HAS_JOYSTICK + int handleLocked(int ev, char key); + void lockButtons(bool lock); +#endif void setCurrScreen(UIScreen* c); @@ -106,6 +114,9 @@ public: int getMsgCount() const { return _msgcount; } bool hasDisplay() const { return _display != NULL; } bool isButtonPressed() const; +#if UI_HAS_JOYSTICK + bool isLocked() const { return _locked; } +#endif BaseChatMesh* getMesh() const { return the_mesh; } uint32_t getBLEPin() const { return _ble_pin; } diff --git a/src/helpers/ui/companion-new/icons.h b/src/helpers/ui/companion-new/icons.h index 8d9ab175..3ee282c1 100644 --- a/src/helpers/ui/companion-new/icons.h +++ b/src/helpers/ui/companion-new/icons.h @@ -121,6 +121,11 @@ static const uint8_t muted_icon[] = { 0x20, 0x6a, 0xea, 0xe4, 0xe4, 0xea, 0x6a, 0x20 }; +// small padlock, 8x8px, shown while the buttons are locked +static const uint8_t lock_icon[] = { + 0x3c, 0x42, 0x42, 0xff, 0xff, 0xe7, 0xff, 0xff +}; + // small lightning bolt, 8x8px, shown next to the battery icon while charging static const uint8_t charging_icon[] = { 0x18, 0x30, 0x60, 0xFC, 0x18, 0x30, 0x60, 0xC0