UITask.cpp 12 KB

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