UITask.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "UITask.h"
  2. #include <Arduino.h>
  3. #include <helpers/TxtDataHelpers.h>
  4. #include "NodePrefs.h"
  5. #define AUTO_OFF_MILLIS 15000 // 15 seconds
  6. #define BOOT_SCREEN_MILLIS 4000 // 4 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, NodePrefs* node_prefs, const char* build_date, const char* firmware_version, uint32_t pin_code) {
  32. _display = display;
  33. _auto_off = millis() + AUTO_OFF_MILLIS;
  34. clearMsgPreview();
  35. _node_prefs = node_prefs;
  36. _pin_code = pin_code;
  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, build_date);
  49. #ifdef PIN_BUZZER
  50. buzzer.begin();
  51. #endif
  52. }
  53. void UITask::soundBuzzer() {
  54. #if defined(PIN_BUZZER)
  55. // gemini's pick
  56. buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
  57. //Serial.println("DBG: Buzzzzzz");
  58. #endif
  59. }
  60. void UITask::msgRead(int msgcount) {
  61. _msgcount = msgcount;
  62. if (msgcount == 0) {
  63. clearMsgPreview();
  64. }
  65. }
  66. void UITask::clearMsgPreview() {
  67. _origin[0] = 0;
  68. _msg[0] = 0;
  69. _need_refresh = true;
  70. }
  71. void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) {
  72. _msgcount = msgcount;
  73. if (path_len == 0xFF) {
  74. sprintf(_origin, "(F) %s", from_name);
  75. } else {
  76. sprintf(_origin, "(%d) %s", (uint32_t) path_len, from_name);
  77. }
  78. StrHelper::strncpy(_msg, text, sizeof(_msg));
  79. if (_display != NULL) {
  80. if (!_display->isOn()) _display->turnOn();
  81. _auto_off = millis() + AUTO_OFF_MILLIS; // extend the auto-off timer
  82. _need_refresh = true;
  83. }
  84. }
  85. void UITask::renderBatteryIndicator(uint16_t batteryMilliVolts) {
  86. // Convert millivolts to percentage
  87. const int minMilliVolts = 3000; // Minimum voltage (e.g., 3.0V)
  88. const int maxMilliVolts = 4200; // Maximum voltage (e.g., 4.2V)
  89. int batteryPercentage = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts);
  90. if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
  91. if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
  92. // battery icon
  93. int iconWidth = 24;
  94. int iconHeight = 12;
  95. int iconX = _display->width() - iconWidth - 5; // Position the icon near the top-right corner
  96. int iconY = 0;
  97. _display->setColor(DisplayDriver::GREEN);
  98. // battery outline
  99. _display->drawRect(iconX, iconY, iconWidth, iconHeight);
  100. // battery "cap"
  101. _display->fillRect(iconX + iconWidth, iconY + (iconHeight / 4), 3, iconHeight / 2);
  102. // fill the battery based on the percentage
  103. int fillWidth = (batteryPercentage * (iconWidth - 4)) / 100;
  104. _display->fillRect(iconX + 2, iconY + 2, fillWidth, iconHeight - 4);
  105. }
  106. void UITask::renderCurrScreen() {
  107. if (_display == NULL) return; // assert() ??
  108. char tmp[80];
  109. if (_origin[0] && _msg[0]) { // message preview
  110. // render message preview
  111. _display->setCursor(0, 0);
  112. _display->setTextSize(1);
  113. _display->setColor(DisplayDriver::GREEN);
  114. _display->print(_node_prefs->node_name);
  115. _display->setCursor(0, 12);
  116. _display->setColor(DisplayDriver::YELLOW);
  117. _display->print(_origin);
  118. _display->setCursor(0, 24);
  119. _display->setColor(DisplayDriver::LIGHT);
  120. _display->print(_msg);
  121. _display->setCursor(_display->width() - 28, 9);
  122. _display->setTextSize(2);
  123. _display->setColor(DisplayDriver::ORANGE);
  124. sprintf(tmp, "%d", _msgcount);
  125. _display->print(tmp);
  126. _display->setColor(DisplayDriver::YELLOW); // last color will be kept on T114
  127. } else if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
  128. // meshcore logo
  129. _display->setColor(DisplayDriver::BLUE);
  130. int logoWidth = 128;
  131. _display->drawXbm((_display->width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
  132. // version info
  133. _display->setColor(DisplayDriver::LIGHT);
  134. _display->setTextSize(1);
  135. uint16_t textWidth = _display->getTextWidth(_version_info);
  136. _display->setCursor((_display->width() - textWidth) / 2, 22);
  137. _display->print(_version_info);
  138. } else { // home screen
  139. // node name
  140. _display->setCursor(0, 0);
  141. _display->setTextSize(1);
  142. _display->setColor(DisplayDriver::GREEN);
  143. _display->print(_node_prefs->node_name);
  144. // battery voltage
  145. renderBatteryIndicator(_board->getBattMilliVolts());
  146. // freq / sf
  147. _display->setCursor(0, 20);
  148. _display->setColor(DisplayDriver::YELLOW);
  149. sprintf(tmp, "FREQ: %06.3f SF%d", _node_prefs->freq, _node_prefs->sf);
  150. _display->print(tmp);
  151. // bw / cr
  152. _display->setCursor(0, 30);
  153. sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr);
  154. _display->print(tmp);
  155. // BT pin
  156. if (!_connected && _pin_code != 0) {
  157. _display->setColor(DisplayDriver::RED);
  158. _display->setTextSize(2);
  159. _display->setCursor(0, 43);
  160. sprintf(tmp, "Pin:%d", _pin_code);
  161. _display->print(tmp);
  162. _display->setColor(DisplayDriver::GREEN);
  163. } else {
  164. _display->setColor(DisplayDriver::LIGHT);
  165. }
  166. }
  167. _need_refresh = false;
  168. }
  169. void UITask::userLedHandler() {
  170. #ifdef PIN_STATUS_LED
  171. static int state = 0;
  172. static int next_change = 0;
  173. static int last_increment = 0;
  174. int cur_time = millis();
  175. if (cur_time > next_change) {
  176. if (state == 0) {
  177. state = 1;
  178. if (_msgcount > 0) {
  179. last_increment = LED_ON_MSG_MILLIS;
  180. } else {
  181. last_increment = LED_ON_MILLIS;
  182. }
  183. next_change = cur_time + last_increment;
  184. } else {
  185. state = 0;
  186. next_change = cur_time + LED_CYCLE_MILLIS - last_increment;
  187. }
  188. digitalWrite(PIN_STATUS_LED, state);
  189. }
  190. #endif
  191. }
  192. void UITask::buttonHandler() {
  193. #ifdef PIN_USER_BTN
  194. static int prev_btn_state = !USER_BTN_PRESSED;
  195. static unsigned long btn_state_change_time = 0;
  196. static unsigned long next_read = 0;
  197. int cur_time = millis();
  198. if (cur_time >= next_read) {
  199. int btn_state = digitalRead(PIN_USER_BTN);
  200. if (btn_state != prev_btn_state) {
  201. if (btn_state == USER_BTN_PRESSED) { // pressed?
  202. if (_display != NULL) {
  203. if (_display->isOn()) {
  204. clearMsgPreview();
  205. } else {
  206. _display->turnOn();
  207. _need_refresh = true;
  208. }
  209. _auto_off = cur_time + AUTO_OFF_MILLIS; // extend auto-off timer
  210. }
  211. } else { // unpressed ? check pressed time ...
  212. if ((cur_time - btn_state_change_time) > 5000) {
  213. #ifdef PIN_STATUS_LED
  214. digitalWrite(PIN_STATUS_LED, LOW);
  215. delay(10);
  216. #endif
  217. _board->powerOff();
  218. }
  219. }
  220. btn_state_change_time = millis();
  221. prev_btn_state = btn_state;
  222. }
  223. next_read = millis() + 100; // 10 reads per second
  224. }
  225. #endif
  226. }
  227. void UITask::loop() {
  228. buttonHandler();
  229. userLedHandler();
  230. #ifdef PIN_BUZZER
  231. if (buzzer.isPlaying()) buzzer.loop();
  232. #endif
  233. if (_display != NULL && _display->isOn()) {
  234. static bool _firstBoot = true;
  235. if(_firstBoot && millis() >= BOOT_SCREEN_MILLIS) {
  236. _need_refresh = true;
  237. _firstBoot = false;
  238. }
  239. if (millis() >= _next_refresh && _need_refresh) {
  240. _display->startFrame();
  241. renderCurrScreen();
  242. _display->endFrame();
  243. _next_refresh = millis() + 1000; // refresh every second
  244. }
  245. if (millis() > _auto_off) {
  246. _display->turnOff();
  247. }
  248. }
  249. }