|
| 1 | +#include "WiFi.h" |
| 2 | + |
| 3 | +#include "display.h" |
| 4 | +#include "time.h" |
| 5 | +#include "wifi.h" |
| 6 | + |
| 7 | +#include "advent_pro_regular.h" |
| 8 | + |
| 9 | +Display::Display() : _state(Loading), _loadingText("starting up") {} |
| 10 | + |
| 11 | +void Display::_start() { |
| 12 | + xTaskCreate(Display::__loop, "display", 10000, this, 10, NULL); |
| 13 | +} |
| 14 | + |
| 15 | +const char *Display::_getErrorText() { |
| 16 | + switch (this->_errorCode) { |
| 17 | + case 404: |
| 18 | + return "Timer not found"; |
| 19 | + default: |
| 20 | + return ""; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +void Display::__loop(void *arg) { |
| 25 | + Display *that = (Display *)arg; |
| 26 | + that->setup(); |
| 27 | + |
| 28 | + for (;;) { |
| 29 | + that->loop(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +Display *Display::from(MatrixPanel_I2S_DMA *matrix) { |
| 34 | + return new Hub75_Display(matrix); |
| 35 | +} |
| 36 | + |
| 37 | +void Display::printLoading(const char *text) { |
| 38 | + this->_loadingText = text; |
| 39 | + this->_state = Loading; |
| 40 | +} |
| 41 | +void Display::printWifiSetup(String id) { |
| 42 | + this->_wifiId = id; |
| 43 | + this->_state = WifiSetupNeeded; |
| 44 | +} |
| 45 | +void Display::printError(int error) { |
| 46 | + this->_errorCode = error; |
| 47 | + this->_state = Error; |
| 48 | +} |
| 49 | +void Display::printTimer(timer::ActiveSegment segment) { |
| 50 | + this->_segment = segment; |
| 51 | + this->_state = Timer; |
| 52 | +} |
| 53 | + |
| 54 | +/* |
| 55 | + * Hub75_Display |
| 56 | + */ |
| 57 | + |
| 58 | +Hub75_Display::Hub75_Display(MatrixPanel_I2S_DMA *matrix) |
| 59 | + : Display(), _matrix(matrix) { |
| 60 | + |
| 61 | + _matrix->begin(); |
| 62 | + _matrix->clearScreen(); |
| 63 | + _matrix->flipDMABuffer(); |
| 64 | + _matrix->setBrightness8(250); |
| 65 | + |
| 66 | + for (int i = 0; i < 10; i++) { |
| 67 | + brand_letter_delays[i] = i * DIM_DELAY_ININTIAL_STEP; |
| 68 | + } |
| 69 | + |
| 70 | + this->_start(); |
| 71 | +} |
| 72 | + |
| 73 | +void Hub75_Display::setup() {} |
| 74 | + |
| 75 | +void Hub75_Display::loop() { |
| 76 | + _matrix->clearScreen(); |
| 77 | + _matrix->setCursor(0, 0); |
| 78 | + |
| 79 | + switch (this->_state) { |
| 80 | + case Loading: { |
| 81 | + _matrix->setFont(&AdventPro_Regular18pt7b); |
| 82 | + _matrix->setTextSize(1); |
| 83 | + _matrix->setCursor(4, 40); |
| 84 | + for (int i = 0; i < 10; i++) { |
| 85 | + _printBrandAnimationLetter(brand_letters[i], brand_letter_brightnesses[i], |
| 86 | + brand_letter_delays[i]); |
| 87 | + } |
| 88 | + |
| 89 | + _matrix->setFont(); |
| 90 | + _setTextColor(0xffffff); |
| 91 | + uint8_t textlen = strlen(_loadingText); |
| 92 | + _matrix->setCursor((128 - textlen * 6) / 2, 54); |
| 93 | + _matrix->print(_loadingText); |
| 94 | + break; |
| 95 | + } |
| 96 | + case WifiSetupNeeded: |
| 97 | + _setTextColor(0xaaaaaa); |
| 98 | + _matrix->setTextSize(1); |
| 99 | + _matrix->setCursor(0, 0); |
| 100 | + _matrix->println("Please setup:"); |
| 101 | + _matrix->println("1. Connect to WiFi"); |
| 102 | + _setTextColor(0x0000ff); |
| 103 | + _matrix->println(" display-" + this->_wifiId); |
| 104 | + _setTextColor(0xBB274B); |
| 105 | + _matrix->println("2. Setup"); |
| 106 | + _setTextColor(0xaaaaaa); |
| 107 | + _matrix->println("3. enter Timer ID"); |
| 108 | + _setTextColor(0xBB274B); |
| 109 | + _matrix->println("4. Go back"); |
| 110 | + _matrix->println("5. Configure WiFi"); |
| 111 | + break; |
| 112 | + case Error: { |
| 113 | + _setTextColor(0xffffff); |
| 114 | + _matrix->setCursor(49, 3); |
| 115 | + _matrix->print("ERROR"); |
| 116 | + |
| 117 | + _setTextColor(0xff0000); |
| 118 | + _matrix->setTextSize(4); |
| 119 | + _matrix->setCursor(28, 15); |
| 120 | + _matrix->print(this->_errorCode); |
| 121 | + |
| 122 | + _matrix->setTextSize(1); |
| 123 | + _setTextColor(0xffffff); |
| 124 | + uint8_t textlen = strlen(_getErrorText()); |
| 125 | + _matrix->setCursor((128 - textlen * 6) / 2, 47); |
| 126 | + _matrix->print(_getErrorText()); |
| 127 | + |
| 128 | + String ip = WiFi.localIP().toString(); |
| 129 | + _matrix->setCursor((128 - ip.length() * 6) / 2, 56); |
| 130 | + _matrix->print(WiFi.localIP().toString()); |
| 131 | + break; |
| 132 | + } |
| 133 | + case Timer: { |
| 134 | + _matrix->setFont(); |
| 135 | + |
| 136 | + _matrix->setTextSize(1); |
| 137 | + _setTextColor(0xff, 0xff, 0xff); |
| 138 | + uint8_t textlen = strlen(_segment.label); |
| 139 | + _matrix->setCursor((128 - textlen * 6) / 2, 3); |
| 140 | + _matrix->print(_segment.label); |
| 141 | + |
| 142 | + _setTextColor(_segment.color); |
| 143 | + _matrix->setTextSize(5); |
| 144 | + _matrix->setTextWrap(false); |
| 145 | + _matrix->setCursor(1, 15); |
| 146 | + _matrix->printf("%02d", _segment.seconds / 60); |
| 147 | + |
| 148 | + _matrix->setCursor(51, 15); |
| 149 | + _matrix->print(":"); |
| 150 | + _matrix->setCursor(71, 15); |
| 151 | + _matrix->printf("%02d", _segment.seconds % 60); |
| 152 | + |
| 153 | + if (timer::timerData()->display_options.clock) { |
| 154 | + time_t timer = |
| 155 | + (_segment.currentTime / 1000) + ((60 * 60) * wifi::timezoneOffset()); |
| 156 | + tm *time = localtime(&timer); |
| 157 | + _matrix->setTextSize(1); |
| 158 | + _setTextColor(0xff, 0xff, 0xff); |
| 159 | + _matrix->setCursor(49, 54); |
| 160 | + |
| 161 | + _matrix->printf("%02d:%02d", time->tm_hour, time->tm_min); |
| 162 | + } |
| 163 | + |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // if ((millis() / 1000) % 2) |
| 169 | + // _matrix->drawPixel(0, 0, _matrix->color333(0xff, 0xff, 0xff)); |
| 170 | + |
| 171 | + _matrix->flipDMABuffer(); |
| 172 | + vTaskDelay(10); |
| 173 | +} |
| 174 | + |
| 175 | +void Hub75_Display::_printBrandAnimationLetter(char letter, uint8_t &brightness, |
| 176 | + uint16_t &delay) { |
| 177 | + _setTextColor(0, 0, brightness); |
| 178 | + _matrix->print(letter); |
| 179 | + if (brightness > DIM_TO && delay == 0) { |
| 180 | + brightness -= DIM_STEP; |
| 181 | + } else if (brightness <= DIM_TO && delay == 0) { |
| 182 | + delay = DIM_DELAY; |
| 183 | + } else if (brightness < 0xff && delay == DIM_DELAY && |
| 184 | + 0xff - brightness > DIM_STEP) { |
| 185 | + brightness += DIM_STEP; |
| 186 | + } else if (brightness < 0xff && delay == DIM_DELAY) { |
| 187 | + brightness = 0xff; |
| 188 | + } else { |
| 189 | + delay--; |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +void Hub75_Display::_setTextColor(uint32_t c) { |
| 194 | + _setTextColor(c >> 16, c >> 8, c); |
| 195 | +} |
| 196 | +void Hub75_Display::_setTextColor(uint8_t r, uint8_t g, uint8_t b) { |
| 197 | + uint16_t packed = |
| 198 | + ((uint16_t)(r & 0xF8) << 8) | ((uint16_t)(g & 0xFC) << 3) | (b >> 3); |
| 199 | + |
| 200 | + _matrix->setTextColor(packed); |
| 201 | +} |
0 commit comments