Skip to content

Commit bda929d

Browse files
committed
Feat: reconnect display after issues instead of restarting
1 parent 2a4a0c0 commit bda929d

File tree

6 files changed

+57
-16
lines changed

6 files changed

+57
-16
lines changed

display/include/display.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Hub75_Display : public Display {
4141
virtual void loop() override;
4242

4343
private:
44+
uint16_t _packColor(uint8_t r, uint8_t g, uint8_t b);
4445
void _setTextColor(uint32_t c);
4546
void _setTextColor(uint8_t r, uint8_t g, uint8_t b);
4647
void _printBrandAnimationLetter(char letter, uint8_t &brightness,

display/src/display.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "WiFi.h"
22

33
#include "display.h"
4+
#include "socket.h"
45
#include "time.h"
56
#include "wifi.h"
67

@@ -73,6 +74,9 @@ Hub75_Display::Hub75_Display(MatrixPanel_I2S_DMA *matrix)
7374

7475
void Hub75_Display::setup() {}
7576

77+
uint8_t wifiIcon[] PROGMEM = {B01111110, B10000001, B00111100,
78+
B01000010, B00011000, B00011000};
79+
7680
void Hub75_Display::loop() {
7781
_matrix->clearScreen();
7882
_matrix->setCursor(0, 0);
@@ -172,15 +176,21 @@ void Hub75_Display::loop() {
172176
_setTextColor(0xff, 0xff, 0xff);
173177
_matrix->setCursor(49, 54);
174178

175-
_matrix->printf("%02d:%02d", time->tm_hour, time->tm_min);
179+
_matrix->printf("%02d:%02d:%02d", time->tm_hour, time->tm_min,
180+
time->tm_sec);
176181
}
177182

178183
break;
179184
}
180185
}
181186

182-
// if ((millis() / 1000) % 2)
183-
// _matrix->drawPixel(0, 0, _matrix->color333(0xff, 0xff, 0xff));
187+
if (!wifi::connected()) {
188+
_matrix->drawBitmap(118, 2, (uint8_t *)&wifiIcon, 8, 6,
189+
this->_packColor(255, 0, 0), 0);
190+
} else if (!WebSocket::connected()) {
191+
_matrix->drawBitmap(118, 2, (uint8_t *)&wifiIcon, 8, 6,
192+
this->_packColor(255, 150, 0), 0);
193+
}
184194

185195
_matrix->flipDMABuffer();
186196
vTaskDelay(10);
@@ -204,12 +214,15 @@ void Hub75_Display::_printBrandAnimationLetter(char letter, uint8_t &brightness,
204214
}
205215
}
206216

217+
uint16_t Hub75_Display::_packColor(uint8_t r, uint8_t g, uint8_t b) {
218+
return ((uint16_t)(r & 0xF8) << 8) | ((uint16_t)(g & 0xFC) << 3) | (b >> 3);
219+
}
220+
207221
void Hub75_Display::_setTextColor(uint32_t c) {
208222
_setTextColor(c >> 16, c >> 8, c);
209223
}
210224
void Hub75_Display::_setTextColor(uint8_t r, uint8_t g, uint8_t b) {
211-
uint16_t packed =
212-
((uint16_t)(r & 0xF8) << 8) | ((uint16_t)(g & 0xFC) << 3) | (b >> 3);
225+
uint16_t packed = this->_packColor(r, g, b);
213226

214227
_matrix->setTextColor(packed);
215228
}

display/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void refreshDisplay() {
108108
void loop() {
109109
wifi::loop();
110110
if (!wifi::connected()) {
111-
ESP.restart();
111+
// ESP.restart();
112112
}
113113

114114
WebSocket::loop();

display/src/socket.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ TIME _latestOffsets[10] = {0};
2222
TIME _latestFluctuations[10] = {0};
2323

2424
unsigned long _lastGetTimeSent = 0;
25+
unsigned long _lastGetTimeReceived = 0;
2526
int _error = 0;
2627
timer::TimerData *_timerData;
2728

@@ -162,10 +163,13 @@ void _handleMessage(JsonDocument &doc) {
162163
JsonObject data = doc["data"];
163164
_loadTimerData(data);
164165
} else if (type == "Timestamp") {
165-
unsigned long now = millis();
166-
unsigned long getTimeRoundtrip = now - _lastGetTimeSent;
166+
_lastGetTimeReceived = millis();
167+
unsigned long getTimeRoundtrip = _lastGetTimeReceived - _lastGetTimeSent;
168+
Serial.printf("Roundtrip: %dms\n", getTimeRoundtrip);
169+
167170
long long serverTime = doc["data"];
168-
TIME timeOffset = serverTime - now + getTimeRoundtrip / 2;
171+
TIME timeOffset =
172+
serverTime + (getTimeRoundtrip / 2) - _lastGetTimeReceived;
169173
_handleNewOffset(timeOffset);
170174
} else if (type == "Error") {
171175
_error = doc["data"][0];
@@ -238,8 +242,8 @@ void init(String timerId) {
238242
_websocketConfig.path = "/api/ws";
239243
_websocketConfig.port = 80;
240244
_websocketConfig.disable_auto_reconnect = false;
241-
_websocketConfig.disable_pingpong_discon = false;
242-
_websocketConfig.keep_alive_enable = true;
245+
_websocketConfig.disable_pingpong_discon = true;
246+
_websocketConfig.keep_alive_enable = false;
243247
_websocketConfig.transport = WEBSOCKET_TRANSPORT_OVER_TCP;
244248
_websocketConfig.cert_pem = NULL;
245249
_websocketConfig.cert_len = 0;
@@ -252,7 +256,15 @@ void init(String timerId) {
252256
}
253257

254258
void loop() {
255-
if (connected() && millis() - _lastGetTimeSent >= 1000) {
259+
bool lastGetTimeReceivedOneSecondAgo =
260+
_lastGetTimeReceived > _lastGetTimeSent &&
261+
millis() - _lastGetTimeReceived >= 1000;
262+
bool lastGetTimeReceivedTimeout = _lastGetTimeReceived < _lastGetTimeSent &&
263+
millis() - _lastGetTimeSent >= 10000;
264+
265+
if (connected() &&
266+
(_lastGetTimeSent == 0 || lastGetTimeReceivedOneSecondAgo ||
267+
lastGetTimeReceivedTimeout)) {
256268
_lastGetTimeSent = millis();
257269

258270
String payload = "{\"type\": \"GetTime\"}";

web/src/routes/t/[timerId]/+page.svelte

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
let soundEnabled: boolean;
2323
let timerData: TimerType | undefined;
2424
let lastGetTimeSent = 0;
25+
let lastGetTimeReceived = 0;
2526
let socket: WebSocket | undefined;
2627
2728
const pushValueAndCaluclateAverage = (values: number[], newValue: number) => {
@@ -108,9 +109,10 @@
108109
timerData = timer;
109110
break;
110111
case 'Timestamp':
111-
const now = performance.now();
112-
const getTimeRoundTrip = now - lastGetTimeSent;
113-
let newOffset = data.data + getTimeRoundTrip / 2 - now;
112+
const lastGetTimeReceived = performance.now();
113+
const getTimeRoundTrip = lastGetTimeReceived - lastGetTimeSent;
114+
console.log('roundtrip', getTimeRoundTrip);
115+
let newOffset = data.data + getTimeRoundTrip / 2 - lastGetTimeReceived;
114116
handleNewOffset(newOffset);
115117
break;
116118
case 'Error':
@@ -124,6 +126,18 @@
124126
socket?.send(JSON.stringify({ data: data.params.timerId, type: 'Hello' }));
125127
126128
setInterval(() => {
129+
let lastGetTimeReceivedOneSecondAgo =
130+
lastGetTimeReceived > lastGetTimeSent && performance.now() - lastGetTimeReceived >= 1000;
131+
let lastGetTimeReceivedTimeout =
132+
lastGetTimeReceived < lastGetTimeSent && performance.now() - lastGetTimeSent >= 10000;
133+
134+
if (
135+
lastGetTimeSent !== 0 &&
136+
!lastGetTimeReceivedOneSecondAgo &&
137+
!lastGetTimeReceivedTimeout
138+
) {
139+
return;
140+
}
127141
lastGetTimeSent = performance.now();
128142
socket?.send(JSON.stringify({ type: 'GetTime' }));
129143
}, 1000);

web/src/routes/t/[timerId]/Timer.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
const update = () => {
9595
currentSegment = calculateCurrentSegment();
9696
const { timerText, label, color, seconds } = currentSegment;
97+
console.log(new Date(currentSegment.currentTime).toLocaleTimeString());
9798
9899
if (timerSpan !== null && timerSpan.innerText !== timerText) {
99100
timerSpan.innerText = timerText;
@@ -115,7 +116,7 @@
115116
onMount(() => {
116117
setInterval(() => {
117118
update();
118-
}, 100);
119+
}, 10);
119120
});
120121
121122
$: {

0 commit comments

Comments
 (0)