UITask.cpp 13 KB

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