UITask.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #include "UITask.h"
  2. #include <Arduino.h>
  3. #include <helpers/TxtDataHelpers.h>
  4. #include "../MyMesh.h"
  5. #define AUTO_OFF_MILLIS 15000 // 15 seconds
  6. #define BOOT_SCREEN_MILLIS 3000 // 3 seconds
  7. #ifdef PIN_STATUS_LED
  8. #define LED_ON_MILLIS 20
  9. #define LED_ON_MSG_MILLIS 200
  10. #define LED_CYCLE_MILLIS 4000
  11. #endif
  12. #ifndef USER_BTN_PRESSED
  13. #define USER_BTN_PRESSED LOW
  14. #endif
  15. // 'meshcore', 128x13px
  16. static const uint8_t meshcore_logo [] PROGMEM = {
  17. 0x3c, 0x01, 0xe3, 0xff, 0xc7, 0xff, 0x8f, 0x03, 0x87, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe,
  18. 0x3c, 0x03, 0xe3, 0xff, 0xc7, 0xff, 0x8e, 0x03, 0x8f, 0xfe, 0x3f, 0xfe, 0x1f, 0xff, 0x1f, 0xfe,
  19. 0x3e, 0x03, 0xc3, 0xff, 0x8f, 0xff, 0x0e, 0x07, 0x8f, 0xfe, 0x7f, 0xfe, 0x1f, 0xff, 0x1f, 0xfc,
  20. 0x3e, 0x07, 0xc7, 0x80, 0x0e, 0x00, 0x0e, 0x07, 0x9e, 0x00, 0x78, 0x0e, 0x3c, 0x0f, 0x1c, 0x00,
  21. 0x3e, 0x0f, 0xc7, 0x80, 0x1e, 0x00, 0x0e, 0x07, 0x1e, 0x00, 0x70, 0x0e, 0x38, 0x0f, 0x3c, 0x00,
  22. 0x7f, 0x0f, 0xc7, 0xfe, 0x1f, 0xfc, 0x1f, 0xff, 0x1c, 0x00, 0x70, 0x0e, 0x38, 0x0e, 0x3f, 0xf8,
  23. 0x7f, 0x1f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x0e, 0x38, 0x0e, 0x3f, 0xf8,
  24. 0x7f, 0x3f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x1e, 0x3f, 0xfe, 0x3f, 0xf0,
  25. 0x77, 0x3b, 0x87, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xfc, 0x38, 0x00,
  26. 0x77, 0xfb, 0x8f, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xf8, 0x38, 0x00,
  27. 0x73, 0xf3, 0x8f, 0xff, 0x0f, 0xff, 0x1c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x78, 0x7f, 0xf8,
  28. 0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfe, 0x3c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x3c, 0x7f, 0xf8,
  29. 0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfc, 0x3c, 0x0e, 0x1f, 0xf8, 0xff, 0xf8, 0x70, 0x3c, 0x7f, 0xf8,
  30. };
  31. void UITask::begin(DisplayDriver* display, SensorManager* sensors, NodePrefs* node_prefs) {
  32. _display = display;
  33. _sensors = sensors;
  34. _auto_off = millis() + AUTO_OFF_MILLIS;
  35. clearMsgPreview();
  36. _node_prefs = node_prefs;
  37. if (_display != NULL) {
  38. _display->turnOn();
  39. }
  40. // strip off dash and commit hash by changing dash to null terminator
  41. // e.g: v1.2.3-abcdef -> v1.2.3
  42. char *version = strdup(FIRMWARE_VERSION);
  43. char *dash = strchr(version, '-');
  44. if (dash) {
  45. *dash = 0;
  46. }
  47. // v1.2.3 (1 Jan 2025)
  48. sprintf(_version_info, "%s (%s)", version, FIRMWARE_BUILD_DATE);
  49. #ifdef PIN_BUZZER
  50. buzzer.begin();
  51. buzzer.quiet(_node_prefs->buzzer_quiet);
  52. buzzer.startup();
  53. #endif
  54. // Initialize digital button if available
  55. #ifdef PIN_USER_BTN
  56. _userButton = new Button(PIN_USER_BTN, USER_BTN_PRESSED);
  57. _userButton->begin();
  58. // Set up digital button callbacks
  59. _userButton->onShortPress([this]() { handleButtonShortPress(); });
  60. _userButton->onDoublePress([this]() { handleButtonDoublePress(); });
  61. _userButton->onTriplePress([this]() { handleButtonTriplePress(); });
  62. _userButton->onQuadruplePress([this]() { handleButtonQuadruplePress(); });
  63. _userButton->onLongPress([this]() { handleButtonLongPress(); });
  64. _userButton->onAnyPress([this]() { handleButtonAnyPress(); });
  65. #endif
  66. // Initialize analog button if available
  67. #ifdef PIN_USER_BTN_ANA
  68. _userButtonAnalog = new Button(PIN_USER_BTN_ANA, USER_BTN_PRESSED, true, 20);
  69. _userButtonAnalog->begin();
  70. // Set up analog button callbacks
  71. _userButtonAnalog->onShortPress([this]() { handleButtonShortPress(); });
  72. _userButtonAnalog->onDoublePress([this]() { handleButtonDoublePress(); });
  73. _userButtonAnalog->onTriplePress([this]() { handleButtonTriplePress(); });
  74. _userButtonAnalog->onQuadruplePress([this]() { handleButtonQuadruplePress(); });
  75. _userButtonAnalog->onLongPress([this]() { handleButtonLongPress(); });
  76. _userButtonAnalog->onAnyPress([this]() { handleButtonAnyPress(); });
  77. #endif
  78. ui_started_at = millis();
  79. }
  80. void UITask::notify(UIEventType t) {
  81. #if defined(PIN_BUZZER)
  82. switch(t){
  83. case UIEventType::contactMessage:
  84. // gemini's pick
  85. buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
  86. break;
  87. case UIEventType::channelMessage:
  88. buzzer.play("kerplop:d=16,o=6,b=120:32g#,32c#");
  89. break;
  90. case UIEventType::ack:
  91. buzzer.play("ack:d=32,o=8,b=120:c");
  92. break;
  93. case UIEventType::roomMessage:
  94. case UIEventType::newContactMessage:
  95. case UIEventType::none:
  96. default:
  97. break;
  98. }
  99. #endif
  100. // Serial.print("DBG: Alert user -> ");
  101. // Serial.println((int) t);
  102. }
  103. void UITask::msgRead(int msgcount) {
  104. _msgcount = msgcount;
  105. if (msgcount == 0) {
  106. clearMsgPreview();
  107. }
  108. }
  109. void UITask::clearMsgPreview() {
  110. _origin[0] = 0;
  111. _msg[0] = 0;
  112. _need_refresh = true;
  113. }
  114. void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) {
  115. _msgcount = msgcount;
  116. if (path_len == 0xFF) {
  117. sprintf(_origin, "(F) %s", from_name);
  118. } else {
  119. sprintf(_origin, "(%d) %s", (uint32_t) path_len, from_name);
  120. }
  121. StrHelper::strncpy(_msg, text, sizeof(_msg));
  122. if (_display != NULL) {
  123. if (!_display->isOn() && !hasConnection()) {
  124. _display->turnOn();
  125. }
  126. if (_display->isOn()) {
  127. _auto_off = millis() + AUTO_OFF_MILLIS; // extend the auto-off timer
  128. _need_refresh = true;
  129. }
  130. }
  131. }
  132. void UITask::renderBatteryIndicator(uint16_t batteryMilliVolts) {
  133. // Convert millivolts to percentage
  134. #ifndef BATT_MIN_MILLIVOLTS
  135. #define BATT_MIN_MILLIVOLTS 3000
  136. #endif
  137. #ifndef BATT_MAX_MILLIVOLTS
  138. #define BATT_MAX_MILLIVOLTS 4200
  139. #endif
  140. const int minMilliVolts = BATT_MIN_MILLIVOLTS;
  141. const int maxMilliVolts = BATT_MAX_MILLIVOLTS;
  142. int batteryPercentage = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts);
  143. if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
  144. if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
  145. // battery icon
  146. int iconWidth = 24;
  147. int iconHeight = 12;
  148. int iconX = _display->width() - iconWidth - 5; // Position the icon near the top-right corner
  149. int iconY = 0;
  150. _display->setColor(DisplayDriver::GREEN);
  151. // battery outline
  152. _display->drawRect(iconX, iconY, iconWidth, iconHeight);
  153. // battery "cap"
  154. _display->fillRect(iconX + iconWidth, iconY + (iconHeight / 4), 3, iconHeight / 2);
  155. // fill the battery based on the percentage
  156. int fillWidth = (batteryPercentage * (iconWidth - 4)) / 100;
  157. _display->fillRect(iconX + 2, iconY + 2, fillWidth, iconHeight - 4);
  158. }
  159. void UITask::renderCurrScreen() {
  160. if (_display == NULL) return; // assert() ??
  161. char tmp[80];
  162. if (_alert[0]) {
  163. _display->setTextSize(1.4);
  164. uint16_t textWidth = _display->getTextWidth(_alert);
  165. _display->setCursor((_display->width() - textWidth) / 2, 22);
  166. _display->setColor(DisplayDriver::GREEN);
  167. _display->print(_alert);
  168. _alert[0] = 0;
  169. _need_refresh = true;
  170. return;
  171. } else if (_origin[0] && _msg[0]) { // message preview
  172. // render message preview
  173. _display->setCursor(0, 0);
  174. _display->setTextSize(1);
  175. _display->setColor(DisplayDriver::GREEN);
  176. _display->print(_node_prefs->node_name);
  177. _display->setCursor(0, 12);
  178. _display->setColor(DisplayDriver::YELLOW);
  179. _display->print(_origin);
  180. _display->setCursor(0, 24);
  181. _display->setColor(DisplayDriver::LIGHT);
  182. _display->print(_msg);
  183. _display->setCursor(_display->width() - 28, 9);
  184. _display->setTextSize(2);
  185. _display->setColor(DisplayDriver::ORANGE);
  186. sprintf(tmp, "%d", _msgcount);
  187. _display->print(tmp);
  188. _display->setColor(DisplayDriver::YELLOW); // last color will be kept on T114
  189. } else if ((millis() - ui_started_at) < BOOT_SCREEN_MILLIS) { // boot screen
  190. // meshcore logo
  191. _display->setColor(DisplayDriver::BLUE);
  192. int logoWidth = 128;
  193. _display->drawXbm((_display->width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
  194. // version info
  195. _display->setColor(DisplayDriver::LIGHT);
  196. _display->setTextSize(1);
  197. uint16_t textWidth = _display->getTextWidth(_version_info);
  198. _display->setCursor((_display->width() - textWidth) / 2, 22);
  199. _display->print(_version_info);
  200. } else { // home screen
  201. // node name
  202. _display->setCursor(0, 0);
  203. _display->setTextSize(1);
  204. _display->setColor(DisplayDriver::GREEN);
  205. _display->print(_node_prefs->node_name);
  206. // battery voltage
  207. renderBatteryIndicator(_board->getBattMilliVolts());
  208. // freq / sf
  209. _display->setCursor(0, 20);
  210. _display->setColor(DisplayDriver::YELLOW);
  211. sprintf(tmp, "FREQ: %06.3f SF%d", _node_prefs->freq, _node_prefs->sf);
  212. _display->print(tmp);
  213. // bw / cr
  214. _display->setCursor(0, 30);
  215. sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr);
  216. _display->print(tmp);
  217. // BT pin
  218. if (!_connected && the_mesh.getBLEPin() != 0) {
  219. _display->setColor(DisplayDriver::RED);
  220. _display->setTextSize(2);
  221. _display->setCursor(0, 43);
  222. sprintf(tmp, "Pin:%d", the_mesh.getBLEPin());
  223. _display->print(tmp);
  224. _display->setColor(DisplayDriver::GREEN);
  225. } else {
  226. _display->setColor(DisplayDriver::LIGHT);
  227. }
  228. }
  229. _need_refresh = false;
  230. }
  231. void UITask::userLedHandler() {
  232. #ifdef PIN_STATUS_LED
  233. static int state = 0;
  234. static int next_change = 0;
  235. static int last_increment = 0;
  236. int cur_time = millis();
  237. if (cur_time > next_change) {
  238. if (state == 0) {
  239. state = 1;
  240. if (_msgcount > 0) {
  241. last_increment = LED_ON_MSG_MILLIS;
  242. } else {
  243. last_increment = LED_ON_MILLIS;
  244. }
  245. next_change = cur_time + last_increment;
  246. } else {
  247. state = 0;
  248. next_change = cur_time + LED_CYCLE_MILLIS - last_increment;
  249. }
  250. digitalWrite(PIN_STATUS_LED, state == LED_STATE_ON);
  251. }
  252. #endif
  253. }
  254. /*
  255. hardware-agnostic pre-shutdown activity should be done here
  256. */
  257. void UITask::shutdown(bool restart){
  258. #ifdef PIN_BUZZER
  259. /* note: we have a choice here -
  260. we can do a blocking buzzer.loop() with non-deterministic consequences
  261. or we can set a flag and delay the shutdown for a couple of seconds
  262. while a non-blocking buzzer.loop() plays out in UITask::loop()
  263. */
  264. buzzer.shutdown();
  265. uint32_t buzzer_timer = millis(); // fail-safe shutdown
  266. while (buzzer.isPlaying() && (millis() - 2500) < buzzer_timer)
  267. buzzer.loop();
  268. #endif // PIN_BUZZER
  269. if (restart) {
  270. _board->reboot();
  271. } else {
  272. radio_driver.powerOff();
  273. _board->powerOff();
  274. }
  275. }
  276. void UITask::loop() {
  277. #ifdef PIN_USER_BTN
  278. if (_userButton) {
  279. _userButton->update();
  280. }
  281. #endif
  282. #ifdef PIN_USER_BTN_ANA
  283. if (_userButtonAnalog) {
  284. _userButtonAnalog->update();
  285. }
  286. #endif
  287. userLedHandler();
  288. #ifdef PIN_BUZZER
  289. if (buzzer.isPlaying()) buzzer.loop();
  290. #endif
  291. if (_display != NULL && _display->isOn()) {
  292. static bool _firstBoot = true;
  293. if(_firstBoot && (millis() - ui_started_at) >= BOOT_SCREEN_MILLIS) {
  294. _need_refresh = true;
  295. _firstBoot = false;
  296. }
  297. if (millis() >= _next_refresh && _need_refresh) {
  298. _display->startFrame();
  299. renderCurrScreen();
  300. _display->endFrame();
  301. _next_refresh = millis() + 1000; // refresh every second
  302. }
  303. #ifdef KEEP_DISPLAY_ON_USB
  304. // Opt-in: refresh the auto-off deadline while externally powered, so the
  305. // timer counts from the moment external power is removed. Off by default
  306. // because OLED panels burn in quickly; only enable for LCD targets or
  307. // where the display is replaceable.
  308. if (board.isExternalPowered()) {
  309. _auto_off = millis() + AUTO_OFF_MILLIS;
  310. }
  311. #endif
  312. if (millis() > _auto_off) {
  313. _display->turnOff();
  314. }
  315. }
  316. }
  317. void UITask::handleButtonAnyPress() {
  318. MESH_DEBUG_PRINTLN("UITask: any press triggered");
  319. // called on any button press before other events, to wake up the display quickly
  320. // do not refresh the display here, as it may block the button handler
  321. if (_display != NULL) {
  322. _displayWasOn = _display->isOn(); // Track display state before any action
  323. if (!_displayWasOn) {
  324. _display->turnOn();
  325. }
  326. _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
  327. }
  328. }
  329. void UITask::handleButtonShortPress() {
  330. MESH_DEBUG_PRINTLN("UITask: short press triggered");
  331. if (_display != NULL) {
  332. // Only clear message preview if display was already on before button press
  333. if (_displayWasOn) {
  334. // If display was on and showing message preview, clear it
  335. if (_origin[0] && _msg[0]) {
  336. clearMsgPreview();
  337. } else {
  338. // Otherwise, refresh the display
  339. _need_refresh = true;
  340. }
  341. } else {
  342. _need_refresh = true; // display just turned on, so we need to refresh
  343. }
  344. // Note: Display turn-on and auto-off timer extension are handled by handleButtonAnyPress
  345. }
  346. }
  347. void UITask::handleButtonDoublePress() {
  348. MESH_DEBUG_PRINTLN("UITask: double press triggered, sending advert");
  349. // ADVERT
  350. #ifdef PIN_BUZZER
  351. notify(UIEventType::ack);
  352. #endif
  353. if (the_mesh.advert()) {
  354. MESH_DEBUG_PRINTLN("Advert sent!");
  355. sprintf(_alert, "Advert sent!");
  356. } else {
  357. MESH_DEBUG_PRINTLN("Advert failed!");
  358. sprintf(_alert, "Advert failed..");
  359. }
  360. _need_refresh = true;
  361. }
  362. void UITask::handleButtonTriplePress() {
  363. MESH_DEBUG_PRINTLN("UITask: triple press triggered");
  364. // Toggle buzzer quiet mode
  365. #ifdef PIN_BUZZER
  366. if (buzzer.isQuiet()) {
  367. buzzer.quiet(false);
  368. notify(UIEventType::ack);
  369. sprintf(_alert, "Buzzer: ON");
  370. } else {
  371. buzzer.quiet(true);
  372. sprintf(_alert, "Buzzer: OFF");
  373. }
  374. _node_prefs->buzzer_quiet = buzzer.isQuiet();
  375. the_mesh.savePrefs();
  376. _need_refresh = true;
  377. #endif
  378. }
  379. void UITask::handleButtonQuadruplePress() {
  380. MESH_DEBUG_PRINTLN("UITask: quad press triggered");
  381. if (_sensors != NULL) {
  382. // toggle GPS onn/off
  383. int num = _sensors->getNumSettings();
  384. for (int i = 0; i < num; i++) {
  385. if (strcmp(_sensors->getSettingName(i), "gps") == 0) {
  386. if (strcmp(_sensors->getSettingValue(i), "1") == 0) {
  387. _sensors->setSettingValue("gps", "0");
  388. notify(UIEventType::ack);
  389. sprintf(_alert, "GPS: Disabled");
  390. } else {
  391. _sensors->setSettingValue("gps", "1");
  392. notify(UIEventType::ack);
  393. sprintf(_alert, "GPS: Enabled");
  394. }
  395. break;
  396. }
  397. }
  398. }
  399. _need_refresh = true;
  400. }
  401. void UITask::handleButtonLongPress() {
  402. MESH_DEBUG_PRINTLN("UITask: long press triggered");
  403. if (millis() - ui_started_at < 8000) { // long press in first 8 seconds since startup -> CLI/rescue
  404. the_mesh.enterCLIRescue();
  405. } else {
  406. shutdown();
  407. }
  408. }