UITask.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // v1.2.3 (1 Jan 2025)
  41. sprintf(_version_info, "%s (%s)", version, build_date);
  42. }
  43. void UITask::msgRead(int msgcount) {
  44. _msgcount = msgcount;
  45. if (msgcount == 0) {
  46. clearMsgPreview();
  47. }
  48. }
  49. void UITask::clearMsgPreview() {
  50. _origin[0] = 0;
  51. _msg[0] = 0;
  52. }
  53. void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) {
  54. _msgcount = msgcount;
  55. if (path_len == 0xFF) {
  56. sprintf(_origin, "(F) %s", from_name);
  57. } else {
  58. sprintf(_origin, "(%d) %s", (uint32_t) path_len, from_name);
  59. }
  60. StrHelper::strncpy(_msg, text, sizeof(_msg));
  61. if (_display != NULL) {
  62. if (!_display->isOn()) _display->turnOn();
  63. _auto_off = millis() + AUTO_OFF_MILLIS; // extend the auto-off timer
  64. }
  65. }
  66. void UITask::renderCurrScreen() {
  67. if (_display == NULL) return; // assert() ??
  68. char tmp[80];
  69. if (_origin[0] && _msg[0]) {
  70. // render message preview
  71. _display->setCursor(0, 0);
  72. _display->setTextSize(1);
  73. _display->print(_node_name);
  74. _display->setCursor(0, 12);
  75. _display->print(_origin);
  76. _display->setCursor(0, 24);
  77. _display->print(_msg);
  78. _display->setCursor(100, 9);
  79. _display->setTextSize(2);
  80. sprintf(tmp, "%d", _msgcount);
  81. _display->print(tmp);
  82. } else {
  83. // render 'home' screen
  84. _display->drawXbm(0, 0, meshcore_logo, 128, 13);
  85. _display->setCursor(0, 20);
  86. _display->setTextSize(1);
  87. _display->print(_node_name);
  88. _display->setCursor(0, 32);
  89. _display->print(_version_info);
  90. if (_connected) {
  91. //_display->printf("freq : %03.2f sf %d\n", _prefs.freq, _prefs.sf);
  92. //_display->printf("bw : %03.2f cr %d\n", _prefs.bw, _prefs.cr);
  93. } else if (_pin_code != 0) {
  94. _display->setTextSize(2);
  95. _display->setCursor(0, 43);
  96. sprintf(tmp, "Pin:%d", _pin_code);
  97. _display->print(tmp);
  98. }
  99. }
  100. }
  101. void UITask::userLedHandler() {
  102. #ifdef PIN_STATUS_LED
  103. static int state = 0;
  104. static int next_change = 0;
  105. int cur_time = millis();
  106. if (cur_time > next_change) {
  107. if (state == 0) {
  108. state = 1; // led on, short = unread msg
  109. if (_msgcount > 0) {
  110. next_change = cur_time + 500;
  111. } else {
  112. next_change = cur_time + 2000;
  113. }
  114. } else {
  115. state = 0;
  116. if (_board->getBattMilliVolts() > 3800) {
  117. next_change = cur_time + 2000;
  118. } else {
  119. next_change = cur_time + 4000; // 4s blank if bat level low
  120. }
  121. }
  122. digitalWrite(PIN_STATUS_LED, state);
  123. }
  124. #endif
  125. }
  126. void UITask::buttonHandler() {
  127. #ifdef PIN_USER_BTN
  128. static int prev_btn_state = !USER_BTN_PRESSED;
  129. static unsigned long btn_state_change_time = 0;
  130. static unsigned long next_read = 0;
  131. int cur_time = millis();
  132. if (cur_time >= next_read) {
  133. int btn_state = digitalRead(PIN_USER_BTN);
  134. if (btn_state != prev_btn_state) {
  135. if (btn_state == USER_BTN_PRESSED) { // pressed?
  136. if (_display != NULL) {
  137. if (_display->isOn()) {
  138. clearMsgPreview();
  139. } else {
  140. _display->turnOn();
  141. }
  142. _auto_off = cur_time + AUTO_OFF_MILLIS; // extend auto-off timer
  143. }
  144. } else { // unpressed ? check pressed time ...
  145. if ((cur_time - btn_state_change_time) > 5000) {
  146. #ifdef PIN_STATUS_LED
  147. digitalWrite(PIN_STATUS_LED, LOW);
  148. delay(10);
  149. #endif
  150. _board->powerOff();
  151. }
  152. }
  153. btn_state_change_time = millis();
  154. prev_btn_state = btn_state;
  155. }
  156. next_read = millis() + 100; // 10 reads per second
  157. }
  158. #endif
  159. }
  160. void UITask::loop() {
  161. buttonHandler();
  162. userLedHandler();
  163. if (_display != NULL && _display->isOn()) {
  164. if (millis() >= _next_refresh) {
  165. _display->startFrame();
  166. renderCurrScreen();
  167. _display->endFrame();
  168. _next_refresh = millis() + 1000; // refresh every second
  169. }
  170. if (millis() > _auto_off) {
  171. _display->turnOff();
  172. }
  173. }
  174. }