UITask.cpp 13 KB

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