UITask.cpp 8.5 KB

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