UITask.cpp 5.6 KB

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