UITask.cpp 9.1 KB

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