UITask.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #include "UITask.h"
  2. #include <helpers/TxtDataHelpers.h>
  3. #include "../MyMesh.h"
  4. #include "target.h"
  5. #ifdef WIFI_SSID
  6. #include <WiFi.h>
  7. #endif
  8. #ifndef AUTO_OFF_MILLIS
  9. #define AUTO_OFF_MILLIS 15000 // 15 seconds
  10. #endif
  11. #define BOOT_SCREEN_MILLIS 3000 // 3 seconds
  12. #ifdef PIN_STATUS_LED
  13. #define LED_ON_MILLIS 20
  14. #define LED_ON_MSG_MILLIS 200
  15. #define LED_CYCLE_MILLIS 4000
  16. #endif
  17. #define LONG_PRESS_MILLIS 1200
  18. #ifndef UI_RECENT_LIST_SIZE
  19. #define UI_RECENT_LIST_SIZE 4
  20. #endif
  21. #if UI_HAS_JOYSTICK
  22. #define PRESS_LABEL "press Enter"
  23. #else
  24. #define PRESS_LABEL "long press"
  25. #endif
  26. #include "icons.h"
  27. class SplashScreen : public UIScreen {
  28. UITask* _task;
  29. unsigned long dismiss_after;
  30. char _version_info[12];
  31. public:
  32. SplashScreen(UITask* task) : _task(task) {
  33. // strip off dash and commit hash by changing dash to null terminator
  34. // e.g: v1.2.3-abcdef -> v1.2.3
  35. const char *ver = FIRMWARE_VERSION;
  36. const char *dash = strchr(ver, '-');
  37. int len = dash ? dash - ver : strlen(ver);
  38. if (len >= sizeof(_version_info)) len = sizeof(_version_info) - 1;
  39. memcpy(_version_info, ver, len);
  40. _version_info[len] = 0;
  41. dismiss_after = millis() + BOOT_SCREEN_MILLIS;
  42. }
  43. int render(DisplayDriver& display) override {
  44. // meshcore logo
  45. display.setColor(DisplayDriver::BLUE);
  46. int logoWidth = 128;
  47. display.drawXbm((display.width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
  48. // version info
  49. display.setColor(DisplayDriver::LIGHT);
  50. display.setTextSize(2);
  51. display.drawTextCentered(display.width()/2, 22, _version_info);
  52. display.setTextSize(1);
  53. display.drawTextCentered(display.width()/2, 42, FIRMWARE_BUILD_DATE);
  54. return 1000;
  55. }
  56. void poll() override {
  57. if (millis() >= dismiss_after) {
  58. _task->gotoHomeScreen();
  59. }
  60. }
  61. };
  62. class HomeScreen : public UIScreen {
  63. enum HomePage {
  64. FIRST,
  65. RECENT,
  66. RADIO,
  67. BLUETOOTH,
  68. ADVERT,
  69. #if ENV_INCLUDE_GPS == 1
  70. GPS,
  71. #endif
  72. #if UI_SENSORS_PAGE == 1
  73. SENSORS,
  74. #endif
  75. SHUTDOWN,
  76. Count // keep as last
  77. };
  78. UITask* _task;
  79. mesh::RTCClock* _rtc;
  80. SensorManager* _sensors;
  81. NodePrefs* _node_prefs;
  82. uint8_t _page;
  83. bool _shutdown_init;
  84. AdvertPath recent[UI_RECENT_LIST_SIZE];
  85. void renderBatteryIndicator(DisplayDriver& display, uint16_t batteryMilliVolts) {
  86. // Convert millivolts to percentage
  87. #ifndef BATT_MIN_MILLIVOLTS
  88. #define BATT_MIN_MILLIVOLTS 3000
  89. #endif
  90. #ifndef BATT_MAX_MILLIVOLTS
  91. #define BATT_MAX_MILLIVOLTS 4200
  92. #endif
  93. const int minMilliVolts = BATT_MIN_MILLIVOLTS;
  94. const int maxMilliVolts = BATT_MAX_MILLIVOLTS;
  95. int batteryPercentage = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts);
  96. if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
  97. if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
  98. // battery icon
  99. int iconWidth = 24;
  100. int iconHeight = 10;
  101. int iconX = display.width() - iconWidth - 5; // Position the icon near the top-right corner
  102. int iconY = 0;
  103. display.setColor(DisplayDriver::GREEN);
  104. // battery outline
  105. display.drawRect(iconX, iconY, iconWidth, iconHeight);
  106. // battery "cap"
  107. display.fillRect(iconX + iconWidth, iconY + (iconHeight / 4), 3, iconHeight / 2);
  108. // fill the battery based on the percentage
  109. int fillWidth = (batteryPercentage * (iconWidth - 4)) / 100;
  110. display.fillRect(iconX + 2, iconY + 2, fillWidth, iconHeight - 4);
  111. // show muted icon if buzzer is muted
  112. #ifdef PIN_BUZZER
  113. if (_task->isBuzzerQuiet()) {
  114. display.setColor(DisplayDriver::RED);
  115. display.drawXbm(iconX - 9, iconY + 1, muted_icon, 8, 8);
  116. }
  117. #endif
  118. }
  119. CayenneLPP sensors_lpp;
  120. int sensors_nb = 0;
  121. bool sensors_scroll = false;
  122. int sensors_scroll_offset = 0;
  123. int next_sensors_refresh = 0;
  124. void refresh_sensors() {
  125. if (millis() > next_sensors_refresh) {
  126. sensors_lpp.reset();
  127. sensors_nb = 0;
  128. sensors_lpp.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
  129. sensors.querySensors(0xFF, sensors_lpp);
  130. LPPReader reader (sensors_lpp.getBuffer(), sensors_lpp.getSize());
  131. uint8_t channel, type;
  132. while(reader.readHeader(channel, type)) {
  133. reader.skipData(type);
  134. sensors_nb ++;
  135. }
  136. sensors_scroll = sensors_nb > UI_RECENT_LIST_SIZE;
  137. #if AUTO_OFF_MILLIS > 0
  138. next_sensors_refresh = millis() + 5000; // refresh sensor values every 5 sec
  139. #else
  140. next_sensors_refresh = millis() + 60000; // refresh sensor values every 1 min
  141. #endif
  142. }
  143. }
  144. public:
  145. HomeScreen(UITask* task, mesh::RTCClock* rtc, SensorManager* sensors, NodePrefs* node_prefs)
  146. : _task(task), _rtc(rtc), _sensors(sensors), _node_prefs(node_prefs), _page(0),
  147. _shutdown_init(false), sensors_lpp(200) { }
  148. void poll() override {
  149. if (_shutdown_init && !_task->isButtonPressed()) { // must wait for USR button to be released
  150. _task->shutdown();
  151. }
  152. }
  153. int render(DisplayDriver& display) override {
  154. char tmp[80];
  155. // node name
  156. display.setTextSize(1);
  157. display.setColor(DisplayDriver::GREEN);
  158. char filtered_name[sizeof(_node_prefs->node_name)];
  159. display.translateUTF8ToBlocks(filtered_name, _node_prefs->node_name, sizeof(filtered_name));
  160. display.setCursor(0, 0);
  161. display.print(filtered_name);
  162. // battery voltage
  163. renderBatteryIndicator(display, _task->getBattMilliVolts());
  164. // curr page indicator
  165. int y = 14;
  166. int x = display.width() / 2 - 5 * (HomePage::Count-1);
  167. for (uint8_t i = 0; i < HomePage::Count; i++, x += 10) {
  168. if (i == _page) {
  169. display.fillRect(x-1, y-1, 3, 3);
  170. } else {
  171. display.fillRect(x, y, 1, 1);
  172. }
  173. }
  174. if (_page == HomePage::FIRST) {
  175. display.setColor(DisplayDriver::YELLOW);
  176. display.setTextSize(2);
  177. sprintf(tmp, "MSG: %d", _task->getMsgCount());
  178. display.drawTextCentered(display.width() / 2, 20, tmp);
  179. #ifdef WIFI_SSID
  180. IPAddress ip = WiFi.localIP();
  181. snprintf(tmp, sizeof(tmp), "IP: %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  182. display.setTextSize(1);
  183. display.drawTextCentered(display.width() / 2, 54, tmp);
  184. #endif
  185. if (_task->hasConnection()) {
  186. display.setColor(DisplayDriver::GREEN);
  187. display.setTextSize(1);
  188. display.drawTextCentered(display.width() / 2, 43, "< Connected >");
  189. } else if (the_mesh.getBLEPin() != 0) { // BT pin
  190. display.setColor(DisplayDriver::RED);
  191. display.setTextSize(2);
  192. sprintf(tmp, "Pin:%d", the_mesh.getBLEPin());
  193. display.drawTextCentered(display.width() / 2, 43, tmp);
  194. }
  195. } else if (_page == HomePage::RECENT) {
  196. the_mesh.getRecentlyHeard(recent, UI_RECENT_LIST_SIZE);
  197. display.setColor(DisplayDriver::GREEN);
  198. int y = 20;
  199. for (int i = 0; i < UI_RECENT_LIST_SIZE; i++, y += 11) {
  200. auto a = &recent[i];
  201. if (a->name[0] == 0) continue; // empty slot
  202. int secs = _rtc->getCurrentTime() - a->recv_timestamp;
  203. if (secs < 60) {
  204. sprintf(tmp, "%ds", secs);
  205. } else if (secs < 60*60) {
  206. sprintf(tmp, "%dm", secs / 60);
  207. } else {
  208. sprintf(tmp, "%dh", secs / (60*60));
  209. }
  210. int timestamp_width = display.getTextWidth(tmp);
  211. int max_name_width = display.width() - timestamp_width - 1;
  212. char filtered_recent_name[sizeof(a->name)];
  213. display.translateUTF8ToBlocks(filtered_recent_name, a->name, sizeof(filtered_recent_name));
  214. display.drawTextEllipsized(0, y, max_name_width, filtered_recent_name);
  215. display.setCursor(display.width() - timestamp_width - 1, y);
  216. display.print(tmp);
  217. }
  218. } else if (_page == HomePage::RADIO) {
  219. display.setColor(DisplayDriver::YELLOW);
  220. display.setTextSize(1);
  221. // freq / sf
  222. display.setCursor(0, 20);
  223. sprintf(tmp, "FQ: %06.3f SF: %d", _node_prefs->freq, _node_prefs->sf);
  224. display.print(tmp);
  225. display.setCursor(0, 31);
  226. sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr);
  227. display.print(tmp);
  228. // tx power, noise floor
  229. display.setCursor(0, 42);
  230. sprintf(tmp, "TX: %ddBm", _node_prefs->tx_power_dbm);
  231. display.print(tmp);
  232. display.setCursor(0, 53);
  233. sprintf(tmp, "Noise floor: %d", radio_driver.getNoiseFloor());
  234. display.print(tmp);
  235. } else if (_page == HomePage::BLUETOOTH) {
  236. display.setColor(DisplayDriver::GREEN);
  237. display.drawXbm((display.width() - 32) / 2, 18,
  238. _task->isSerialEnabled() ? bluetooth_on : bluetooth_off,
  239. 32, 32);
  240. display.setTextSize(1);
  241. display.drawTextCentered(display.width() / 2, 64 - 11, "toggle: " PRESS_LABEL);
  242. } else if (_page == HomePage::ADVERT) {
  243. display.setColor(DisplayDriver::GREEN);
  244. display.drawXbm((display.width() - 32) / 2, 18, advert_icon, 32, 32);
  245. display.drawTextCentered(display.width() / 2, 64 - 11, "advert: " PRESS_LABEL);
  246. #if ENV_INCLUDE_GPS == 1
  247. } else if (_page == HomePage::GPS) {
  248. LocationProvider* nmea = sensors.getLocationProvider();
  249. char buf[50];
  250. int y = 18;
  251. bool gps_state = _task->getGPSState();
  252. #ifdef PIN_GPS_SWITCH
  253. bool hw_gps_state = digitalRead(PIN_GPS_SWITCH);
  254. if (gps_state != hw_gps_state) {
  255. strcpy(buf, gps_state ? "gps off(hw)" : "gps off(sw)");
  256. } else {
  257. strcpy(buf, gps_state ? "gps on" : "gps off");
  258. }
  259. #else
  260. strcpy(buf, gps_state ? "gps on" : "gps off");
  261. #endif
  262. display.drawTextLeftAlign(0, y, buf);
  263. if (nmea == NULL) {
  264. y = y + 12;
  265. display.drawTextLeftAlign(0, y, "Can't access GPS");
  266. } else {
  267. strcpy(buf, nmea->isValid()?"fix":"no fix");
  268. display.drawTextRightAlign(display.width()-1, y, buf);
  269. y = y + 12;
  270. display.drawTextLeftAlign(0, y, "sat");
  271. sprintf(buf, "%d", nmea->satellitesCount());
  272. display.drawTextRightAlign(display.width()-1, y, buf);
  273. y = y + 12;
  274. display.drawTextLeftAlign(0, y, "pos");
  275. sprintf(buf, "%.4f %.4f",
  276. nmea->getLatitude()/1000000., nmea->getLongitude()/1000000.);
  277. display.drawTextRightAlign(display.width()-1, y, buf);
  278. y = y + 12;
  279. display.drawTextLeftAlign(0, y, "alt");
  280. sprintf(buf, "%.2f", nmea->getAltitude()/1000.);
  281. display.drawTextRightAlign(display.width()-1, y, buf);
  282. y = y + 12;
  283. }
  284. #endif
  285. #if UI_SENSORS_PAGE == 1
  286. } else if (_page == HomePage::SENSORS) {
  287. int y = 18;
  288. refresh_sensors();
  289. char buf[30];
  290. char name[30];
  291. LPPReader r(sensors_lpp.getBuffer(), sensors_lpp.getSize());
  292. for (int i = 0; i < sensors_scroll_offset; i++) {
  293. uint8_t channel, type;
  294. r.readHeader(channel, type);
  295. r.skipData(type);
  296. }
  297. for (int i = 0; i < (sensors_scroll?UI_RECENT_LIST_SIZE:sensors_nb); i++) {
  298. uint8_t channel, type;
  299. if (!r.readHeader(channel, type)) { // reached end, reset
  300. r.reset();
  301. r.readHeader(channel, type);
  302. }
  303. display.setCursor(0, y);
  304. float v;
  305. switch (type) {
  306. case LPP_GPS: // GPS
  307. float lat, lon, alt;
  308. r.readGPS(lat, lon, alt);
  309. strcpy(name, "gps"); sprintf(buf, "%.4f %.4f", lat, lon);
  310. break;
  311. case LPP_VOLTAGE:
  312. r.readVoltage(v);
  313. strcpy(name, "voltage"); sprintf(buf, "%6.2f", v);
  314. break;
  315. case LPP_CURRENT:
  316. r.readCurrent(v);
  317. strcpy(name, "current"); sprintf(buf, "%.3f", v);
  318. break;
  319. case LPP_TEMPERATURE:
  320. r.readTemperature(v);
  321. strcpy(name, "temperature"); sprintf(buf, "%.2f", v);
  322. break;
  323. case LPP_RELATIVE_HUMIDITY:
  324. r.readRelativeHumidity(v);
  325. strcpy(name, "humidity"); sprintf(buf, "%.2f", v);
  326. break;
  327. case LPP_BAROMETRIC_PRESSURE:
  328. r.readPressure(v);
  329. strcpy(name, "pressure"); sprintf(buf, "%.2f", v);
  330. break;
  331. case LPP_ALTITUDE:
  332. r.readAltitude(v);
  333. strcpy(name, "altitude"); sprintf(buf, "%.0f", v);
  334. break;
  335. case LPP_POWER:
  336. r.readPower(v);
  337. strcpy(name, "power"); sprintf(buf, "%6.2f", v);
  338. break;
  339. default:
  340. r.skipData(type);
  341. strcpy(name, "unk"); sprintf(buf, "");
  342. }
  343. display.setCursor(0, y);
  344. display.print(name);
  345. display.setCursor(
  346. display.width()-display.getTextWidth(buf)-1, y
  347. );
  348. display.print(buf);
  349. y = y + 12;
  350. }
  351. if (sensors_scroll) sensors_scroll_offset = (sensors_scroll_offset+1)%sensors_nb;
  352. else sensors_scroll_offset = 0;
  353. #endif
  354. } else if (_page == HomePage::SHUTDOWN) {
  355. display.setColor(DisplayDriver::GREEN);
  356. display.setTextSize(1);
  357. if (_shutdown_init) {
  358. display.drawTextCentered(display.width() / 2, 34, "hibernating...");
  359. } else {
  360. display.drawXbm((display.width() - 32) / 2, 18, power_icon, 32, 32);
  361. display.drawTextCentered(display.width() / 2, 64 - 11, "hibernate:" PRESS_LABEL);
  362. }
  363. }
  364. return 5000; // next render after 5000 ms
  365. }
  366. bool handleInput(char c) override {
  367. if (c == KEY_LEFT || c == KEY_PREV) {
  368. _page = (_page + HomePage::Count - 1) % HomePage::Count;
  369. return true;
  370. }
  371. if (c == KEY_NEXT || c == KEY_RIGHT) {
  372. _page = (_page + 1) % HomePage::Count;
  373. if (_page == HomePage::RECENT) {
  374. _task->showAlert("Recent adverts", 800);
  375. }
  376. return true;
  377. }
  378. if (c == KEY_ENTER && _page == HomePage::BLUETOOTH) {
  379. if (_task->isSerialEnabled()) { // toggle Bluetooth on/off
  380. _task->disableSerial();
  381. } else {
  382. _task->enableSerial();
  383. }
  384. return true;
  385. }
  386. if (c == KEY_ENTER && _page == HomePage::ADVERT) {
  387. _task->notify(UIEventType::ack);
  388. if (the_mesh.advert()) {
  389. _task->showAlert("Advert sent!", 1000);
  390. } else {
  391. _task->showAlert("Advert failed..", 1000);
  392. }
  393. return true;
  394. }
  395. #if ENV_INCLUDE_GPS == 1
  396. if (c == KEY_ENTER && _page == HomePage::GPS) {
  397. _task->toggleGPS();
  398. return true;
  399. }
  400. #endif
  401. #if UI_SENSORS_PAGE == 1
  402. if (c == KEY_ENTER && _page == HomePage::SENSORS) {
  403. _task->toggleGPS();
  404. next_sensors_refresh=0;
  405. return true;
  406. }
  407. #endif
  408. if (c == KEY_ENTER && _page == HomePage::SHUTDOWN) {
  409. _shutdown_init = true; // need to wait for button to be released
  410. return true;
  411. }
  412. return false;
  413. }
  414. };
  415. class MsgPreviewScreen : public UIScreen {
  416. UITask* _task;
  417. mesh::RTCClock* _rtc;
  418. struct MsgEntry {
  419. uint32_t timestamp;
  420. char origin[62];
  421. char msg[78];
  422. };
  423. #define MAX_UNREAD_MSGS 32
  424. int num_unread;
  425. int head = MAX_UNREAD_MSGS - 1; // index of latest unread message
  426. MsgEntry unread[MAX_UNREAD_MSGS];
  427. public:
  428. MsgPreviewScreen(UITask* task, mesh::RTCClock* rtc) : _task(task), _rtc(rtc) { num_unread = 0; }
  429. void addPreview(uint8_t path_len, const char* from_name, const char* msg) {
  430. head = (head + 1) % MAX_UNREAD_MSGS;
  431. if (num_unread < MAX_UNREAD_MSGS) num_unread++;
  432. auto p = &unread[head];
  433. p->timestamp = _rtc->getCurrentTime();
  434. if (path_len == 0xFF) {
  435. sprintf(p->origin, "(D) %s:", from_name);
  436. } else {
  437. sprintf(p->origin, "(%d) %s:", (uint32_t) path_len, from_name);
  438. }
  439. StrHelper::strncpy(p->msg, msg, sizeof(p->msg));
  440. }
  441. int render(DisplayDriver& display) override {
  442. char tmp[16];
  443. display.setCursor(0, 0);
  444. display.setTextSize(1);
  445. display.setColor(DisplayDriver::GREEN);
  446. sprintf(tmp, "Unread: %d", num_unread);
  447. display.print(tmp);
  448. auto p = &unread[head];
  449. int secs = _rtc->getCurrentTime() - p->timestamp;
  450. if (secs < 60) {
  451. sprintf(tmp, "%ds", secs);
  452. } else if (secs < 60*60) {
  453. sprintf(tmp, "%dm", secs / 60);
  454. } else {
  455. sprintf(tmp, "%dh", secs / (60*60));
  456. }
  457. display.setCursor(display.width() - display.getTextWidth(tmp) - 2, 0);
  458. display.print(tmp);
  459. display.drawRect(0, 11, display.width(), 1); // horiz line
  460. display.setCursor(0, 14);
  461. display.setColor(DisplayDriver::YELLOW);
  462. char filtered_origin[sizeof(p->origin)];
  463. display.translateUTF8ToBlocks(filtered_origin, p->origin, sizeof(filtered_origin));
  464. display.print(filtered_origin);
  465. display.setCursor(0, 25);
  466. display.setColor(DisplayDriver::LIGHT);
  467. char filtered_msg[sizeof(p->msg)];
  468. display.translateUTF8ToBlocks(filtered_msg, p->msg, sizeof(filtered_msg));
  469. display.printWordWrap(filtered_msg, display.width());
  470. #if AUTO_OFF_MILLIS==0 // probably e-ink
  471. return 10000; // 10 s
  472. #else
  473. return 1000; // next render after 1000 ms
  474. #endif
  475. }
  476. bool handleInput(char c) override {
  477. if (c == KEY_NEXT || c == KEY_RIGHT) {
  478. head = (head + MAX_UNREAD_MSGS - 1) % MAX_UNREAD_MSGS;
  479. num_unread--;
  480. if (num_unread == 0) {
  481. _task->gotoHomeScreen();
  482. }
  483. return true;
  484. }
  485. if (c == KEY_ENTER) {
  486. num_unread = 0; // clear unread queue
  487. _task->gotoHomeScreen();
  488. return true;
  489. }
  490. return false;
  491. }
  492. };
  493. void UITask::begin(DisplayDriver* display, SensorManager* sensors, NodePrefs* node_prefs) {
  494. _display = display;
  495. _sensors = sensors;
  496. _auto_off = millis() + AUTO_OFF_MILLIS;
  497. #if defined(PIN_USER_BTN)
  498. user_btn.begin();
  499. #endif
  500. #if defined(PIN_USER_BTN_ANA)
  501. analog_btn.begin();
  502. #endif
  503. _node_prefs = node_prefs;
  504. if (_display != NULL) {
  505. _display->turnOn();
  506. }
  507. #ifdef PIN_BUZZER
  508. buzzer.begin();
  509. buzzer.quiet(_node_prefs->buzzer_quiet);
  510. #endif
  511. #ifdef PIN_VIBRATION
  512. vibration.begin();
  513. #endif
  514. ui_started_at = millis();
  515. _alert_expiry = 0;
  516. splash = new SplashScreen(this);
  517. home = new HomeScreen(this, &rtc_clock, sensors, node_prefs);
  518. msg_preview = new MsgPreviewScreen(this, &rtc_clock);
  519. setCurrScreen(splash);
  520. }
  521. void UITask::showAlert(const char* text, int duration_millis) {
  522. strcpy(_alert, text);
  523. _alert_expiry = millis() + duration_millis;
  524. }
  525. void UITask::notify(UIEventType t) {
  526. #if defined(PIN_BUZZER)
  527. switch(t){
  528. case UIEventType::contactMessage:
  529. // gemini's pick
  530. buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
  531. break;
  532. case UIEventType::channelMessage:
  533. buzzer.play("kerplop:d=16,o=6,b=120:32g#,32c#");
  534. break;
  535. case UIEventType::ack:
  536. buzzer.play("ack:d=32,o=8,b=120:c");
  537. break;
  538. case UIEventType::roomMessage:
  539. case UIEventType::newContactMessage:
  540. case UIEventType::none:
  541. default:
  542. break;
  543. }
  544. #endif
  545. #ifdef PIN_VIBRATION
  546. // Trigger vibration for all UI events except none
  547. if (t != UIEventType::none) {
  548. vibration.trigger();
  549. }
  550. #endif
  551. }
  552. void UITask::msgRead(int msgcount) {
  553. _msgcount = msgcount;
  554. if (msgcount == 0) {
  555. gotoHomeScreen();
  556. }
  557. }
  558. void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) {
  559. _msgcount = msgcount;
  560. ((MsgPreviewScreen *) msg_preview)->addPreview(path_len, from_name, text);
  561. setCurrScreen(msg_preview);
  562. if (_display != NULL) {
  563. if (!_display->isOn() && !hasConnection()) {
  564. _display->turnOn();
  565. }
  566. if (_display->isOn()) {
  567. _auto_off = millis() + AUTO_OFF_MILLIS; // extend the auto-off timer
  568. _next_refresh = 100; // trigger refresh
  569. }
  570. }
  571. }
  572. void UITask::userLedHandler() {
  573. #ifdef PIN_STATUS_LED
  574. int cur_time = millis();
  575. if (cur_time > next_led_change) {
  576. if (led_state == 0) {
  577. led_state = 1;
  578. if (_msgcount > 0) {
  579. last_led_increment = LED_ON_MSG_MILLIS;
  580. } else {
  581. last_led_increment = LED_ON_MILLIS;
  582. }
  583. next_led_change = cur_time + last_led_increment;
  584. } else {
  585. led_state = 0;
  586. next_led_change = cur_time + LED_CYCLE_MILLIS - last_led_increment;
  587. }
  588. digitalWrite(PIN_STATUS_LED, led_state == LED_STATE_ON);
  589. }
  590. #endif
  591. }
  592. void UITask::setCurrScreen(UIScreen* c) {
  593. curr = c;
  594. _next_refresh = 100;
  595. }
  596. /*
  597. hardware-agnostic pre-shutdown activity should be done here
  598. */
  599. void UITask::shutdown(bool restart){
  600. #ifdef PIN_BUZZER
  601. /* note: we have a choice here -
  602. we can do a blocking buzzer.loop() with non-deterministic consequences
  603. or we can set a flag and delay the shutdown for a couple of seconds
  604. while a non-blocking buzzer.loop() plays out in UITask::loop()
  605. */
  606. buzzer.shutdown();
  607. uint32_t buzzer_timer = millis(); // fail-safe shutdown
  608. while (buzzer.isPlaying() && (millis() - 2500) < buzzer_timer)
  609. buzzer.loop();
  610. #endif // PIN_BUZZER
  611. if (restart) {
  612. _board->reboot();
  613. } else {
  614. _display->turnOff();
  615. radio_driver.powerOff();
  616. _board->powerOff();
  617. }
  618. }
  619. bool UITask::isButtonPressed() const {
  620. #ifdef PIN_USER_BTN
  621. return user_btn.isPressed();
  622. #else
  623. return false;
  624. #endif
  625. }
  626. void UITask::loop() {
  627. char c = 0;
  628. #if UI_HAS_JOYSTICK
  629. int ev = user_btn.check();
  630. if (ev == BUTTON_EVENT_CLICK) {
  631. c = checkDisplayOn(KEY_ENTER);
  632. } else if (ev == BUTTON_EVENT_LONG_PRESS) {
  633. c = handleLongPress(KEY_ENTER); // REVISIT: could be mapped to different key code
  634. }
  635. ev = joystick_left.check();
  636. if (ev == BUTTON_EVENT_CLICK) {
  637. c = checkDisplayOn(KEY_LEFT);
  638. } else if (ev == BUTTON_EVENT_LONG_PRESS) {
  639. c = handleLongPress(KEY_LEFT);
  640. }
  641. ev = joystick_right.check();
  642. if (ev == BUTTON_EVENT_CLICK) {
  643. c = checkDisplayOn(KEY_RIGHT);
  644. } else if (ev == BUTTON_EVENT_LONG_PRESS) {
  645. c = handleLongPress(KEY_RIGHT);
  646. }
  647. ev = back_btn.check();
  648. if (ev == BUTTON_EVENT_TRIPLE_CLICK) {
  649. c = handleTripleClick(KEY_SELECT);
  650. }
  651. #elif defined(PIN_USER_BTN)
  652. int ev = user_btn.check();
  653. if (ev == BUTTON_EVENT_CLICK) {
  654. c = checkDisplayOn(KEY_NEXT);
  655. } else if (ev == BUTTON_EVENT_LONG_PRESS) {
  656. c = handleLongPress(KEY_ENTER);
  657. } else if (ev == BUTTON_EVENT_DOUBLE_CLICK) {
  658. c = handleDoubleClick(KEY_PREV);
  659. } else if (ev == BUTTON_EVENT_TRIPLE_CLICK) {
  660. c = handleTripleClick(KEY_SELECT);
  661. }
  662. #endif
  663. #if defined(PIN_USER_BTN_ANA)
  664. if (abs(millis() - _analogue_pin_read_millis) > 10) {
  665. ev = analog_btn.check();
  666. if (ev == BUTTON_EVENT_CLICK) {
  667. c = checkDisplayOn(KEY_NEXT);
  668. } else if (ev == BUTTON_EVENT_LONG_PRESS) {
  669. c = handleLongPress(KEY_ENTER);
  670. } else if (ev == BUTTON_EVENT_DOUBLE_CLICK) {
  671. c = handleDoubleClick(KEY_PREV);
  672. } else if (ev == BUTTON_EVENT_TRIPLE_CLICK) {
  673. c = handleTripleClick(KEY_SELECT);
  674. }
  675. _analogue_pin_read_millis = millis();
  676. }
  677. #endif
  678. #if defined(BACKLIGHT_BTN)
  679. if (millis() > next_backlight_btn_check) {
  680. bool touch_state = digitalRead(PIN_BUTTON2);
  681. #if defined(DISP_BACKLIGHT)
  682. digitalWrite(DISP_BACKLIGHT, !touch_state);
  683. #elif defined(EXP_PIN_BACKLIGHT)
  684. expander.digitalWrite(EXP_PIN_BACKLIGHT, !touch_state);
  685. #endif
  686. next_backlight_btn_check = millis() + 300;
  687. }
  688. #endif
  689. if (c != 0 && curr) {
  690. curr->handleInput(c);
  691. _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
  692. _next_refresh = 100; // trigger refresh
  693. }
  694. userLedHandler();
  695. #ifdef PIN_BUZZER
  696. if (buzzer.isPlaying()) buzzer.loop();
  697. #endif
  698. if (curr) curr->poll();
  699. if (_display != NULL && _display->isOn()) {
  700. if (millis() >= _next_refresh && curr) {
  701. _display->startFrame();
  702. int delay_millis = curr->render(*_display);
  703. if (millis() < _alert_expiry) { // render alert popup
  704. _display->setTextSize(1);
  705. int y = _display->height() / 3;
  706. int p = _display->height() / 32;
  707. _display->setColor(DisplayDriver::DARK);
  708. _display->fillRect(p, y, _display->width() - p*2, y);
  709. _display->setColor(DisplayDriver::LIGHT); // draw box border
  710. _display->drawRect(p, y, _display->width() - p*2, y);
  711. _display->drawTextCentered(_display->width() / 2, y + p*3, _alert);
  712. _next_refresh = _alert_expiry; // will need refresh when alert is dismissed
  713. } else {
  714. _next_refresh = millis() + delay_millis;
  715. }
  716. _display->endFrame();
  717. }
  718. #if AUTO_OFF_MILLIS > 0
  719. if (millis() > _auto_off) {
  720. _display->turnOff();
  721. }
  722. #endif
  723. }
  724. #ifdef PIN_VIBRATION
  725. vibration.loop();
  726. #endif
  727. #ifdef AUTO_SHUTDOWN_MILLIVOLTS
  728. if (millis() > next_batt_chck) {
  729. uint16_t milliVolts = getBattMilliVolts();
  730. if (milliVolts > 0 && milliVolts < AUTO_SHUTDOWN_MILLIVOLTS) {
  731. // show low battery shutdown alert
  732. // we should only do this for eink displays, which will persist after power loss
  733. #if defined(THINKNODE_M1) || defined(LILYGO_TECHO)
  734. if (_display != NULL) {
  735. _display->startFrame();
  736. _display->setTextSize(2);
  737. _display->setColor(DisplayDriver::RED);
  738. _display->drawTextCentered(_display->width() / 2, 20, "Low Battery.");
  739. _display->drawTextCentered(_display->width() / 2, 40, "Shutting Down!");
  740. _display->endFrame();
  741. }
  742. #endif
  743. shutdown();
  744. }
  745. next_batt_chck = millis() + 8000;
  746. }
  747. #endif
  748. }
  749. char UITask::checkDisplayOn(char c) {
  750. if (_display != NULL) {
  751. if (!_display->isOn()) {
  752. _display->turnOn(); // turn display on and consume event
  753. c = 0;
  754. }
  755. _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
  756. _next_refresh = 0; // trigger refresh
  757. }
  758. return c;
  759. }
  760. char UITask::handleLongPress(char c) {
  761. if (millis() - ui_started_at < 8000) { // long press in first 8 seconds since startup -> CLI/rescue
  762. the_mesh.enterCLIRescue();
  763. c = 0; // consume event
  764. }
  765. return c;
  766. }
  767. char UITask::handleDoubleClick(char c) {
  768. MESH_DEBUG_PRINTLN("UITask: double click triggered");
  769. checkDisplayOn(c);
  770. return c;
  771. }
  772. char UITask::handleTripleClick(char c) {
  773. MESH_DEBUG_PRINTLN("UITask: triple click triggered");
  774. checkDisplayOn(c);
  775. toggleBuzzer();
  776. c = 0;
  777. return c;
  778. }
  779. bool UITask::getGPSState() {
  780. if (_sensors != NULL) {
  781. int num = _sensors->getNumSettings();
  782. for (int i = 0; i < num; i++) {
  783. if (strcmp(_sensors->getSettingName(i), "gps") == 0) {
  784. return !strcmp(_sensors->getSettingValue(i), "1");
  785. }
  786. }
  787. }
  788. return false;
  789. }
  790. void UITask::toggleGPS() {
  791. if (_sensors != NULL) {
  792. // toggle GPS on/off
  793. int num = _sensors->getNumSettings();
  794. for (int i = 0; i < num; i++) {
  795. if (strcmp(_sensors->getSettingName(i), "gps") == 0) {
  796. if (strcmp(_sensors->getSettingValue(i), "1") == 0) {
  797. _sensors->setSettingValue("gps", "0");
  798. _node_prefs->gps_enabled = 0;
  799. notify(UIEventType::ack);
  800. } else {
  801. _sensors->setSettingValue("gps", "1");
  802. _node_prefs->gps_enabled = 1;
  803. notify(UIEventType::ack);
  804. }
  805. the_mesh.savePrefs();
  806. showAlert(_node_prefs->gps_enabled ? "GPS: Enabled" : "GPS: Disabled", 800);
  807. _next_refresh = 0;
  808. break;
  809. }
  810. }
  811. }
  812. }
  813. void UITask::toggleBuzzer() {
  814. // Toggle buzzer quiet mode
  815. #ifdef PIN_BUZZER
  816. if (buzzer.isQuiet()) {
  817. buzzer.quiet(false);
  818. notify(UIEventType::ack);
  819. } else {
  820. buzzer.quiet(true);
  821. }
  822. _node_prefs->buzzer_quiet = buzzer.isQuiet();
  823. the_mesh.savePrefs();
  824. showAlert(buzzer.isQuiet() ? "Buzzer: OFF" : "Buzzer: ON", 800);
  825. _next_refresh = 0; // trigger refresh
  826. #endif
  827. }