From 71635e5e6f1a62191bbed407d885de2f2aa39669 Mon Sep 17 00:00:00 2001 From: Jakub <106778416+MarekZegare4@users.noreply.github.com> Date: Tue, 12 May 2026 20:12:54 +0200 Subject: [PATCH] Bot: DM+channel modes with separate replies; buzzer auto-mute; UX fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Bot redesign: DM bot active for all private messages (no pubkey filter), channel bot optional with separate enable flag; both can run simultaneously - Bot: one shared trigger, separate reply texts for DM and channel - Buzzer: add Auto mode (mutes when BT connected, unmutes on disconnect); per-channel force-on overrides still work as intended - Settings: defer flash writes to menu exit (only when dirty) - Settings: add Seconds toggle (hide clock seconds, refresh drops 1s→60s) - Fullscreen msg preview: fix truncation (fmsg buf 79→140 chars) - RingtoneEditor: fix melody playback cut after first note (RTTTL lib holds pointer — moved play buffer to member variable) - BotScreen: remove key hint footer - .gitattributes: protect README.md from upstream merge conflicts Co-Authored-By: Claude Sonnet 4.6 --- .gitattributes | 1 + examples/companion_radio/DataStore.cpp | 18 ++- examples/companion_radio/MyMesh.cpp | 6 +- examples/companion_radio/MyMeshBot.h | 16 +- examples/companion_radio/NodePrefs.h | 14 +- examples/companion_radio/ui-new/BotScreen.h | 139 +++++++----------- .../ui-new/RingtoneEditorScreen.h | 25 ++-- examples/companion_radio/ui-new/UITask.cpp | 72 +++++++-- examples/companion_radio/ui-new/UITask.h | 2 + 9 files changed, 154 insertions(+), 139 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..b0ff8112 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +README.md merge=ours diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index 9832de42..2d719396 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -255,12 +255,16 @@ void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& no file.read((uint8_t *)&_prefs.home_pages_mask, sizeof(_prefs.home_pages_mask)); if (file.available()) { file.read((uint8_t *)&_prefs.bot_enabled, sizeof(_prefs.bot_enabled)); + file.read((uint8_t *)&_prefs.bot_channel_enabled, sizeof(_prefs.bot_channel_enabled)); file.read((uint8_t *)&_prefs.bot_channel_idx, sizeof(_prefs.bot_channel_idx)); file.read((uint8_t *)_prefs.bot_trigger, sizeof(_prefs.bot_trigger)); - file.read((uint8_t *)_prefs.bot_reply, sizeof(_prefs.bot_reply)); + file.read((uint8_t *)_prefs.bot_reply_dm, sizeof(_prefs.bot_reply_dm)); + file.read((uint8_t *)_prefs.bot_reply_ch, sizeof(_prefs.bot_reply_ch)); if (file.available()) { - file.read((uint8_t *)&_prefs.bot_target_type, sizeof(_prefs.bot_target_type)); - file.read((uint8_t *)_prefs.bot_dm_pubkey, sizeof(_prefs.bot_dm_pubkey)); + file.read((uint8_t *)&_prefs.clock_hide_seconds, sizeof(_prefs.clock_hide_seconds)); + if (file.available()) { + file.read((uint8_t *)&_prefs.buzzer_auto, sizeof(_prefs.buzzer_auto)); + } } } } @@ -323,11 +327,13 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_ file.write((uint8_t *)_prefs.ringtone_notes, sizeof(_prefs.ringtone_notes)); file.write((uint8_t *)&_prefs.home_pages_mask, sizeof(_prefs.home_pages_mask)); file.write((uint8_t *)&_prefs.bot_enabled, sizeof(_prefs.bot_enabled)); + file.write((uint8_t *)&_prefs.bot_channel_enabled, sizeof(_prefs.bot_channel_enabled)); file.write((uint8_t *)&_prefs.bot_channel_idx, sizeof(_prefs.bot_channel_idx)); file.write((uint8_t *)_prefs.bot_trigger, sizeof(_prefs.bot_trigger)); - file.write((uint8_t *)_prefs.bot_reply, sizeof(_prefs.bot_reply)); - file.write((uint8_t *)&_prefs.bot_target_type, sizeof(_prefs.bot_target_type)); - file.write((uint8_t *)_prefs.bot_dm_pubkey, sizeof(_prefs.bot_dm_pubkey)); + file.write((uint8_t *)_prefs.bot_reply_dm, sizeof(_prefs.bot_reply_dm)); + file.write((uint8_t *)_prefs.bot_reply_ch, sizeof(_prefs.bot_reply_ch)); + file.write((uint8_t *)&_prefs.clock_hide_seconds, sizeof(_prefs.clock_hide_seconds)); + file.write((uint8_t *)&_prefs.buzzer_auto, sizeof(_prefs.buzzer_auto)); file.close(); } diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index e42afa08..4d8669fc 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -876,11 +876,11 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe _prefs.ringtone_len = 0; // no custom ringtone by default _prefs.home_pages_mask = 0x01FF; // all pages visible _prefs.bot_enabled = 0; + _prefs.bot_channel_enabled = 0; _prefs.bot_channel_idx = 0; _prefs.bot_trigger[0] = '\0'; - _prefs.bot_reply[0] = '\0'; - _prefs.bot_target_type = 0; - memset(_prefs.bot_dm_pubkey, 0, sizeof(_prefs.bot_dm_pubkey)); + _prefs.bot_reply_dm[0] = '\0'; + _prefs.bot_reply_ch[0] = '\0'; _prefs.auto_off_secs = 15; // 15 seconds auto-off by default _prefs.tz_offset_hours = 0; // UTC by default _prefs.low_batt_mv = 3400; // auto-shutdown at 3.4V by default diff --git a/examples/companion_radio/MyMeshBot.h b/examples/companion_radio/MyMeshBot.h index 728e9882..a413f76a 100644 --- a/examples/companion_radio/MyMeshBot.h +++ b/examples/companion_radio/MyMeshBot.h @@ -3,17 +3,10 @@ // Included at the bottom of MyMesh.cpp after all class definitions. void MyMesh::tryBotReplyDM(const ContactInfo& from, const char* text) { - if (!(_prefs.bot_enabled && _prefs.bot_target_type == 1 && - _prefs.bot_trigger[0] && _prefs.bot_reply[0] && + if (!(_prefs.bot_enabled && _prefs.bot_trigger[0] && _prefs.bot_reply_dm[0] && millis() - _bot_last_reply_ms > 10000UL)) return; - // all-zero pubkey = any sender; otherwise match first 4 bytes - bool key_ok = (_prefs.bot_dm_pubkey[0] == 0 && _prefs.bot_dm_pubkey[1] == 0 && - _prefs.bot_dm_pubkey[2] == 0 && _prefs.bot_dm_pubkey[3] == 0) || - memcmp(from.id.pub_key, _prefs.bot_dm_pubkey, 4) == 0; - if (!key_ok) return; - const char* tr = _prefs.bot_trigger; int tlen = strlen(tr); bool matched = false; @@ -26,7 +19,7 @@ void MyMesh::tryBotReplyDM(const ContactInfo& from, const char* text) { uint32_t ts = getRTCClock()->getCurrentTime(); char expanded[200]; - expandMsg(_prefs.bot_reply, expanded, sizeof(expanded), + expandMsg(_prefs.bot_reply_dm, expanded, sizeof(expanded), sensors.node_lat, sensors.node_lon, sensors.node_lat != 0.0 || sensors.node_lon != 0.0, ts, _prefs.tz_offset_hours); @@ -36,8 +29,7 @@ void MyMesh::tryBotReplyDM(const ContactInfo& from, const char* text) { } void MyMesh::tryBotReplyChannel(uint8_t channel_idx, const char* text) { - if (!(_prefs.bot_enabled && _prefs.bot_target_type == 0 && - _prefs.bot_trigger[0] && _prefs.bot_reply[0] && + if (!(_prefs.bot_channel_enabled && _prefs.bot_trigger[0] && _prefs.bot_reply_ch[0] && channel_idx == _prefs.bot_channel_idx && millis() - _bot_last_reply_ms > 10000UL)) return; @@ -57,7 +49,7 @@ void MyMesh::tryBotReplyChannel(uint8_t channel_idx, const char* text) { uint32_t ts = getRTCClock()->getCurrentTime(); char expanded[200]; - expandMsg(_prefs.bot_reply, expanded, sizeof(expanded), + expandMsg(_prefs.bot_reply_ch, expanded, sizeof(expanded), sensors.node_lat, sensors.node_lon, sensors.node_lat != 0.0 || sensors.node_lon != 0.0, ts, _prefs.tz_offset_hours); diff --git a/examples/companion_radio/NodePrefs.h b/examples/companion_radio/NodePrefs.h index 68bf8dc6..4a8d0bcb 100644 --- a/examples/companion_radio/NodePrefs.h +++ b/examples/companion_radio/NodePrefs.h @@ -49,10 +49,12 @@ struct NodePrefs { // persisted to file uint8_t ringtone_len; // number of notes in custom ringtone (0 = use default) uint8_t ringtone_notes[32]; // packed: bits0-2=pitch, bits3-4=octave-4, bits5-6=dur_idx uint16_t home_pages_mask; // bitmask of visible home pages (bit0=Clock..bit8=Shutdown); 0=all visible - uint8_t bot_enabled; // 0=disabled, 1=enabled - uint8_t bot_channel_idx; // channel index to monitor (when bot_target_type==0) - char bot_trigger[64]; // trigger phrase (case-insensitive contains match) - char bot_reply[140]; // auto-reply text - uint8_t bot_target_type; // 0=channel (default), 1=DM contact - uint8_t bot_dm_pubkey[4]; // pubkey prefix of DM target; all-zero = any DM sender + uint8_t bot_enabled; // 0=disabled, 1=DM bot active (responds to all DMs) + uint8_t bot_channel_enabled; // 0=disabled, 1=channel bot active for bot_channel_idx + uint8_t bot_channel_idx; // channel index for channel bot + char bot_trigger[64]; // trigger phrase (case-insensitive contains match) + char bot_reply_dm[140]; // auto-reply text for DM + char bot_reply_ch[140]; // auto-reply text for channel + uint8_t clock_hide_seconds; // 0=show HH:MM:SS/refresh 1s (default), 1=hide/refresh 60s + uint8_t buzzer_auto; // 0=manual (default), 1=auto-mute when BT connected }; \ No newline at end of file diff --git a/examples/companion_radio/ui-new/BotScreen.h b/examples/companion_radio/ui-new/BotScreen.h index 6c833f9b..1603ac48 100644 --- a/examples/companion_radio/ui-new/BotScreen.h +++ b/examples/companion_radio/ui-new/BotScreen.h @@ -6,11 +6,14 @@ class BotScreen : public UIScreen { UITask* _task; NodePrefs* _prefs; - static const int ITEM_COUNT = 4; + // Items: 0=Enable(DM), 1=Channel, 2=Trigger, 3=Reply DM, 4=Reply Ch + static const int ITEM_COUNT = 5; static const int ITEM_H = 11; static const int START_Y = 12; static const int VAL_X = 70; + int _sel; + // keyboard state (reused for trigger and reply fields) int _kb_field; // -1=off, 2=trigger, 3=reply char _kb_buf[KB_MAX_LEN + 1]; @@ -21,18 +24,10 @@ class BotScreen : public UIScreen { bool _kb_ph_mode; int _kb_ph_sel; - int _sel; - - // channel + DM contact caches (refreshed on enter) - static const int MAX_BOT_DM = 16; - + // channel cache (refreshed on enter) int _num_channels; uint8_t _channel_indices[MAX_GROUP_CHANNELS]; - int _num_dm; - uint8_t _dm_pubkeys[MAX_BOT_DM][4]; - char _dm_names[MAX_BOT_DM][32]; - void refreshChannels() { _num_channels = 0; ChannelDetails ch; @@ -42,33 +37,12 @@ class BotScreen : public UIScreen { } } - void refreshContacts() { - _num_dm = 0; - ContactInfo ci; - int n = the_mesh.getNumContacts(); - for (int i = 0; i < n && _num_dm < MAX_BOT_DM; i++) { - if (!the_mesh.getContactByIdx(i, ci)) continue; - if (ci.type != ADV_TYPE_CHAT) continue; - if (!(ci.flags & 0x01)) continue; // favourites only - memcpy(_dm_pubkeys[_num_dm], ci.id.pub_key, 4); - strncpy(_dm_names[_num_dm], ci.name, sizeof(_dm_names[0]) - 1); - _dm_names[_num_dm][sizeof(_dm_names[0]) - 1] = '\0'; - _num_dm++; - } - } - - // returns combined index in [channels..., DM contacts...] - int currentTargetIdx() const { - if (_prefs->bot_target_type == 0) { - for (int i = 0; i < _num_channels; i++) - if (_channel_indices[i] == _prefs->bot_channel_idx) return i; - return 0; - } else { - for (int i = 0; i < _num_dm; i++) - if (memcmp(_dm_pubkeys[i], _prefs->bot_dm_pubkey, 4) == 0) - return _num_channels + i; - return _num_channels; // fallback: first DM - } + // Returns index into _channel_indices[] for current bot_channel_idx, or -1 if not found/disabled. + int currentChannelListIdx() const { + if (!_prefs->bot_channel_enabled) return -1; + for (int i = 0; i < _num_channels; i++) + if (_channel_indices[i] == _prefs->bot_channel_idx) return i; + return -1; } public: @@ -78,7 +52,6 @@ public: _sel = 0; _kb_field = -1; refreshChannels(); - refreshContacts(); } int render(DisplayDriver& display) override { @@ -86,7 +59,6 @@ public: display.setColor(DisplayDriver::LIGHT); if (_kb_field >= 0) { - // keyboard mode — uses same layout as global keyboard const char* label = (_kb_field == 2) ? "Trigger:" : "Reply:"; const char* disp_start = _kb_buf; int disp_len = _kb_len; @@ -97,7 +69,6 @@ public: display.print(preview); display.fillRect(0, KB_SEP_Y, display.width(), 1); - // char rows for (int row = 0; row < KB_ROWS_CHAR; row++) { int y = KB_CHARS_Y + row * KB_CELL_H; for (int col = 0; col < KB_COLS_CHAR; col++) { @@ -118,7 +89,6 @@ public: } } - // special row: [^] [Sp] [Del] [{}] [OK] — 5 buttons at 25px each const char* spec[] = { "[^]", "[Sp]", "[Del]", "[{}]", "[OK]" }; for (int i = 0; i < KB_SPECIAL; i++) { bool sel = (_kb_row == KB_ROWS_CHAR && _kb_col == i); @@ -136,7 +106,6 @@ public: display.setColor(DisplayDriver::LIGHT); } - // placeholder picker overlay if (_kb_ph_mode) { display.setColor(DisplayDriver::DARK); display.fillRect(20, 20, 88, 12 + KB_PH_COUNT * 10); @@ -165,7 +134,7 @@ public: display.drawTextCentered(display.width() / 2, 0, "AUTO-REPLY BOT"); display.fillRect(0, 10, display.width(), 1); - static const char* labels[] = { "Enable", NULL, "Trigger", "Reply" }; + static const char* labels[] = { "Enable", "Channel", "Trigger", "Reply DM", "Reply Ch" }; for (int i = 0; i < ITEM_COUNT; i++) { int y = START_Y + i * ITEM_H; bool sel = (i == _sel); @@ -174,46 +143,33 @@ public: display.setColor(DisplayDriver::DARK); } display.setCursor(2, y); - if (i == 1) - display.print(_prefs->bot_target_type == 0 ? "Channel" : "Contact"); - else - display.print(labels[i]); + display.print(labels[i]); display.setCursor(VAL_X, y); if (i == 0) { display.print(_prefs->bot_enabled ? "ON" : "OFF"); } else if (i == 1) { - int total = _num_channels + _num_dm; - if (total == 0) { - display.print("none"); - } else if (_prefs->bot_target_type == 0) { + if (!_prefs->bot_channel_enabled || _num_channels == 0) { + display.print("OFF"); + } else { ChannelDetails ch; - if (_num_channels > 0 && the_mesh.getChannel(_prefs->bot_channel_idx, ch) && ch.name[0]) + if (the_mesh.getChannel(_prefs->bot_channel_idx, ch) && ch.name[0]) display.drawTextEllipsized(VAL_X, y, display.width() - VAL_X - 1, ch.name); else display.print("?"); - } else { - bool found = false; - for (int j = 0; j < _num_dm && !found; j++) { - if (memcmp(_dm_pubkeys[j], _prefs->bot_dm_pubkey, 4) == 0) { - display.drawTextEllipsized(VAL_X, y, display.width() - VAL_X - 1, _dm_names[j]); - found = true; - } - } - if (!found) display.print("?"); } } else if (i == 2) { const char* tr = _prefs->bot_trigger; display.drawTextEllipsized(VAL_X, y, display.width() - VAL_X - 1, tr[0] ? tr : "(none)"); + } else if (i == 3) { + const char* rp = _prefs->bot_reply_dm; + display.drawTextEllipsized(VAL_X, y, display.width() - VAL_X - 1, rp[0] ? rp : "(none)"); } else { - const char* rp = _prefs->bot_reply; + const char* rp = _prefs->bot_reply_ch; display.drawTextEllipsized(VAL_X, y, display.width() - VAL_X - 1, rp[0] ? rp : "(none)"); } display.setColor(DisplayDriver::LIGHT); } - - display.setCursor(0, 54); - display.print("ENT:edit CANCEL:save"); return 300; } @@ -226,7 +182,6 @@ public: bool cancel = (c == KEY_CANCEL || c == KEY_CONTEXT_MENU); if (_kb_field >= 0) { - // placeholder picker overlay takes priority if (_kb_ph_mode) { if (c == KEY_UP && _kb_ph_sel > 0) { _kb_ph_sel--; return true; } if (c == KEY_DOWN && _kb_ph_sel < KB_PH_COUNT - 1) { _kb_ph_sel++; return true; } @@ -249,7 +204,7 @@ public: if (up) { if (_kb_row > 0) { _kb_row--; - if (_kb_row == KB_ROWS_CHAR - 1) // leaving special row upward + if (_kb_row == KB_ROWS_CHAR - 1) _kb_col = _kb_col * KB_COLS_CHAR / KB_SPECIAL; } return true; @@ -257,15 +212,12 @@ public: if (down) { if (_kb_row < KB_ROWS_CHAR) { _kb_row++; - if (_kb_row == KB_ROWS_CHAR) // entering special row + if (_kb_row == KB_ROWS_CHAR) _kb_col = _kb_col * KB_SPECIAL / KB_COLS_CHAR; } return true; } - if (left) { - if (_kb_col > 0) _kb_col--; - return true; - } + if (left) { if (_kb_col > 0) _kb_col--; return true; } if (right) { int max_col = (_kb_row == KB_ROWS_CHAR) ? KB_SPECIAL - 1 : KB_COLS_CHAR - 1; if (_kb_col < max_col) _kb_col++; @@ -293,13 +245,15 @@ public: _kb_ph_sel = 0; break; case 4: - // OK — commit to prefs if (_kb_field == 2) { strncpy(_prefs->bot_trigger, _kb_buf, sizeof(_prefs->bot_trigger) - 1); _prefs->bot_trigger[sizeof(_prefs->bot_trigger) - 1] = '\0'; + } else if (_kb_field == 3) { + strncpy(_prefs->bot_reply_dm, _kb_buf, sizeof(_prefs->bot_reply_dm) - 1); + _prefs->bot_reply_dm[sizeof(_prefs->bot_reply_dm) - 1] = '\0'; } else { - strncpy(_prefs->bot_reply, _kb_buf, sizeof(_prefs->bot_reply) - 1); - _prefs->bot_reply[sizeof(_prefs->bot_reply) - 1] = '\0'; + strncpy(_prefs->bot_reply_ch, _kb_buf, sizeof(_prefs->bot_reply_ch) - 1); + _prefs->bot_reply_ch[sizeof(_prefs->bot_reply_ch) - 1] = '\0'; } _kb_field = -1; break; @@ -323,24 +277,28 @@ public: return true; } if (_sel == 1) { - int total = _num_channels + _num_dm; - if (total == 0) return false; - int idx = currentTargetIdx(); - if (right || enter) idx = (idx + 1) % total; - if (left) idx = (idx + total - 1) % total; + if (_num_channels == 0) return false; + // Cycle: OFF → ch[0] → ch[1] → ... → OFF + int idx = currentChannelListIdx(); // -1 = OFF + if (right || enter) { + idx++; + if (idx >= _num_channels) idx = -1; // wrap to OFF + } + if (left) { + idx--; + if (idx < -1) idx = _num_channels - 1; + } if (left || right || enter) { - if (idx < _num_channels) { - _prefs->bot_target_type = 0; - _prefs->bot_channel_idx = _channel_indices[idx]; + if (idx < 0) { + _prefs->bot_channel_enabled = 0; } else { - int di = idx - _num_channels; - _prefs->bot_target_type = 1; - memcpy(_prefs->bot_dm_pubkey, _dm_pubkeys[di], 4); + _prefs->bot_channel_enabled = 1; + _prefs->bot_channel_idx = _channel_indices[idx]; } return true; } } - if ((_sel == 2 || _sel == 3) && enter) { + if ((_sel == 2 || _sel == 3 || _sel == 4) && enter) { _kb_field = _sel; _kb_row = 0; _kb_col = 0; @@ -350,9 +308,12 @@ public: if (_sel == 2) { strncpy(_kb_buf, _prefs->bot_trigger, sizeof(_kb_buf) - 1); _kb_maxlen = sizeof(_prefs->bot_trigger) - 1; + } else if (_sel == 3) { + strncpy(_kb_buf, _prefs->bot_reply_dm, sizeof(_kb_buf) - 1); + _kb_maxlen = sizeof(_prefs->bot_reply_dm) - 1; } else { - strncpy(_kb_buf, _prefs->bot_reply, sizeof(_kb_buf) - 1); - _kb_maxlen = sizeof(_prefs->bot_reply) - 1; + strncpy(_kb_buf, _prefs->bot_reply_ch, sizeof(_kb_buf) - 1); + _kb_maxlen = sizeof(_prefs->bot_reply_ch) - 1; } _kb_buf[sizeof(_kb_buf) - 1] = '\0'; _kb_len = strlen(_kb_buf); diff --git a/examples/companion_radio/ui-new/RingtoneEditorScreen.h b/examples/companion_radio/ui-new/RingtoneEditorScreen.h index 2704042f..ab2b9d79 100644 --- a/examples/companion_radio/ui-new/RingtoneEditorScreen.h +++ b/examples/companion_radio/ui-new/RingtoneEditorScreen.h @@ -26,6 +26,7 @@ class RingtoneEditorScreen : public UIScreen { bool _menu_open; int _menu_sel; int _menu_scroll; + char _play_buf[220]; // persistent RTTTL buffer — library holds a pointer into it enum MenuOpt { M_PLAY_STOP, M_DURATION, M_BPM_UP, M_BPM_DOWN, @@ -46,28 +47,27 @@ class RingtoneEditorScreen : public UIScreen { if (_scroll < 0) _scroll = 0; } - void buildRTTTL(char* buf, int buf_len) { + void buildRTTTL() { uint16_t bpm = BPM_OPTS[_bpm_idx < 5 ? _bpm_idx : 2]; - int pos = snprintf(buf, buf_len, "Ring:d=8,o=5,b=%u:", bpm); - for (int i = 0; i < _len && pos < buf_len - 8; i++) { - if (i > 0 && pos < buf_len - 1) buf[pos++] = ','; + int pos = snprintf(_play_buf, sizeof(_play_buf), "Ring:d=8,o=5,b=%u:", bpm); + for (int i = 0; i < _len && pos < (int)sizeof(_play_buf) - 8; i++) { + if (i > 0 && pos < (int)sizeof(_play_buf) - 1) _play_buf[pos++] = ','; uint8_t pitch = notePitch(_notes[i]); uint8_t octave = noteOctave(_notes[i]); uint8_t dur_val = DUR_VALS[noteDurIdx(_notes[i])]; if (pitch == 0) - pos += snprintf(buf + pos, buf_len - pos, "%dp", dur_val); + pos += snprintf(_play_buf + pos, sizeof(_play_buf) - pos, "%dp", dur_val); else - pos += snprintf(buf + pos, buf_len - pos, "%d%c%d", dur_val, PITCH_NAMES[pitch], octave); + pos += snprintf(_play_buf + pos, sizeof(_play_buf) - pos, "%d%c%d", dur_val, PITCH_NAMES[pitch], octave); } - if (pos < buf_len) buf[pos] = '\0'; + if (pos < (int)sizeof(_play_buf)) _play_buf[pos] = '\0'; } void previewNote(uint8_t note_byte) { uint8_t pitch = notePitch(note_byte); if (pitch == 0) { _task->stopMelody(); return; } - char buf[28]; - snprintf(buf, sizeof(buf), "P:d=16,o=5,b=240:%c%d", PITCH_NAMES[pitch], noteOctave(note_byte)); - _task->playMelody(buf); + snprintf(_play_buf, sizeof(_play_buf), "P:d=16,o=5,b=240:%c%d", PITCH_NAMES[pitch], noteOctave(note_byte)); + _task->playMelody(_play_buf); } public: @@ -211,9 +211,8 @@ public: if (_task->isMelodyPlaying()) { _task->stopMelody(); } else if (_len > 0) { - char buf[220]; - buildRTTTL(buf, sizeof(buf)); - _task->playMelody(buf); + buildRTTTL(); + _task->playMelody(_play_buf); } break; case M_DURATION: diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index ebfbdb0b..1a33d263 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -126,6 +126,7 @@ class SettingsScreen : public UIScreen { BRIGHTNESS, AUTO_OFF, BATT_DISPLAY, + CLOCK_SECONDS, // Sound section SECTION_SOUND, BUZZER, @@ -350,7 +351,9 @@ class SettingsScreen : public UIScreen { display.print("Buzzer"); display.setCursor(VAL_X, y); #ifdef PIN_BUZZER - display.print(_task->isBuzzerQuiet() ? "OFF" : "ON"); + { static const char* labels[] = { "ON", "OFF", "Auto" }; + int m = _task->getBuzzerMode(); + display.print(labels[m < 3 ? m : 0]); } #else display.print("N/A"); #endif @@ -399,6 +402,10 @@ class SettingsScreen : public UIScreen { display.setCursor(VAL_X, y); uint8_t mode = p ? p->batt_display_mode : 0; display.print(BATT_DISPLAY_LABELS[mode < BATT_DISPLAY_COUNT ? mode : 0]); + } else if (item == CLOCK_SECONDS) { + display.print("Seconds"); + display.setCursor(VAL_X, y); + display.print((p && p->clock_hide_seconds) ? "OFF" : "ON"); } else if (item == DM_FILTER) { display.print("DM"); display.setCursor(VAL_X, y); @@ -615,11 +622,11 @@ public: _edit_ph_mode = true; _edit_ph_sel = 0; } else { - // OK — save + // OK — commit to prefs, save deferred to settings exit if (p) { strncpy(p->custom_msgs[_edit_slot], _edit_buf, sizeof(p->custom_msgs[0]) - 1); p->custom_msgs[_edit_slot][sizeof(p->custom_msgs[0]) - 1] = '\0'; - the_mesh.savePrefs(); + _dirty = true; } _edit_slot = -1; } @@ -666,7 +673,8 @@ public: return right || left; } if (_selected == BUZZER && (left || right || enter)) { - _task->toggleBuzzer(); // saves immediately internally + _task->cycleBuzzerMode(); + _dirty = true; return true; } if (_selected == BUZZER_VOLUME) { @@ -721,6 +729,11 @@ public: if (left) idx = (idx + BATT_DISPLAY_COUNT - 1) % BATT_DISPLAY_COUNT; if (left || right) { p->batt_display_mode = idx; _dirty = true; return true; } } + if (_selected == CLOCK_SECONDS && p && (left || right || enter)) { + p->clock_hide_seconds ^= 1; + _dirty = true; + return true; + } if (_selected == DM_FILTER && p && (left || right || enter)) { p->dm_show_all = p->dm_show_all ? 0 : 1; _dirty = true; @@ -1237,7 +1250,7 @@ public: if (ring_pos >= 0) { const char* ftext = _hist[ring_pos].text; const char* fsep = strstr(ftext, ": "); - char fsender[22], fmsg[79]; + char fsender[22], fmsg[140]; if (fsep) { int nl = fsep - ftext; if (nl > 21) nl = 21; strncpy(fsender, ftext, nl); fsender[nl] = '\0'; @@ -1992,7 +2005,11 @@ public: char buf[24]; display.setColor(DisplayDriver::LIGHT); display.setTextSize(2); - sprintf(buf, "%02d:%02d:%02d", ti->tm_hour, ti->tm_min, ti->tm_sec); + bool show_sec = !_node_prefs || !_node_prefs->clock_hide_seconds; + if (show_sec) + sprintf(buf, "%02d:%02d:%02d", ti->tm_hour, ti->tm_min, ti->tm_sec); + else + sprintf(buf, "%02d:%02d", ti->tm_hour, ti->tm_min); display.drawTextCentered(display.width() / 2, 18, buf); display.setTextSize(1); @@ -2209,7 +2226,11 @@ public: display.drawTextCentered(display.width() / 2, 64 - 11, "hibernate:" PRESS_LABEL); } } - return (_page == HomePage::CLOCK) ? 1000 : 5000; + if (_page == HomePage::CLOCK) { + bool show_sec = !_node_prefs || !_node_prefs->clock_hide_seconds; + return show_sec ? 1000 : 60000; + } + return 5000; } bool handleInput(char c) override { @@ -2604,6 +2625,13 @@ void UITask::loop() { userLedHandler(); #ifdef PIN_BUZZER + if (_node_prefs && _node_prefs->buzzer_auto) { + bool should_quiet = hasConnection(); + if (buzzer.isQuiet() != should_quiet) { + buzzer.quiet(should_quiet); + _next_refresh = 0; + } + } if (buzzer.isPlaying()) buzzer.loop(); #endif @@ -2776,17 +2804,41 @@ void UITask::setBuzzerVolumeLevel(uint8_t level) { } void UITask::toggleBuzzer() { - // Toggle buzzer quiet mode #ifdef PIN_BUZZER + if (_node_prefs) _node_prefs->buzzer_auto = 0; // exit auto mode if (buzzer.isQuiet()) { buzzer.quiet(false); notify(UIEventType::ack); } else { buzzer.quiet(true); } - _node_prefs->buzzer_quiet = buzzer.isQuiet(); + if (_node_prefs) _node_prefs->buzzer_quiet = buzzer.isQuiet(); the_mesh.savePrefs(); showAlert(buzzer.isQuiet() ? "Buzzer: OFF" : "Buzzer: ON", 800); - _next_refresh = 0; // trigger refresh + _next_refresh = 0; #endif } + +int UITask::getBuzzerMode() { +#ifdef PIN_BUZZER + if (_node_prefs && _node_prefs->buzzer_auto) return 2; + return buzzer.isQuiet() ? 1 : 0; +#else + return 1; +#endif +} + +void UITask::cycleBuzzerMode() { +#ifdef PIN_BUZZER + if (!_node_prefs) return; + int mode = getBuzzerMode(); + mode = (mode + 1) % 3; // ON(0) → OFF(1) → Auto(2) → ON + _node_prefs->buzzer_auto = (mode == 2) ? 1 : 0; + if (mode == 0) { buzzer.quiet(false); _node_prefs->buzzer_quiet = 0; notify(UIEventType::ack); } + if (mode == 1) { buzzer.quiet(true); _node_prefs->buzzer_quiet = 1; } + if (mode == 2) { buzzer.quiet(hasConnection()); } + static const char* labels[] = { "Buzzer: ON", "Buzzer: OFF", "Buzzer: Auto" }; + showAlert(labels[mode], 800); + _next_refresh = 0; +#endif +} diff --git a/examples/companion_radio/ui-new/UITask.h b/examples/companion_radio/ui-new/UITask.h index 48d1c030..6b809c01 100644 --- a/examples/companion_radio/ui-new/UITask.h +++ b/examples/companion_radio/ui-new/UITask.h @@ -111,6 +111,8 @@ public: } void toggleBuzzer(); + void cycleBuzzerMode(); // ON → OFF → Auto → ON + int getBuzzerMode(); // 0=ON, 1=OFF, 2=Auto bool getGPSState(); void toggleGPS(); void applyBrightness();