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 <noreply@anthropic.com>
This commit is contained in:
Jakub
2026-05-23 15:20:40 +02:00
parent 85cfb6f1af
commit fea1be8e81
5 changed files with 70 additions and 6 deletions

View File

@@ -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();
}

View File

@@ -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
};

View File

@@ -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;

View File

@@ -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

View File

@@ -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 (03).
// 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() { }