UITask.cpp 16 KB

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