UITask.cpp 11 KB

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