UITask.cpp 27 KB

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