UITask.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "UITask.h"
  2. #include <Arduino.h>
  3. #include <helpers/TxtDataHelpers.h>
  4. #define AUTO_OFF_MILLIS 15000 // 15 seconds
  5. // 'meshcore', 128x13px
  6. static const uint8_t meshcore_logo [] PROGMEM = {
  7. 0x3c, 0x01, 0xe3, 0xff, 0xc7, 0xff, 0x8f, 0x03, 0x87, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe,
  8. 0x3c, 0x03, 0xe3, 0xff, 0xc7, 0xff, 0x8e, 0x03, 0x8f, 0xfe, 0x3f, 0xfe, 0x1f, 0xff, 0x1f, 0xfe,
  9. 0x3e, 0x03, 0xc3, 0xff, 0x8f, 0xff, 0x0e, 0x07, 0x8f, 0xfe, 0x7f, 0xfe, 0x1f, 0xff, 0x1f, 0xfc,
  10. 0x3e, 0x07, 0xc7, 0x80, 0x0e, 0x00, 0x0e, 0x07, 0x9e, 0x00, 0x78, 0x0e, 0x3c, 0x0f, 0x1c, 0x00,
  11. 0x3e, 0x0f, 0xc7, 0x80, 0x1e, 0x00, 0x0e, 0x07, 0x1e, 0x00, 0x70, 0x0e, 0x38, 0x0f, 0x3c, 0x00,
  12. 0x7f, 0x0f, 0xc7, 0xfe, 0x1f, 0xfc, 0x1f, 0xff, 0x1c, 0x00, 0x70, 0x0e, 0x38, 0x0e, 0x3f, 0xf8,
  13. 0x7f, 0x1f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x0e, 0x38, 0x0e, 0x3f, 0xf8,
  14. 0x7f, 0x3f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x1e, 0x3f, 0xfe, 0x3f, 0xf0,
  15. 0x77, 0x3b, 0x87, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xfc, 0x38, 0x00,
  16. 0x77, 0xfb, 0x8f, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xf8, 0x38, 0x00,
  17. 0x73, 0xf3, 0x8f, 0xff, 0x0f, 0xff, 0x1c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x78, 0x7f, 0xf8,
  18. 0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfe, 0x3c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x3c, 0x7f, 0xf8,
  19. 0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfc, 0x3c, 0x0e, 0x1f, 0xf8, 0xff, 0xf8, 0x70, 0x3c, 0x7f, 0xf8,
  20. };
  21. void UITask::begin(const char* node_name, const char* build_date, uint32_t pin_code) {
  22. _prevBtnState = HIGH;
  23. _auto_off = millis() + AUTO_OFF_MILLIS;
  24. clearMsgPreview();
  25. _node_name = node_name;
  26. _build_date = build_date;
  27. _pin_code = pin_code;
  28. _display->turnOn();
  29. }
  30. void UITask::clearMsgPreview() {
  31. _origin[0] = 0;
  32. _msg[0] = 0;
  33. }
  34. void UITask::showMsgPreview(uint8_t path_len, const char* from_name, const char* text) {
  35. if (path_len == 0xFF) {
  36. sprintf(_origin, "(F) %s", from_name);
  37. } else {
  38. sprintf(_origin, "(%d) %s", (uint32_t) path_len, from_name);
  39. }
  40. StrHelper::strncpy(_msg, text, sizeof(_msg));
  41. if (!_display->isOn()) _display->turnOn();
  42. _auto_off = millis() + AUTO_OFF_MILLIS; // extend the auto-off timer
  43. }
  44. void UITask::renderCurrScreen() {
  45. char tmp[80];
  46. if (_origin[0] && _msg[0]) {
  47. // render message preview
  48. _display->setCursor(0, 0);
  49. _display->setTextSize(1);
  50. _display->print(_node_name);
  51. _display->setCursor(0, 12);
  52. _display->print(_origin);
  53. _display->setCursor(0, 24);
  54. _display->print(_msg);
  55. //_display->setCursor(100, 9); TODO
  56. //_display->setTextSize(2);
  57. //_display->printf("%d", msgs);
  58. } else {
  59. // render 'home' screen
  60. _display->drawXbm(0, 0, meshcore_logo, 128, 13);
  61. _display->setCursor(0, 20);
  62. _display->setTextSize(1);
  63. _display->print(_node_name);
  64. sprintf(tmp, "Build: %s", _build_date);
  65. _display->setCursor(0, 32);
  66. _display->print(tmp);
  67. if (_connected) {
  68. //_display->printf("freq : %03.2f sf %d\n", _prefs.freq, _prefs.sf);
  69. //_display->printf("bw : %03.2f cr %d\n", _prefs.bw, _prefs.cr);
  70. } else if (_pin_code != 0) {
  71. _display->setTextSize(2);
  72. _display->setCursor(0, 43);
  73. sprintf(tmp, "Pin:%d", _pin_code);
  74. _display->print(tmp);
  75. }
  76. }
  77. }
  78. void UITask::loop() {
  79. if (millis() >= _next_read) {
  80. int btnState = digitalRead(PIN_USER_BTN);
  81. if (btnState != _prevBtnState) {
  82. if (btnState == LOW) { // pressed?
  83. if (_display->isOn()) {
  84. clearMsgPreview();
  85. } else {
  86. _display->turnOn();
  87. }
  88. _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
  89. }
  90. _prevBtnState = btnState;
  91. }
  92. _next_read = millis() + 100; // 10 reads per second
  93. }
  94. if (_display->isOn()) {
  95. if (millis() >= _next_refresh) {
  96. _display->startFrame();
  97. renderCurrScreen();
  98. _display->endFrame();
  99. _next_refresh = millis() + 1000; // refresh every second
  100. }
  101. if (millis() > _auto_off) {
  102. _display->turnOff();
  103. }
  104. }
  105. }