UITask.cpp 16 KB

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