UITask.cpp 13 KB

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