UITask.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "UITask.h"
  2. #include <Arduino.h>
  3. #include <helpers/TxtDataHelpers.h>
  4. #define AUTO_OFF_MILLIS 15000 // 15 seconds
  5. #ifndef USER_BTN_PRESSED
  6. #define USER_BTN_PRESSED LOW
  7. #endif
  8. // 'meshcore', 128x13px
  9. static const uint8_t meshcore_logo [] PROGMEM = {
  10. 0x3c, 0x01, 0xe3, 0xff, 0xc7, 0xff, 0x8f, 0x03, 0x87, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe,
  11. 0x3c, 0x03, 0xe3, 0xff, 0xc7, 0xff, 0x8e, 0x03, 0x8f, 0xfe, 0x3f, 0xfe, 0x1f, 0xff, 0x1f, 0xfe,
  12. 0x3e, 0x03, 0xc3, 0xff, 0x8f, 0xff, 0x0e, 0x07, 0x8f, 0xfe, 0x7f, 0xfe, 0x1f, 0xff, 0x1f, 0xfc,
  13. 0x3e, 0x07, 0xc7, 0x80, 0x0e, 0x00, 0x0e, 0x07, 0x9e, 0x00, 0x78, 0x0e, 0x3c, 0x0f, 0x1c, 0x00,
  14. 0x3e, 0x0f, 0xc7, 0x80, 0x1e, 0x00, 0x0e, 0x07, 0x1e, 0x00, 0x70, 0x0e, 0x38, 0x0f, 0x3c, 0x00,
  15. 0x7f, 0x0f, 0xc7, 0xfe, 0x1f, 0xfc, 0x1f, 0xff, 0x1c, 0x00, 0x70, 0x0e, 0x38, 0x0e, 0x3f, 0xf8,
  16. 0x7f, 0x1f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x0e, 0x38, 0x0e, 0x3f, 0xf8,
  17. 0x7f, 0x3f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x1e, 0x3f, 0xfe, 0x3f, 0xf0,
  18. 0x77, 0x3b, 0x87, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xfc, 0x38, 0x00,
  19. 0x77, 0xfb, 0x8f, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xf8, 0x38, 0x00,
  20. 0x73, 0xf3, 0x8f, 0xff, 0x0f, 0xff, 0x1c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x78, 0x7f, 0xf8,
  21. 0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfe, 0x3c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x3c, 0x7f, 0xf8,
  22. 0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfc, 0x3c, 0x0e, 0x1f, 0xf8, 0xff, 0xf8, 0x70, 0x3c, 0x7f, 0xf8,
  23. };
  24. void UITask::begin(DisplayDriver* display, const char* node_name, const char* build_date, const char* firmware_version, uint32_t pin_code) {
  25. _display = display;
  26. _auto_off = millis() + AUTO_OFF_MILLIS;
  27. clearMsgPreview();
  28. _node_name = node_name;
  29. _pin_code = pin_code;
  30. if (_display != NULL) {
  31. _display->turnOn();
  32. }
  33. // strip off dash and commit hash by changing dash to null terminator
  34. // e.g: v1.2.3-abcdef -> v1.2.3
  35. char *version = strdup(firmware_version);
  36. char *dash = strchr(version, '-');
  37. if(dash){
  38. *dash = 0;
  39. }
  40. #ifdef PIN_USER_BTN
  41. pinMode(PIN_USER_BTN, INPUT);
  42. #endif
  43. // v1.2.3 (1 Jan 2025)
  44. sprintf(_version_info, "%s (%s)", version, build_date);
  45. }
  46. void UITask::msgRead(int msgcount) {
  47. _msgcount = msgcount;
  48. if (msgcount == 0) {
  49. clearMsgPreview();
  50. }
  51. }
  52. void UITask::clearMsgPreview() {
  53. _origin[0] = 0;
  54. _msg[0] = 0;
  55. _need_refresh = true;
  56. }
  57. void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) {
  58. _msgcount = msgcount;
  59. if (path_len == 0xFF) {
  60. sprintf(_origin, "(F) %s", from_name);
  61. } else {
  62. sprintf(_origin, "(%d) %s", (uint32_t) path_len, from_name);
  63. }
  64. StrHelper::strncpy(_msg, text, sizeof(_msg));
  65. if (_display != NULL) {
  66. if (!_display->isOn()) _display->turnOn();
  67. _auto_off = millis() + AUTO_OFF_MILLIS; // extend the auto-off timer
  68. _need_refresh = true;
  69. }
  70. }
  71. void UITask::renderCurrScreen() {
  72. if (_display == NULL) return; // assert() ??
  73. char tmp[80];
  74. if (_origin[0] && _msg[0]) {
  75. // render message preview
  76. _display->setCursor(0, 0);
  77. _display->setTextSize(1);
  78. _display->setColor(DisplayDriver::GREEN);
  79. _display->print(_node_name);
  80. _display->setCursor(0, 12);
  81. _display->setColor(DisplayDriver::YELLOW);
  82. _display->print(_origin);
  83. _display->setCursor(0, 24);
  84. _display->setColor(DisplayDriver::LIGHT);
  85. _display->print(_msg);
  86. _display->setCursor(100, 9);
  87. _display->setTextSize(2);
  88. _display->setColor(DisplayDriver::ORANGE);
  89. sprintf(tmp, "%d", _msgcount);
  90. _display->print(tmp);
  91. } else {
  92. // render 'home' screen
  93. _display->setColor(DisplayDriver::BLUE);
  94. _display->drawXbm(0, 0, meshcore_logo, 128, 13);
  95. _display->setCursor(0, 20);
  96. _display->setTextSize(1);
  97. _display->setColor(DisplayDriver::LIGHT);
  98. _display->print(_node_name);
  99. _display->setCursor(0, 32);
  100. _display->print(_version_info);
  101. if (_connected) {
  102. //_display->printf("freq : %03.2f sf %d\n", _prefs.freq, _prefs.sf);
  103. //_display->printf("bw : %03.2f cr %d\n", _prefs.bw, _prefs.cr);
  104. } else if (_pin_code != 0) {
  105. _display->setColor(DisplayDriver::RED);
  106. _display->setTextSize(2);
  107. _display->setCursor(0, 43);
  108. sprintf(tmp, "Pin:%d", _pin_code);
  109. _display->print(tmp);
  110. }
  111. }
  112. _need_refresh = false;
  113. }
  114. void UITask::userLedHandler() {
  115. #ifdef PIN_STATUS_LED
  116. static int state = 0;
  117. static int next_change = 0;
  118. int cur_time = millis();
  119. if (cur_time > next_change) {
  120. if (state == 0) {
  121. state = 1; // led on, short = unread msg
  122. if (_msgcount > 0) {
  123. next_change = cur_time + 500;
  124. } else {
  125. next_change = cur_time + 2000;
  126. }
  127. } else {
  128. state = 0;
  129. if (_board->getBattMilliVolts() > 3800) {
  130. next_change = cur_time + 2000;
  131. } else {
  132. next_change = cur_time + 4000; // 4s blank if bat level low
  133. }
  134. }
  135. digitalWrite(PIN_STATUS_LED, state);
  136. }
  137. #endif
  138. }
  139. void UITask::buttonHandler() {
  140. #ifdef PIN_USER_BTN
  141. static int prev_btn_state = !USER_BTN_PRESSED;
  142. static unsigned long btn_state_change_time = 0;
  143. static unsigned long next_read = 0;
  144. int cur_time = millis();
  145. if (cur_time >= next_read) {
  146. int btn_state = digitalRead(PIN_USER_BTN);
  147. if (btn_state != prev_btn_state) {
  148. if (btn_state == USER_BTN_PRESSED) { // pressed?
  149. if (_display != NULL) {
  150. if (_display->isOn()) {
  151. clearMsgPreview();
  152. } else {
  153. _display->turnOn();
  154. _need_refresh = true;
  155. }
  156. _auto_off = cur_time + AUTO_OFF_MILLIS; // extend auto-off timer
  157. }
  158. } else { // unpressed ? check pressed time ...
  159. if ((cur_time - btn_state_change_time) > 5000) {
  160. #ifdef PIN_STATUS_LED
  161. digitalWrite(PIN_STATUS_LED, LOW);
  162. delay(10);
  163. #endif
  164. _board->powerOff();
  165. }
  166. }
  167. btn_state_change_time = millis();
  168. prev_btn_state = btn_state;
  169. }
  170. next_read = millis() + 100; // 10 reads per second
  171. }
  172. #endif
  173. }
  174. void UITask::loop() {
  175. buttonHandler();
  176. userLedHandler();
  177. if (_display != NULL && _display->isOn()) {
  178. if (millis() >= _next_refresh && _need_refresh) {
  179. _display->startFrame();
  180. renderCurrScreen();
  181. _display->endFrame();
  182. _next_refresh = millis() + 1000; // refresh every second
  183. }
  184. if (millis() > _auto_off) {
  185. _display->turnOff();
  186. }
  187. }
  188. }