From fea1be8e81f45f03974d47379ddc6355b98314e9 Mon Sep 17 00:00:00 2001 From: Jakub <106778416+MarekZegare4@users.noreply.github.com> Date: Sat, 23 May 2026 15:20:40 +0200 Subject: [PATCH] feat(ui): runtime joystick rotation independent of display rotation Add NodePrefs::joystick_rotation (0-3 steps CW), persisted in DataStore alongside display_rotation. rotateJoystickKey() now takes a runtime rot parameter instead of compile-time JOYSTICK_ROTATION macro. UITask::loop() reads joystick_rotation from NodePrefs each frame. Settings screen exposes "Joystick" rotation item on any build with UI_HAS_JOYSTICK. JOYSTICK_ROTATION macro still works as compile-time default for legacy/non-prefs builds. Co-Authored-By: Claude Sonnet 4.6 --- examples/companion_radio/DataStore.cpp | 14 +++++++++ examples/companion_radio/NodePrefs.h | 1 + .../companion_radio/ui-new/SettingsScreen.h | 18 +++++++++++ examples/companion_radio/ui-new/UITask.cpp | 13 ++++---- src/helpers/ui/UIScreen.h | 30 +++++++++++++++++++ 5 files changed, 70 insertions(+), 6 deletions(-) diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index 78722ee9..b3792f51 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -293,16 +293,29 @@ void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& no file.read((uint8_t *)&_prefs.display_rotation, sizeof(_prefs.display_rotation)); if (file.available()) { file.read((uint8_t *)_prefs.page_order, sizeof(_prefs.page_order)); + if (file.available()) { + file.read((uint8_t *)&_prefs.joystick_rotation, sizeof(_prefs.joystick_rotation)); + } else { +#ifdef JOYSTICK_ROTATION + _prefs.joystick_rotation = JOYSTICK_ROTATION; +#endif + } } } else { #ifdef DISPLAY_ROTATION _prefs.display_rotation = DISPLAY_ROTATION; +#endif +#ifdef JOYSTICK_ROTATION + _prefs.joystick_rotation = JOYSTICK_ROTATION; #endif } } } else { #ifdef DISPLAY_ROTATION _prefs.display_rotation = DISPLAY_ROTATION; +#endif +#ifdef JOYSTICK_ROTATION + _prefs.joystick_rotation = JOYSTICK_ROTATION; #endif } } @@ -399,6 +412,7 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_ file.write((uint8_t *)&_prefs.use_lemon_font, sizeof(_prefs.use_lemon_font)); file.write((uint8_t *)&_prefs.display_rotation, sizeof(_prefs.display_rotation)); file.write((uint8_t *)_prefs.page_order, sizeof(_prefs.page_order)); + file.write((uint8_t *)&_prefs.joystick_rotation, sizeof(_prefs.joystick_rotation)); file.close(); } diff --git a/examples/companion_radio/NodePrefs.h b/examples/companion_radio/NodePrefs.h index 8b973727..b7c9a2eb 100644 --- a/examples/companion_radio/NodePrefs.h +++ b/examples/companion_radio/NodePrefs.h @@ -83,4 +83,5 @@ struct NodePrefs { // persisted to file // Home screen page order: each byte = bit-index+1 (see HP_* bit positions + 9=Settings, 10=Messages). // 0 = end of list (also uninitialized legacy). hasCustomOrder iff page_order[0] in 1..11. uint8_t page_order[11]; + uint8_t joystick_rotation; // 0-3 steps CW; independent of display_rotation }; \ No newline at end of file diff --git a/examples/companion_radio/ui-new/SettingsScreen.h b/examples/companion_radio/ui-new/SettingsScreen.h index 0d977ef6..6e3ae321 100644 --- a/examples/companion_radio/ui-new/SettingsScreen.h +++ b/examples/companion_radio/ui-new/SettingsScreen.h @@ -23,6 +23,9 @@ class SettingsScreen : public UIScreen { FONT, #if defined(EINK_DISPLAY_MODEL) ROTATION, +#endif +#if defined(UI_HAS_JOYSTICK) + JOY_ROTATION, #endif // Sound section SECTION_SOUND, @@ -443,6 +446,14 @@ class SettingsScreen : public UIScreen { { static const char* ROT_LABELS[] = { "0 deg", "90 deg", "180 deg", "270 deg" }; uint8_t r = p ? (p->display_rotation & 3) : 0; display.print(ROT_LABELS[r]); } +#endif +#if defined(UI_HAS_JOYSTICK) + } else if (item == JOY_ROTATION) { + display.print("Joystick"); + display.setCursor(display.valCol(), y); + { static const char* ROT_LABELS[] = { "0 deg", "90 deg", "180 deg", "270 deg" }; + uint8_t r = p ? (p->joystick_rotation & 3) : 0; + display.print(ROT_LABELS[r]); } #endif } else if (item == DM_FILTER) { display.print("DM"); @@ -666,6 +677,13 @@ public: _dirty = true; return true; } +#endif +#if defined(UI_HAS_JOYSTICK) + if (_selected == JOY_ROTATION && p && (left || right || enter)) { + p->joystick_rotation = (p->joystick_rotation + (left ? 3 : 1)) & 3; + _dirty = true; + return true; + } #endif if (_selected == DM_FILTER && p && (left || right || enter)) { p->dm_show_all = p->dm_show_all ? 0 : 1; diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 2bef598a..837990f8 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -1294,6 +1294,7 @@ static void formatDashVal(uint8_t field, char* val, int val_len, uint16_t batt_m void UITask::loop() { char c = 0; #if UI_HAS_JOYSTICK + uint8_t joy_rot = _node_prefs ? _node_prefs->joystick_rotation : JOYSTICK_ROTATION; int ev = user_btn.check(); if (ev == BUTTON_EVENT_CLICK) { if (back_btn.isPressed()) { @@ -1328,24 +1329,24 @@ void UITask::loop() { #if UI_HAS_JOYSTICK_UPDOWN ev = joystick_up.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_UP); + c = checkDisplayOn(rotateJoystickKey(KEY_UP, joy_rot)); } ev = joystick_down.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_DOWN); + c = checkDisplayOn(rotateJoystickKey(KEY_DOWN, joy_rot)); } #endif ev = joystick_left.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_LEFT); + c = checkDisplayOn(rotateJoystickKey(KEY_LEFT, joy_rot)); } else if (ev == BUTTON_EVENT_LONG_PRESS) { - c = handleLongPress(KEY_LEFT); + c = handleLongPress(rotateJoystickKey(KEY_LEFT, joy_rot)); } ev = joystick_right.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_RIGHT); + c = checkDisplayOn(rotateJoystickKey(KEY_RIGHT, joy_rot)); } else if (ev == BUTTON_EVENT_LONG_PRESS) { - c = handleLongPress(KEY_RIGHT); + c = handleLongPress(rotateJoystickKey(KEY_RIGHT, joy_rot)); } if (_lock_seq_used && millis() - _lock_seq_ms > 5000) { _lock_seq_used = false; // safety reset if Back release event was missed diff --git a/src/helpers/ui/UIScreen.h b/src/helpers/ui/UIScreen.h index 6aa1d69c..d78c9124 100644 --- a/src/helpers/ui/UIScreen.h +++ b/src/helpers/ui/UIScreen.h @@ -14,6 +14,36 @@ #define KEY_PREV 0xF2 #define KEY_CONTEXT_MENU 0xF3 +#ifndef JOYSTICK_ROTATION + #define JOYSTICK_ROTATION 0 +#endif + +// Rotate directional key by rot steps clockwise (0–3). +// Pass NodePrefs::joystick_rotation at runtime; JOYSTICK_ROTATION macro is the compile-time default. +static inline char rotateJoystickKey(char key, uint8_t rot) { + switch (rot & 3) { + case 1: + if ((uint8_t)key == KEY_UP) return KEY_RIGHT; + if ((uint8_t)key == KEY_RIGHT) return KEY_DOWN; + if ((uint8_t)key == KEY_DOWN) return KEY_LEFT; + if ((uint8_t)key == KEY_LEFT) return KEY_UP; + break; + case 2: + if ((uint8_t)key == KEY_UP) return KEY_DOWN; + if ((uint8_t)key == KEY_DOWN) return KEY_UP; + if ((uint8_t)key == KEY_LEFT) return KEY_RIGHT; + if ((uint8_t)key == KEY_RIGHT) return KEY_LEFT; + break; + case 3: + if ((uint8_t)key == KEY_UP) return KEY_LEFT; + if ((uint8_t)key == KEY_LEFT) return KEY_DOWN; + if ((uint8_t)key == KEY_DOWN) return KEY_RIGHT; + if ((uint8_t)key == KEY_RIGHT) return KEY_UP; + break; + } + return key; +} + class UIScreen { protected: UIScreen() { }