Skip to content

Commit bf0f78d

Browse files
Refactored system messages
1 parent 2dcf03f commit bf0f78d

File tree

3 files changed

+65
-39
lines changed

3 files changed

+65
-39
lines changed

src/displayTask.cpp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ static float mapf(const float x, const float in_min, const float in_max, const f
55
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
66
}
77

8+
static void showSystemMessage(char *str)
9+
{
10+
static LGFX_Sprite sysMess(&display);
11+
12+
if (sysMess.width() == 0 || sysMess.height() == 0)
13+
{
14+
sysMess.setColorDepth(lgfx::palette_2bit);
15+
if (!sysMess.createSprite(display.width(), GRAPH_HEIGHT))
16+
{
17+
log_e("could not create sprite");
18+
return;
19+
}
20+
sysMess.setPaletteColor(1, 200, 200, 200);
21+
sysMess.setTextColor(1);
22+
}
23+
sysMess.clear();
24+
25+
auto cnt = 0;
26+
char *pch = strtok(str, "\n");
27+
while (pch)
28+
{
29+
sysMess.drawCenterString(pch, display.width() >> 1, cnt++ * DejaVu24Modded.yAdvance, &DejaVu24Modded);
30+
pch = strtok(NULL, "\n");
31+
}
32+
33+
sysMess.pushSprite(0, GRAPH_HEIGHT + 5);
34+
}
35+
836
static void updateCo2History(const int32_t w, const int32_t h, const int32_t x, const int32_t y)
937
{
1038
[[maybe_unused]] auto const START_MS = millis();
@@ -532,24 +560,13 @@ static void updateWeatherForecast(const int32_t w, const int32_t h, const int32_
532560
weather.pushSprite(x, y);
533561
}
534562

535-
static void handleMessage(const displayMessage &msg)
563+
static void handleMessage(displayMessage &msg)
536564
{
537565
switch (msg.type)
538566
{
539567
case displayMessage::SYSTEM_MESSAGE:
540568
{
541-
static LGFX_Sprite sysMess(&display);
542-
if (sysMess.width() == 0 || sysMess.height() == 0)
543-
{
544-
if (!sysMess.createSprite(display.width(), GRAPH_HEIGHT))
545-
{
546-
log_e("could not create sprite");
547-
return;
548-
}
549-
}
550-
sysMess.setTextColor(TFT_WHITE, TFT_BLACK);
551-
sysMess.drawCenterString(msg.str, display.width() >> 1, 40, &DejaVu24Modded);
552-
sysMess.pushSprite(0, GRAPH_HEIGHT + 5);
569+
showSystemMessage(msg.str);
553570
break;
554571
}
555572

@@ -632,6 +649,11 @@ static void updateClock(const struct tm &timeinfo)
632649
clock.pushSprite(0, GRAPH_HEIGHT * 3 + 15);
633650
}
634651

652+
bool TFTtouched(int32_t &x, int32_t &y)
653+
{
654+
return display.getTouch(&x, &y) ? true : false;
655+
}
656+
635657
void displayTask(void *parameter)
636658
{
637659
display.setColorDepth(lgfx::rgb565_2Byte);

src/displayTask.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ static const auto WEATHER_BACKGROUND = display.color565(245, 102, 10);
3535

3636
QueueHandle_t displayQueue = nullptr;
3737

38-
void displayTask(void *parameter);
39-
4038
/* weather icons from https://github.com/visualcrossing/WeatherIcons/tree/main/PNG/1st%20Set%20-%20Color */
4139

4240
extern const uint8_t clear_day_png_start[] asm("_binary_src_weather_icons_clear_day_png_start");

src/main.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "displayMessageStruct.hpp"
2424

2525
extern void displayTask(void *parameter);
26+
extern bool TFTtouched(int32_t &x, int32_t &y);
2627
extern void weatherDownloadTask(void *parameter);
2728

2829
extern QueueHandle_t displayQueue;
@@ -253,6 +254,21 @@ static void ntpSynced(void *cb_arg)
253254
sntp_set_time_sync_notification_cb(NULL);
254255
}
255256

257+
static bool getSensorhubIP(IPAddress &hubIP)
258+
{
259+
int ndx = MDNS.queryService("http", "tcp");
260+
while (--ndx != -1)
261+
{
262+
if (MDNS.hostname(ndx) == WEBSOCKET_MDNS_HOSTNAME)
263+
break;
264+
}
265+
if (ndx == -1)
266+
return false;
267+
268+
hubIP = MDNS.IP(ndx);
269+
return true;
270+
}
271+
256272
void setup()
257273
{
258274
Serial.begin(115200);
@@ -300,53 +316,43 @@ void setup()
300316
while (1)
301317
delay(100);
302318
}
319+
320+
messageOnTFT("Looking for WiFi...\nPlease wait");
321+
303322
log_i("waiting for WiFi network %s to connect", WIFI_SSID);
304323

305324
while (!WiFi.isConnected())
306325
vTaskDelay(pdMS_TO_TICKS(10));
307326

308327
log_i("connected to %s", WIFI_SSID);
309328

310-
messageOnTFT("Searching the sensors...");
311-
312329
if (mdns_init() != ESP_OK)
313330
{
314-
messageOnTFT("mDNS ERROR. System halted");
331+
messageOnTFT("mDNS init ERROR. System halted");
315332
while (1)
316333
delay(100);
317334
}
318335

319-
int ndx = MDNS.queryService("http", "tcp");
320-
321-
log_i("found %i mDNS servers", ndx);
322-
323-
while (--ndx != -1)
324-
{
325-
if (MDNS.hostname(ndx) == WEBSOCKET_MDNS_HOSTNAME)
326-
break;
327-
}
336+
sntp_set_time_sync_notification_cb((sntp_sync_time_cb_t)ntpSynced);
337+
configTzTime(TIMEZONE, NTP_POOL);
328338

329-
if (ndx == -1)
339+
const char searchMess[] = {"Searching the sensors..."};
340+
messageOnTFT(searchMess);
341+
while (!getSensorhubIP(websocketServerIP))
330342
{
331-
messageOnTFT("No sensors. System halted");
332-
while (1)
333-
delay(100);
343+
messageOnTFT("No sensors found\nCheck if the sensors are running\nTap the screen to search again");
344+
int32_t x, y;
345+
while (!TFTtouched(x, y))
346+
vTaskDelay(pdTICKS_TO_MS(10));
347+
messageOnTFT(searchMess);
334348
}
335349

336-
log_i("sensorhub found at IP %s", MDNS.IP(ndx).toString().c_str());
337-
338-
websocketServerIP = MDNS.IP(ndx);
339-
340-
sntp_set_time_sync_notification_cb((sntp_sync_time_cb_t)ntpSynced);
341-
configTzTime(TIMEZONE, NTP_POOL);
342-
343350
webSocket.begin(websocketServerIP, WEBSOCKET_PORT, WEBSOCKET_URL);
344351
webSocket.onEvent(webSocketEvent);
345352
webSocket.setReconnectInterval(600);
346353
}
347354

348355
constexpr const auto TICK_RATE_HZ = 50;
349-
350356
constexpr const TickType_t ticksToWait = pdTICKS_TO_MS(1000 / TICK_RATE_HZ);
351357
static TickType_t xLastWakeTime = xTaskGetTickCount();
352358

0 commit comments

Comments
 (0)