UITask.cpp 14 KB

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