UITask.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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, const char* build_date, const char* firmware_version, uint32_t pin_code) {
  33. _display = display;
  34. _auto_off = millis() + AUTO_OFF_MILLIS;
  35. clearMsgPreview();
  36. _node_prefs = node_prefs;
  37. _pin_code = pin_code;
  38. if (_display != NULL) {
  39. _display->turnOn();
  40. }
  41. // strip off dash and commit hash by changing dash to null terminator
  42. // e.g: v1.2.3-abcdef -> v1.2.3
  43. char *version = strdup(firmware_version);
  44. char *dash = strchr(version, '-');
  45. if(dash){
  46. *dash = 0;
  47. }
  48. // v1.2.3 (1 Jan 2025)
  49. sprintf(_version_info, "%s (%s)", version, build_date);
  50. #ifdef PIN_BUZZER
  51. buzzer.begin();
  52. #endif
  53. // Initialize button with appropriate configuration
  54. #if defined(PIN_USER_BTN) || defined(PIN_USER_BTN_ANA)
  55. #ifdef PIN_USER_BTN
  56. _userButton = new Button(PIN_USER_BTN, USER_BTN_PRESSED);
  57. #else
  58. _userButton = new Button(PIN_USER_BTN_ANA, USER_BTN_PRESSED, true, 20);
  59. #endif
  60. _userButton->begin();
  61. // Set up button callbacks
  62. _userButton->onShortPress([this]() { handleButtonShortPress(); });
  63. _userButton->onDoublePress([this]() { handleButtonDoublePress(); });
  64. _userButton->onTriplePress([this]() { handleButtonTriplePress(); });
  65. _userButton->onLongPress([this]() { handleButtonLongPress(); });
  66. _userButton->onAnyPress([this]() { handleButtonAnyPress(); });
  67. #endif
  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 (_origin[0] && _msg[0]) { // message preview
  142. // render message preview
  143. _display->setCursor(0, 0);
  144. _display->setTextSize(1);
  145. _display->setColor(DisplayDriver::GREEN);
  146. _display->print(_node_prefs->node_name);
  147. _display->setCursor(0, 12);
  148. _display->setColor(DisplayDriver::YELLOW);
  149. _display->print(_origin);
  150. _display->setCursor(0, 24);
  151. _display->setColor(DisplayDriver::LIGHT);
  152. _display->print(_msg);
  153. _display->setCursor(_display->width() - 28, 9);
  154. _display->setTextSize(2);
  155. _display->setColor(DisplayDriver::ORANGE);
  156. sprintf(tmp, "%d", _msgcount);
  157. _display->print(tmp);
  158. _display->setColor(DisplayDriver::YELLOW); // last color will be kept on T114
  159. } else if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
  160. // meshcore logo
  161. _display->setColor(DisplayDriver::BLUE);
  162. int logoWidth = 128;
  163. _display->drawXbm((_display->width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
  164. // version info
  165. _display->setColor(DisplayDriver::LIGHT);
  166. _display->setTextSize(1);
  167. uint16_t textWidth = _display->getTextWidth(_version_info);
  168. _display->setCursor((_display->width() - textWidth) / 2, 22);
  169. _display->print(_version_info);
  170. } else { // home screen
  171. // node name
  172. _display->setCursor(0, 0);
  173. _display->setTextSize(1);
  174. _display->setColor(DisplayDriver::GREEN);
  175. _display->print(_node_prefs->node_name);
  176. // battery voltage
  177. renderBatteryIndicator(_board->getBattMilliVolts());
  178. // freq / sf
  179. _display->setCursor(0, 20);
  180. _display->setColor(DisplayDriver::YELLOW);
  181. sprintf(tmp, "FREQ: %06.3f SF%d", _node_prefs->freq, _node_prefs->sf);
  182. _display->print(tmp);
  183. // bw / cr
  184. _display->setCursor(0, 30);
  185. sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr);
  186. _display->print(tmp);
  187. // BT pin
  188. if (!_connected && _pin_code != 0) {
  189. _display->setColor(DisplayDriver::RED);
  190. _display->setTextSize(2);
  191. _display->setCursor(0, 43);
  192. sprintf(tmp, "Pin:%d", _pin_code);
  193. _display->print(tmp);
  194. _display->setColor(DisplayDriver::GREEN);
  195. } else {
  196. _display->setColor(DisplayDriver::LIGHT);
  197. }
  198. }
  199. _need_refresh = false;
  200. }
  201. void UITask::userLedHandler() {
  202. #ifdef PIN_STATUS_LED
  203. static int state = 0;
  204. static int next_change = 0;
  205. static int last_increment = 0;
  206. int cur_time = millis();
  207. if (cur_time > next_change) {
  208. if (state == 0) {
  209. state = 1;
  210. if (_msgcount > 0) {
  211. last_increment = LED_ON_MSG_MILLIS;
  212. } else {
  213. last_increment = LED_ON_MILLIS;
  214. }
  215. next_change = cur_time + last_increment;
  216. } else {
  217. state = 0;
  218. next_change = cur_time + LED_CYCLE_MILLIS - last_increment;
  219. }
  220. digitalWrite(PIN_STATUS_LED, state);
  221. }
  222. #endif
  223. }
  224. /*
  225. hardware-agnostic pre-shutdown activity should be done here
  226. */
  227. void UITask::shutdown(bool restart){
  228. #ifdef PIN_BUZZER
  229. /* note: we have a choice here -
  230. we can do a blocking buzzer.loop() with non-deterministic consequences
  231. or we can set a flag and delay the shutdown for a couple of seconds
  232. while a non-blocking buzzer.loop() plays out in UITask::loop()
  233. */
  234. buzzer.shutdown();
  235. uint32_t buzzer_timer = millis(); // fail-safe shutdown
  236. while (buzzer.isPlaying() && (millis() - 2500) < buzzer_timer)
  237. buzzer.loop();
  238. #endif // PIN_BUZZER
  239. if (restart)
  240. _board->reboot();
  241. else
  242. _board->powerOff();
  243. }
  244. void UITask::loop() {
  245. #if defined(PIN_USER_BTN) || defined(PIN_USER_BTN_ANA)
  246. if (_userButton) {
  247. _userButton->update();
  248. }
  249. #endif
  250. userLedHandler();
  251. #ifdef PIN_BUZZER
  252. if (buzzer.isPlaying()) buzzer.loop();
  253. #endif
  254. if (_display != NULL && _display->isOn()) {
  255. static bool _firstBoot = true;
  256. if(_firstBoot && millis() >= BOOT_SCREEN_MILLIS) {
  257. _need_refresh = true;
  258. _firstBoot = false;
  259. }
  260. if (millis() >= _next_refresh && _need_refresh) {
  261. _display->startFrame();
  262. renderCurrScreen();
  263. _display->endFrame();
  264. _next_refresh = millis() + 1000; // refresh every second
  265. }
  266. if (millis() > _auto_off) {
  267. _display->turnOff();
  268. }
  269. }
  270. }
  271. void UITask::handleButtonAnyPress() {
  272. MESH_DEBUG_PRINTLN("UITask: any press triggered");
  273. // called on any button press before other events, to wake up the display quickly
  274. // do not refresh the display here, as it may block the button handler
  275. if (_display != NULL) {
  276. _displayWasOn = _display->isOn(); // Track display state before any action
  277. if (!_displayWasOn) {
  278. _display->turnOn();
  279. }
  280. _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
  281. }
  282. }
  283. void UITask::handleButtonShortPress() {
  284. MESH_DEBUG_PRINTLN("UITask: short press triggered");
  285. if (_display != NULL) {
  286. // Only clear message preview if display was already on before button press
  287. if (_displayWasOn) {
  288. // If display was on and showing message preview, clear it
  289. if (_origin[0] && _msg[0]) {
  290. clearMsgPreview();
  291. } else {
  292. // Otherwise, refresh the display
  293. _need_refresh = true;
  294. }
  295. }
  296. // Note: Display turn-on and auto-off timer extension are handled by handleButtonAnyPress
  297. }
  298. }
  299. void UITask::handleButtonDoublePress() {
  300. MESH_DEBUG_PRINTLN("UITask: double press triggered, sending advert");
  301. // ADVERT
  302. if(the_mesh.advert()) {
  303. MESH_DEBUG_PRINTLN("Advert sent!");
  304. }
  305. else {
  306. MESH_DEBUG_PRINTLN("Advert failed!");
  307. }
  308. }
  309. void UITask::handleButtonTriplePress() {
  310. MESH_DEBUG_PRINTLN("UITask: triple press triggered");
  311. // Toggle buzzer quiet mode
  312. #ifdef PIN_BUZZER
  313. if (buzzer.isQuiet()) {
  314. buzzer.quiet(false);
  315. soundBuzzer(UIEventType::ack);
  316. } else {
  317. soundBuzzer(UIEventType::ack);
  318. buzzer.quiet(true);
  319. }
  320. #endif
  321. }
  322. void UITask::handleButtonLongPress() {
  323. MESH_DEBUG_PRINTLN("UITask: long press triggered");
  324. shutdown();
  325. }