diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index bf94416a..4c99f992 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -253,6 +253,12 @@ void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& no file.read((uint8_t *)_prefs.ringtone_notes, sizeof(_prefs.ringtone_notes)); if (file.available()) { 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_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)); + } } } } @@ -312,6 +318,10 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_ file.write((uint8_t *)&_prefs.ringtone_len, sizeof(_prefs.ringtone_len)); 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_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.close(); } diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 3522accb..a195fe11 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -570,6 +570,32 @@ void MyMesh::onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packe } if (_ui) _ui->newMsg(path_len, channel_name, text, offline_queue_len, 0); #endif + + // auto-reply bot + if (_prefs.bot_enabled && _prefs.bot_trigger[0] && _prefs.bot_reply[0] && + channel_idx == _prefs.bot_channel_idx && + millis() - _bot_last_reply_ms > 10000UL) { + // case-insensitive substring match + const char* t = text; + const char* tr = _prefs.bot_trigger; + int tlen = strlen(tr); + bool matched = false; + for (; *t && !matched; t++) { + int i = 0; + while (i < tlen && tolower((uint8_t)t[i]) == tolower((uint8_t)tr[i])) i++; + if (i == tlen) matched = true; + } + if (matched) { + ChannelDetails ch; + if (getChannel(channel_idx, ch)) { + uint32_t ts = getRTCClock()->getCurrentTime(); + int rlen = strlen(_prefs.bot_reply); + if (sendGroupMessage(ts, ch.channel, _prefs.node_name, _prefs.bot_reply, rlen)) { + _bot_last_reply_ms = millis(); + } + } + } + } } void MyMesh::onChannelDataRecv(const mesh::GroupChannel &channel, mesh::Packet *pkt, uint16_t data_type, @@ -868,6 +894,10 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe _prefs.ringtone_bpm_idx = 2; // 120 bpm default _prefs.ringtone_len = 0; // no custom ringtone by default _prefs.home_pages_mask = 0x01FF; // all pages visible + _prefs.bot_enabled = 0; + _prefs.bot_channel_idx = 0; + _prefs.bot_trigger[0] = '\0'; + _prefs.bot_reply[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/MyMesh.h b/examples/companion_radio/MyMesh.h index 90bc928f..46651b88 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -221,6 +221,7 @@ private: uint8_t *sign_data; uint32_t sign_data_len; unsigned long dirty_contacts_expiry; + unsigned long _bot_last_reply_ms; TransportKey send_scope; diff --git a/examples/companion_radio/NodePrefs.h b/examples/companion_radio/NodePrefs.h index 8a43b19f..57302120 100644 --- a/examples/companion_radio/NodePrefs.h +++ b/examples/companion_radio/NodePrefs.h @@ -49,4 +49,8 @@ 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 + char bot_trigger[64]; // trigger phrase (case-insensitive contains match) + char bot_reply[140]; // auto-reply text }; \ No newline at end of file diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 3aa097bb..b42248a4 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -2085,12 +2085,254 @@ const char* RingtoneEditorScreen::MENU_LABELS[RingtoneEditorScreen::M_COUNT] "Play/Stop", "Duration", "BPM+", "BPM-", "Insert", "Delete", "Save & Exit", "Discard" }; +// ── BotScreen ───────────────────────────────────────────────────────────────── +class BotScreen : public UIScreen { + UITask* _task; + NodePrefs* _prefs; + + static const int ITEM_COUNT = 4; + static const int ITEM_H = 11; + static const int START_Y = 12; + static const int VAL_X = 70; + + // keyboard state (reused for trigger and reply fields) + int _kb_field; // -1=off, 2=trigger, 3=reply + char _kb_buf[140]; + int _kb_len; + int _kb_maxlen; + int _kb_row, _kb_col; + bool _kb_caps; + + int _sel; + + static const char KB_CHARS[4][10]; + static const int KB_ROWS = 4, KB_COLS = 10; + + // channel count cache (refreshed on enter) + int _num_channels; + uint8_t _channel_indices[MAX_GROUP_CHANNELS]; + + void refreshChannels() { + _num_channels = 0; + ChannelDetails ch; + for (int i = 0; i < MAX_GROUP_CHANNELS; i++) { + if (the_mesh.getChannel(i, ch) && ch.name[0] != '\0') + _channel_indices[_num_channels++] = (uint8_t)i; + } + } + + // returns display index of current bot_channel_idx in _channel_indices + int currentChanDisplayIdx() const { + for (int i = 0; i < _num_channels; i++) + if (_channel_indices[i] == _prefs->bot_channel_idx) return i; + return 0; + } + +public: + BotScreen(UITask* task, NodePrefs* prefs) : _task(task), _prefs(prefs) {} + + void enter() { + _sel = 0; + _kb_field = -1; + refreshChannels(); + } + + int render(DisplayDriver& display) override { + display.setTextSize(1); + display.setColor(DisplayDriver::LIGHT); + + if (_kb_field >= 0) { + // keyboard mode + const char* label = (_kb_field == 2) ? "Trigger:" : "Reply:"; + char preview[24]; + const char* disp = _kb_buf; + int dlen = _kb_len; + if (dlen > 16) { disp = _kb_buf + (dlen - 16); dlen = 16; } + snprintf(preview, sizeof(preview), "%s %.*s_", label, dlen, disp); + display.setCursor(0, 0); + display.print(preview); + display.fillRect(0, 10, display.width(), 1); + + for (int row = 0; row < KB_ROWS; row++) { + int y = 14 + row * 11; + for (int col = 0; col < KB_COLS; col++) { + bool sel = (_kb_row == row && _kb_col == col); + char ch = KB_CHARS[row][col]; + if (_kb_caps && ch >= 'a' && ch <= 'z') ch -= 32; + char buf[2] = { ch == ' ' ? '_' : ch, '\0' }; + int cx = col * 12; + if (sel) { + display.fillRect(cx, y - 1, 11, 10); + display.setColor(DisplayDriver::DARK); + } else { + display.setColor(DisplayDriver::LIGHT); + } + display.setCursor(cx + 3, y); + display.print(buf); + display.setColor(DisplayDriver::LIGHT); + } + } + // special row + const char* spec[] = { "[^]", "[Sp]", "[Del]", "[OK]" }; + for (int i = 0; i < 4; i++) { + bool sel = (_kb_row == KB_ROWS && _kb_col == i); + bool active = (i == 0 && _kb_caps); + int sx = i * 32; + if (sel || active) { + display.fillRect(sx, 58, 31, 9); + display.setColor(DisplayDriver::DARK); + } else { + display.setColor(DisplayDriver::LIGHT); + } + display.setCursor(sx + 2, 59); + display.print(spec[i]); + display.setColor(DisplayDriver::LIGHT); + } + return 50; + } + + display.drawTextCentered(display.width() / 2, 0, "AUTO-REPLY BOT"); + display.fillRect(0, 10, display.width(), 1); + + static const char* labels[] = { "Enable", "Channel", "Trigger", "Reply" }; + for (int i = 0; i < ITEM_COUNT; i++) { + int y = START_Y + i * ITEM_H; + bool sel = (i == _sel); + if (sel) { + display.fillRect(0, y - 1, display.width(), ITEM_H); + display.setColor(DisplayDriver::DARK); + } + display.setCursor(2, y); + display.print(labels[i]); + display.setCursor(VAL_X, y); + + if (i == 0) { + display.print(_prefs->bot_enabled ? "ON" : "OFF"); + } else if (i == 1) { + if (_num_channels == 0) { + display.print("none"); + } else { + ChannelDetails ch; + 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("Ch?"); + } + } 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 { + const char* rp = _prefs->bot_reply; + 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; + } + + bool handleInput(char c) override { + bool up = (c == KEY_UP); + bool down = (c == KEY_DOWN); + bool left = (c == KEY_LEFT || c == KEY_PREV); + bool right = (c == KEY_RIGHT || c == KEY_NEXT); + bool enter = (c == KEY_ENTER); + bool cancel = (c == KEY_CANCEL || c == KEY_CONTEXT_MENU); + + if (_kb_field >= 0) { + // keyboard input + if (cancel) { _kb_field = -1; return true; } + if (up && _kb_row > 0) { _kb_row--; return true; } + if (down && _kb_row < KB_ROWS) { _kb_row++; return true; } + if (left && _kb_col > 0) { _kb_col--; return true; } + if (right && _kb_col < (_kb_row < KB_ROWS ? KB_COLS - 1 : 3)) { _kb_col++; return true; } + if (enter) { + if (_kb_row < KB_ROWS) { + // character press + if (_kb_len < _kb_maxlen) { + char ch = KB_CHARS[_kb_row][_kb_col]; + if (_kb_caps && ch >= 'a' && ch <= 'z') ch -= 32; + _kb_buf[_kb_len++] = ch; + _kb_buf[_kb_len] = '\0'; + } + } else { + // special row + switch (_kb_col) { + case 0: _kb_caps = !_kb_caps; break; + case 1: if (_kb_len < _kb_maxlen) { _kb_buf[_kb_len++] = ' '; _kb_buf[_kb_len] = '\0'; } break; + case 2: if (_kb_len > 0) { _kb_buf[--_kb_len] = '\0'; } break; + case 3: + // commit + if (_kb_field == 2) + strncpy(_prefs->bot_trigger, _kb_buf, sizeof(_prefs->bot_trigger) - 1); + else + strncpy(_prefs->bot_reply, _kb_buf, sizeof(_prefs->bot_reply) - 1); + _kb_field = -1; + break; + } + } + return true; + } + return true; + } + + if (cancel) { + the_mesh.savePrefs(); + _task->gotoToolsScreen(); + return true; + } + if (up && _sel > 0) { _sel--; return true; } + if (down && _sel < ITEM_COUNT - 1) { _sel++; return true; } + + if (_sel == 0 && (enter || left || right)) { + _prefs->bot_enabled ^= 1; + return true; + } + if (_sel == 1 && _num_channels > 0) { + int idx = currentChanDisplayIdx(); + if (right || enter) idx = (idx + 1) % _num_channels; + if (left) idx = (idx + _num_channels - 1) % _num_channels; + if (left || right || enter) { _prefs->bot_channel_idx = _channel_indices[idx]; return true; } + } + if ((_sel == 2 || _sel == 3) && enter) { + _kb_field = _sel; + _kb_row = 0; + _kb_col = 0; + _kb_caps = false; + if (_sel == 2) { + strncpy(_kb_buf, _prefs->bot_trigger, sizeof(_kb_buf) - 1); + _kb_maxlen = sizeof(_prefs->bot_trigger) - 1; + } else { + strncpy(_kb_buf, _prefs->bot_reply, sizeof(_kb_buf) - 1); + _kb_maxlen = sizeof(_prefs->bot_reply) - 1; + } + _kb_buf[sizeof(_kb_buf) - 1] = '\0'; + _kb_len = strlen(_kb_buf); + return true; + } + return false; + } +}; + +const char BotScreen::KB_CHARS[4][10] = { + {'a','b','c','d','e','f','g','h','i','j'}, + {'k','l','m','n','o','p','q','r','s','t'}, + {'u','v','w','x','y','z','.',' ','!','?'}, + {'1','2','3','4','5','6','7','8','9','0'}, +}; + // ── ToolsScreen ─────────────────────────────────────────────────────────────── class ToolsScreen : public UIScreen { UITask* _task; + int _sel; + + static const int ITEM_COUNT = 2; + static const char* ITEMS[ITEM_COUNT]; public: - ToolsScreen(UITask* task) : _task(task) {} + ToolsScreen(UITask* task) : _task(task), _sel(0) {} int render(DisplayDriver& display) override { display.setTextSize(1); @@ -2098,11 +2340,17 @@ public: display.drawTextCentered(display.width() / 2, 0, "TOOLS"); display.fillRect(0, 10, display.width(), 1); - display.fillRect(0, 12, display.width(), 11); - display.setColor(DisplayDriver::DARK); - display.setCursor(4, 13); - display.print("Ringtone Editor"); - display.setColor(DisplayDriver::LIGHT); + for (int i = 0; i < ITEM_COUNT; i++) { + int y = 14 + i * 12; + bool sel = (i == _sel); + if (sel) { + display.fillRect(0, y - 1, display.width(), 11); + display.setColor(DisplayDriver::DARK); + } + display.setCursor(4, y); + display.print(ITEMS[i]); + display.setColor(DisplayDriver::LIGHT); + } display.setCursor(0, 54); display.print("ENT:open CANCEL:back"); @@ -2110,11 +2358,17 @@ public: } bool handleInput(char c) override { + if (c == KEY_UP && _sel > 0) { _sel--; return true; } + if (c == KEY_DOWN && _sel < ITEM_COUNT - 1) { _sel++; return true; } if (c == KEY_CANCEL || c == KEY_CONTEXT_MENU) { _task->gotoHomeScreen(); return true; } - if (c == KEY_ENTER) { _task->gotoRingtoneEditor(); return true; } + if (c == KEY_ENTER) { + if (_sel == 0) { _task->gotoRingtoneEditor(); return true; } + if (_sel == 1) { _task->gotoBotScreen(); return true; } + } return false; } }; +const char* ToolsScreen::ITEMS[2] = { "Ringtone Editor", "Auto-Reply Bot" }; // ── HomeScreen ──────────────────────────────────────────────────────────────── class HomeScreen : public UIScreen { @@ -2685,6 +2939,7 @@ void UITask::begin(DisplayDriver* display, SensorManager* sensors, NodePrefs* no quick_msg = new QuickMsgScreen(this); tools_screen = new ToolsScreen(this); ringtone_edit = new RingtoneEditorScreen(this, node_prefs); + bot_screen = new BotScreen(this, node_prefs); setCurrScreen(splash); applyBrightness(); @@ -2704,6 +2959,11 @@ void UITask::gotoRingtoneEditor() { setCurrScreen(ringtone_edit); } +void UITask::gotoBotScreen() { + ((BotScreen*)bot_screen)->enter(); + setCurrScreen(bot_screen); +} + void UITask::playMelody(const char* melody) { #ifdef PIN_BUZZER buzzer.playForced(melody); diff --git a/examples/companion_radio/ui-new/UITask.h b/examples/companion_radio/ui-new/UITask.h index db689ce5..48d1c030 100644 --- a/examples/companion_radio/ui-new/UITask.h +++ b/examples/companion_radio/ui-new/UITask.h @@ -57,6 +57,7 @@ class UITask : public AbstractUITask { UIScreen* quick_msg; UIScreen* tools_screen; UIScreen* ringtone_edit; + UIScreen* bot_screen; UIScreen* curr; void userLedHandler(); @@ -88,6 +89,7 @@ public: void gotoQuickMsgScreen(); void gotoToolsScreen(); void gotoRingtoneEditor(); + void gotoBotScreen(); void playMelody(const char* melody); void stopMelody(); bool isMelodyPlaying();