UITask.cpp 27 KB

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