UITask.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. #include "UITask.h"
  2. #include <helpers/TxtDataHelpers.h>
  3. #include "../MyMesh.h"
  4. #include "target.h"
  5. #define AUTO_OFF_MILLIS 15000 // 15 seconds
  6. #define BOOT_SCREEN_MILLIS 3000 // 3 seconds
  7. #ifdef PIN_STATUS_LED
  8. #define LED_ON_MILLIS 20
  9. #define LED_ON_MSG_MILLIS 200
  10. #define LED_CYCLE_MILLIS 4000
  11. #endif
  12. #define LONG_PRESS_MILLIS 1200
  13. #ifndef UI_RECENT_LIST_SIZE
  14. #define UI_RECENT_LIST_SIZE 4
  15. #endif
  16. #define PRESS_LABEL "long press"
  17. #include "icons.h"
  18. class SplashScreen : public UIScreen {
  19. UITask* _task;
  20. unsigned long dismiss_after;
  21. char _version_info[12];
  22. public:
  23. SplashScreen(UITask* task) : _task(task) {
  24. // strip off dash and commit hash by changing dash to null terminator
  25. // e.g: v1.2.3-abcdef -> v1.2.3
  26. const char *ver = FIRMWARE_VERSION;
  27. const char *dash = strchr(ver, '-');
  28. int len = dash ? dash - ver : strlen(ver);
  29. if (len >= sizeof(_version_info)) len = sizeof(_version_info) - 1;
  30. memcpy(_version_info, ver, len);
  31. _version_info[len] = 0;
  32. dismiss_after = millis() + BOOT_SCREEN_MILLIS;
  33. }
  34. int render(DisplayDriver& display) override {
  35. // meshcore logo
  36. display.setColor(DisplayDriver::BLUE);
  37. int logoWidth = 128;
  38. display.drawXbm((display.width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
  39. // version info
  40. display.setColor(DisplayDriver::LIGHT);
  41. display.setTextSize(2);
  42. display.drawTextCentered(display.width()/2, 22, _version_info);
  43. display.setTextSize(1);
  44. display.drawTextCentered(display.width()/2, 42, FIRMWARE_BUILD_DATE);
  45. return 1000;
  46. }
  47. void poll() override {
  48. if (millis() >= dismiss_after) {
  49. _task->gotoHomeScreen();
  50. }
  51. }
  52. };
  53. class HomeScreen : public UIScreen {
  54. enum HomePage {
  55. FIRST,
  56. RECENT,
  57. RADIO,
  58. BLUETOOTH,
  59. ADVERT,
  60. SHUTDOWN,
  61. Count // keep as last
  62. };
  63. UITask* _task;
  64. mesh::RTCClock* _rtc;
  65. SensorManager* _sensors;
  66. NodePrefs* _node_prefs;
  67. uint8_t _page;
  68. bool _shutdown_init;
  69. AdvertPath recent[UI_RECENT_LIST_SIZE];
  70. void renderBatteryIndicator(DisplayDriver& display, uint16_t batteryMilliVolts) {
  71. // Convert millivolts to percentage
  72. const int minMilliVolts = 3000; // Minimum voltage (e.g., 3.0V)
  73. const int maxMilliVolts = 4200; // Maximum voltage (e.g., 4.2V)
  74. int batteryPercentage = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts);
  75. if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
  76. if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
  77. // battery icon
  78. int iconWidth = 24;
  79. int iconHeight = 10;
  80. int iconX = display.width() - iconWidth - 5; // Position the icon near the top-right corner
  81. int iconY = 0;
  82. display.setColor(DisplayDriver::GREEN);
  83. // battery outline
  84. display.drawRect(iconX, iconY, iconWidth, iconHeight);
  85. // battery "cap"
  86. display.fillRect(iconX + iconWidth, iconY + (iconHeight / 4), 3, iconHeight / 2);
  87. // fill the battery based on the percentage
  88. int fillWidth = (batteryPercentage * (iconWidth - 4)) / 100;
  89. display.fillRect(iconX + 2, iconY + 2, fillWidth, iconHeight - 4);
  90. }
  91. public:
  92. HomeScreen(UITask* task, mesh::RTCClock* rtc, SensorManager* sensors, NodePrefs* node_prefs)
  93. : _task(task), _rtc(rtc), _sensors(sensors), _node_prefs(node_prefs), _page(0), _shutdown_init(false) { }
  94. void poll() override {
  95. if (_shutdown_init && !_task->isButtonPressed()) { // must wait for USR button to be released
  96. _task->shutdown();
  97. }
  98. }
  99. int render(DisplayDriver& display) override {
  100. char tmp[80];
  101. // node name
  102. display.setCursor(0, 0);
  103. display.setTextSize(1);
  104. display.setColor(DisplayDriver::GREEN);
  105. display.print(_node_prefs->node_name);
  106. // battery voltage
  107. renderBatteryIndicator(display, _task->getBattMilliVolts());
  108. // curr page indicator
  109. int y = 14;
  110. int x = display.width() / 2 - 25;
  111. for (uint8_t i = 0; i < HomePage::Count; i++, x += 10) {
  112. if (i == _page) {
  113. display.fillRect(x-1, y-1, 3, 3);
  114. } else {
  115. display.fillRect(x, y, 1, 1);
  116. }
  117. }
  118. if (_page == HomePage::FIRST) {
  119. display.setColor(DisplayDriver::YELLOW);
  120. display.setTextSize(2);
  121. sprintf(tmp, "MSG: %d", _task->getMsgCount());
  122. display.drawTextCentered(display.width() / 2, 20, tmp);
  123. if (_task->hasConnection()) {
  124. display.setColor(DisplayDriver::GREEN);
  125. display.setTextSize(1);
  126. display.drawTextCentered(display.width() / 2, 43, "< Connected >");
  127. } else if (the_mesh.getBLEPin() != 0) { // BT pin
  128. display.setColor(DisplayDriver::RED);
  129. display.setTextSize(2);
  130. sprintf(tmp, "Pin:%d", the_mesh.getBLEPin());
  131. display.drawTextCentered(display.width() / 2, 43, tmp);
  132. }
  133. } else if (_page == HomePage::RECENT) {
  134. the_mesh.getRecentlyHeard(recent, UI_RECENT_LIST_SIZE);
  135. display.setColor(DisplayDriver::GREEN);
  136. int y = 20;
  137. for (int i = 0; i < UI_RECENT_LIST_SIZE; i++, y += 11) {
  138. auto a = &recent[i];
  139. if (a->name[0] == 0) continue; // empty slot
  140. display.setCursor(0, y);
  141. display.print(a->name);
  142. int secs = _rtc->getCurrentTime() - a->recv_timestamp;
  143. if (secs < 60) {
  144. sprintf(tmp, "%ds", secs);
  145. } else if (secs < 60*60) {
  146. sprintf(tmp, "%dm", secs / 60);
  147. } else {
  148. sprintf(tmp, "%dh", secs / (60*60));
  149. }
  150. display.setCursor(display.width() - display.getTextWidth(tmp) - 1, y);
  151. display.print(tmp);
  152. }
  153. } else if (_page == HomePage::RADIO) {
  154. display.setColor(DisplayDriver::YELLOW);
  155. display.setTextSize(1);
  156. // freq / sf
  157. display.setCursor(0, 20);
  158. sprintf(tmp, "FQ: %06.3f SF: %d", _node_prefs->freq, _node_prefs->sf);
  159. display.print(tmp);
  160. display.setCursor(0, 31);
  161. sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr);
  162. display.print(tmp);
  163. // tx power, noise floor
  164. display.setCursor(0, 42);
  165. sprintf(tmp, "TX: %ddBm", _node_prefs->tx_power_dbm);
  166. display.print(tmp);
  167. display.setCursor(0, 53);
  168. sprintf(tmp, "Noise floor: %d", radio_driver.getNoiseFloor());
  169. display.print(tmp);
  170. } else if (_page == HomePage::BLUETOOTH) {
  171. display.setColor(DisplayDriver::GREEN);
  172. display.drawXbm((display.width() - 32) / 2, 18,
  173. _task->isSerialEnabled() ? bluetooth_on : bluetooth_off,
  174. 32, 32);
  175. display.setTextSize(1);
  176. display.drawTextCentered(display.width() / 2, 64 - 11, "toggle: " PRESS_LABEL);
  177. } else if (_page == HomePage::ADVERT) {
  178. display.setColor(DisplayDriver::GREEN);
  179. display.drawXbm((display.width() - 32) / 2, 18, advert_icon, 32, 32);
  180. display.drawTextCentered(display.width() / 2, 64 - 11, "advert: " PRESS_LABEL);
  181. } else if (_page == HomePage::SHUTDOWN) {
  182. display.setColor(DisplayDriver::GREEN);
  183. display.setTextSize(1);
  184. if (_shutdown_init) {
  185. display.drawTextCentered(display.width() / 2, 34, "hibernating...");
  186. } else {
  187. display.drawXbm((display.width() - 32) / 2, 18, power_icon, 32, 32);
  188. display.drawTextCentered(display.width() / 2, 64 - 11, "hibernate: " PRESS_LABEL);
  189. }
  190. }
  191. return 5000; // next render after 5000 ms
  192. }
  193. bool handleInput(char c) override {
  194. if (c == KEY_LEFT) {
  195. _page = (_page + HomePage::Count - 1) % HomePage::Count;
  196. return true;
  197. }
  198. if (c == KEY_RIGHT || c == KEY_SELECT) {
  199. _page = (_page + 1) % HomePage::Count;
  200. if (_page == HomePage::RECENT) {
  201. _task->showAlert("Recent adverts", 800);
  202. }
  203. return true;
  204. }
  205. if (c == KEY_ENTER && _page == HomePage::BLUETOOTH) {
  206. if (_task->isSerialEnabled()) { // toggle Bluetooth on/off
  207. _task->disableSerial();
  208. } else {
  209. _task->enableSerial();
  210. }
  211. return true;
  212. }
  213. if (c == KEY_ENTER && _page == HomePage::ADVERT) {
  214. #ifdef PIN_BUZZER
  215. _task->soundBuzzer(UIEventType::ack);
  216. #endif
  217. if (the_mesh.advert()) {
  218. _task->showAlert("Advert sent!", 1000);
  219. } else {
  220. _task->showAlert("Advert failed..", 1000);
  221. }
  222. return true;
  223. }
  224. if (c == KEY_ENTER && _page == HomePage::SHUTDOWN) {
  225. _shutdown_init = true; // need to wait for button to be released
  226. return true;
  227. }
  228. return false;
  229. }
  230. };
  231. class MsgPreviewScreen : public UIScreen {
  232. UITask* _task;
  233. mesh::RTCClock* _rtc;
  234. struct MsgEntry {
  235. uint32_t timestamp;
  236. char origin[62];
  237. char msg[78];
  238. };
  239. #define MAX_UNREAD_MSGS 32
  240. int num_unread;
  241. MsgEntry unread[MAX_UNREAD_MSGS];
  242. public:
  243. MsgPreviewScreen(UITask* task, mesh::RTCClock* rtc) : _task(task), _rtc(rtc) { num_unread = 0; }
  244. void addPreview(uint8_t path_len, const char* from_name, const char* msg) {
  245. if (num_unread >= MAX_UNREAD_MSGS) return; // full
  246. auto p = &unread[num_unread++];
  247. p->timestamp = _rtc->getCurrentTime();
  248. if (path_len == 0xFF) {
  249. sprintf(p->origin, "(D) %s:", from_name);
  250. } else {
  251. sprintf(p->origin, "(%d) %s:", (uint32_t) path_len, from_name);
  252. }
  253. StrHelper::strncpy(p->msg, msg, sizeof(p->msg));
  254. }
  255. int render(DisplayDriver& display) override {
  256. char tmp[16];
  257. display.setCursor(0, 0);
  258. display.setTextSize(1);
  259. display.setColor(DisplayDriver::GREEN);
  260. sprintf(tmp, "Unread: %d", num_unread);
  261. display.print(tmp);
  262. auto p = &unread[0];
  263. int secs = _rtc->getCurrentTime() - p->timestamp;
  264. if (secs < 60) {
  265. sprintf(tmp, "%ds", secs);
  266. } else if (secs < 60*60) {
  267. sprintf(tmp, "%dm", secs / 60);
  268. } else {
  269. sprintf(tmp, "%dh", secs / (60*60));
  270. }
  271. display.setCursor(display.width() - display.getTextWidth(tmp) - 2, 0);
  272. display.print(tmp);
  273. display.drawRect(0, 11, display.width(), 1); // horiz line
  274. display.setCursor(0, 14);
  275. display.setColor(DisplayDriver::YELLOW);
  276. display.print(p->origin);
  277. display.setCursor(0, 25);
  278. display.setColor(DisplayDriver::LIGHT);
  279. display.printWordWrap(p->msg, display.width());
  280. return 1000; // next render after 1000 ms
  281. }
  282. bool handleInput(char c) override {
  283. if (c == KEY_SELECT || c == KEY_RIGHT) {
  284. num_unread--;
  285. if (num_unread == 0) {
  286. _task->gotoHomeScreen();
  287. } else {
  288. // delete first/curr item from unread queue
  289. for (int i = 0; i < num_unread; i++) {
  290. unread[i] = unread[i + 1];
  291. }
  292. }
  293. return true;
  294. }
  295. if (c == KEY_ENTER) {
  296. num_unread = 0; // clear unread queue
  297. _task->gotoHomeScreen();
  298. return true;
  299. }
  300. return false;
  301. }
  302. };
  303. void UITask::begin(DisplayDriver* display, SensorManager* sensors, NodePrefs* node_prefs) {
  304. _display = display;
  305. _sensors = sensors;
  306. _auto_off = millis() + AUTO_OFF_MILLIS;
  307. #if defined(PIN_USER_BTN)
  308. user_btn.begin();
  309. #endif
  310. _node_prefs = node_prefs;
  311. if (_display != NULL) {
  312. _display->turnOn();
  313. }
  314. #ifdef PIN_BUZZER
  315. buzzer.begin();
  316. #endif
  317. ui_started_at = millis();
  318. _alert_expiry = 0;
  319. splash = new SplashScreen(this);
  320. home = new HomeScreen(this, &rtc_clock, sensors, node_prefs);
  321. msg_preview = new MsgPreviewScreen(this, &rtc_clock);
  322. setCurrScreen(splash);
  323. }
  324. void UITask::showAlert(const char* text, int duration_millis) {
  325. strcpy(_alert, text);
  326. _alert_expiry = millis() + duration_millis;
  327. }
  328. void UITask::soundBuzzer(UIEventType bet) {
  329. #if defined(PIN_BUZZER)
  330. switch(bet){
  331. case UIEventType::contactMessage:
  332. // gemini's pick
  333. buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
  334. break;
  335. case UIEventType::channelMessage:
  336. buzzer.play("kerplop:d=16,o=6,b=120:32g#,32c#");
  337. break;
  338. case UIEventType::ack:
  339. buzzer.play("ack:d=32,o=8,b=120:c");
  340. break;
  341. case UIEventType::roomMessage:
  342. case UIEventType::newContactMessage:
  343. case UIEventType::none:
  344. default:
  345. break;
  346. }
  347. #endif
  348. }
  349. void UITask::msgRead(int msgcount) {
  350. _msgcount = msgcount;
  351. if (msgcount == 0) {
  352. gotoHomeScreen();
  353. }
  354. }
  355. void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) {
  356. _msgcount = msgcount;
  357. ((MsgPreviewScreen *) msg_preview)->addPreview(path_len, from_name, text);
  358. setCurrScreen(msg_preview);
  359. if (_display != NULL) {
  360. if (!_display->isOn()) _display->turnOn();
  361. _auto_off = millis() + AUTO_OFF_MILLIS; // extend the auto-off timer
  362. _next_refresh = 0; // trigger refresh
  363. }
  364. }
  365. void UITask::userLedHandler() {
  366. #ifdef PIN_STATUS_LED
  367. static int state = 0;
  368. static int next_change = 0;
  369. static int last_increment = 0;
  370. int cur_time = millis();
  371. if (cur_time > next_change) {
  372. if (state == 0) {
  373. state = 1;
  374. if (_msgcount > 0) {
  375. last_increment = LED_ON_MSG_MILLIS;
  376. } else {
  377. last_increment = LED_ON_MILLIS;
  378. }
  379. next_change = cur_time + last_increment;
  380. } else {
  381. state = 0;
  382. next_change = cur_time + LED_CYCLE_MILLIS - last_increment;
  383. }
  384. digitalWrite(PIN_STATUS_LED, state);
  385. }
  386. #endif
  387. }
  388. void UITask::setCurrScreen(UIScreen* c) {
  389. curr = c;
  390. _next_refresh = 0;
  391. }
  392. /*
  393. hardware-agnostic pre-shutdown activity should be done here
  394. */
  395. void UITask::shutdown(bool restart){
  396. #ifdef PIN_BUZZER
  397. /* note: we have a choice here -
  398. we can do a blocking buzzer.loop() with non-deterministic consequences
  399. or we can set a flag and delay the shutdown for a couple of seconds
  400. while a non-blocking buzzer.loop() plays out in UITask::loop()
  401. */
  402. buzzer.shutdown();
  403. uint32_t buzzer_timer = millis(); // fail-safe shutdown
  404. while (buzzer.isPlaying() && (millis() - 2500) < buzzer_timer)
  405. buzzer.loop();
  406. #endif // PIN_BUZZER
  407. if (restart) {
  408. _board->reboot();
  409. } else {
  410. _display->turnOff();
  411. _board->powerOff();
  412. }
  413. }
  414. bool UITask::isButtonPressed() const {
  415. #ifdef PIN_USER_BTN
  416. return user_btn.isPressed();
  417. #else
  418. return false;
  419. #endif
  420. }
  421. void UITask::loop() {
  422. char c = 0;
  423. #if defined(PIN_USER_BTN)
  424. int ev = user_btn.check();
  425. if (ev == BUTTON_EVENT_CLICK) {
  426. c = checkDisplayOn(KEY_SELECT);
  427. } else if (ev == BUTTON_EVENT_LONG_PRESS) {
  428. c = handleLongPress(KEY_ENTER);
  429. }
  430. #endif
  431. #if defined(WIO_TRACKER_L1)
  432. ev = joystick_left.check();
  433. if (ev == BUTTON_EVENT_CLICK) {
  434. c = checkDisplayOn(KEY_LEFT);
  435. } else if (ev == BUTTON_EVENT_LONG_PRESS) {
  436. c = handleLongPress(KEY_LEFT);
  437. }
  438. ev = joystick_right.check();
  439. if (ev == BUTTON_EVENT_CLICK) {
  440. c = checkDisplayOn(KEY_RIGHT);
  441. } else if (ev == BUTTON_EVENT_LONG_PRESS) {
  442. c = handleLongPress(KEY_RIGHT);
  443. }
  444. #endif
  445. if (c != 0 && curr) {
  446. curr->handleInput(c);
  447. _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
  448. _next_refresh = 0; // trigger refresh
  449. }
  450. userLedHandler();
  451. #ifdef PIN_BUZZER
  452. if (buzzer.isPlaying()) buzzer.loop();
  453. #endif
  454. if (curr) curr->poll();
  455. if (_display != NULL && _display->isOn()) {
  456. if (millis() >= _next_refresh && curr) {
  457. _display->startFrame();
  458. int delay_millis = curr->render(*_display);
  459. if (millis() < _alert_expiry) { // render alert popup
  460. _display->setTextSize(1);
  461. int y = _display->height() / 3;
  462. int p = _display->height() / 32;
  463. _display->setColor(DisplayDriver::DARK);
  464. _display->fillRect(p, y, _display->width() - p*2, y);
  465. _display->setColor(DisplayDriver::LIGHT); // draw box border
  466. _display->drawRect(p, y, _display->width() - p*2, y);
  467. _display->drawTextCentered(_display->width() / 2, y + p*3, _alert);
  468. _next_refresh = _alert_expiry; // will need refresh when alert is dismissed
  469. } else {
  470. _next_refresh = millis() + delay_millis;
  471. }
  472. _display->endFrame();
  473. }
  474. if (millis() > _auto_off) {
  475. _display->turnOff();
  476. }
  477. }
  478. #ifdef AUTO_SHUTDOWN_MILLIVOLTS
  479. if (millis() > next_batt_chck) {
  480. uint16_t milliVolts = getBattMilliVolts();
  481. if (milliVolts > 0 && milliVolts < AUTO_SHUTDOWN_MILLIVOLTS) {
  482. shutdown();
  483. }
  484. next_batt_chck = millis() + 8000;
  485. }
  486. #endif
  487. }
  488. char UITask::checkDisplayOn(char c) {
  489. if (_display != NULL) {
  490. if (!_display->isOn()) {
  491. _display->turnOn(); // turn display on and consume event
  492. c = 0;
  493. }
  494. _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
  495. _next_refresh = 0; // trigger refresh
  496. }
  497. return c;
  498. }
  499. char UITask::handleLongPress(char c) {
  500. if (millis() - ui_started_at < 8000) { // long press in first 8 seconds since startup -> CLI/rescue
  501. the_mesh.enterCLIRescue();
  502. c = 0; // consume event
  503. }
  504. return c;
  505. }
  506. /*
  507. void UITask::handleButtonTriplePress() {
  508. MESH_DEBUG_PRINTLN("UITask: triple press triggered");
  509. // Toggle buzzer quiet mode
  510. #ifdef PIN_BUZZER
  511. if (buzzer.isQuiet()) {
  512. buzzer.quiet(false);
  513. soundBuzzer(UIEventType::ack);
  514. showAlert("Buzzer: ON", 600);
  515. } else {
  516. buzzer.quiet(true);
  517. showAlert("Buzzer: OFF", 600);
  518. }
  519. _next_refresh = 0; // trigger refresh
  520. #endif
  521. }
  522. */