Преглед изворни кода

instead of turning display off, go to a standby screen that shows date and time

Thomas Buck пре 4 месеци
родитељ
комит
dbac88668a
1 измењених фајлова са 66 додато и 4 уклоњено
  1. 66
    4
      src/ui.cpp

+ 66
- 4
src/ui.cpp Прегледај датотеку

@@ -16,8 +16,10 @@
16 16
 
17 17
 #include <Arduino.h>
18 18
 #include <WiFi.h>
19
+#include <time.h>
19 20
 
20 21
 #include "config.h"
22
+#include "DebugLog.h"
21 23
 #include "mqtt.h"
22 24
 #include "ui.h"
23 25
 
@@ -59,7 +61,15 @@
59 61
 #define MIN_TOUCH_DELAY_MS 200
60 62
 #define TOUCH_PRESSURE_MIN 200
61 63
 #define FULL_BRIGHT_MS (1000 * 30)
62
-#define NO_BRIGHT_MS (1000 * 5)
64
+#define NO_BRIGHT_MS (1000 * 2)
65
+#define STANDBY_BRIGHTNESS 10
66
+
67
+#define NTP_SERVER "pool.ntp.org"
68
+#define STANDBY_REDRAW_MS (1000 * 10)
69
+
70
+// TODO make configurable
71
+#define gmtOffset_sec (60 * 60)
72
+#define daylightOffset_sec (60 * 60)
63 73
 
64 74
 static SPIClass mySpi = SPIClass(HSPI);
65 75
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
@@ -84,6 +94,7 @@ static int ldr_value = 0;
84 94
 static unsigned long last_touch_time = 0;
85 95
 static int curr_brightness = 255;
86 96
 static int set_max_brightness = 255;
97
+static unsigned long last_standby_draw = 0;
87 98
 
88 99
 static TS_Point touchToScreen(TS_Point p) {
89 100
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
@@ -219,6 +230,47 @@ static void draw_info(void) {
219 230
     tft.drawString("LDR: " + String(ldr_value), 0, 40 + 16 * 12, 1);
220 231
 }
221 232
 
233
+static void draw_standby(void) {
234
+    tft.fillScreen(TFT_BLACK);
235
+
236
+    tft.setTextDatum(TC_DATUM); // top center
237
+    tft.drawString(ESP_PLATFORM_NAME " " NAME_OF_FEATURE " V" ESP_ENV_VERSION, LCD_WIDTH / 2, 0, 2);
238
+    tft.drawString("by xythobuz.de", LCD_WIDTH / 2, 16, 2);
239
+
240
+    struct tm timeinfo;
241
+    String date, time;
242
+    if(getLocalTime(&timeinfo)) {
243
+        if (timeinfo.tm_mday < 10) {
244
+            date += "0";
245
+        }
246
+        date += String(timeinfo.tm_mday);
247
+        date += ".";
248
+        if ((timeinfo.tm_mon + 1) < 10) {
249
+            date += "0";
250
+        }
251
+        date += String(timeinfo.tm_mon + 1);
252
+        date += ".";
253
+        date += String(timeinfo.tm_year + 1900);
254
+
255
+        if (timeinfo.tm_hour < 10) {
256
+            time += "0";
257
+        }
258
+        time += String(timeinfo.tm_hour);
259
+        time += ":";
260
+        if (timeinfo.tm_min < 10) {
261
+            time += "0";
262
+        }
263
+        time += String(timeinfo.tm_min);
264
+    }
265
+
266
+    tft.setTextDatum(MC_DATUM); // middle center
267
+    tft.drawString(date, LCD_WIDTH / 2, LCD_HEIGHT / 2 - 8, 2);
268
+    tft.drawString(time, LCD_WIDTH / 2, LCD_HEIGHT / 2 + 8, 2);
269
+
270
+    tft.setTextDatum(BC_DATUM); // bottom center
271
+    tft.drawString("Touch to begin...", LCD_WIDTH / 2, LCD_HEIGHT, 2);
272
+}
273
+
222 274
 void ui_init(void) {
223 275
     mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
224 276
     ts.begin(mySpi);
@@ -310,6 +362,9 @@ void ui_progress(enum ui_state state) {
310 362
         } break;
311 363
 
312 364
         case UI_READY: {
365
+            // get time via NTP
366
+            configTime(gmtOffset_sec, daylightOffset_sec, NTP_SERVER);
367
+
313 368
             ui_page = UI_START;
314 369
             ui_draw_menu();
315 370
         } break;
@@ -328,9 +383,14 @@ void ui_run(void) {
328 383
     if (diff < FULL_BRIGHT_MS) {
329 384
         curr_brightness = set_max_brightness;
330 385
     } else if (diff < (FULL_BRIGHT_MS + NO_BRIGHT_MS)) {
331
-        curr_brightness = map(diff - FULL_BRIGHT_MS, 0, NO_BRIGHT_MS, set_max_brightness, 0);
386
+        curr_brightness = map(diff - FULL_BRIGHT_MS, 0, NO_BRIGHT_MS, set_max_brightness, STANDBY_BRIGHTNESS);
332 387
     } else {
333
-        curr_brightness = 0;
388
+        if ((curr_brightness > STANDBY_BRIGHTNESS) || ((now - last_standby_draw) >= STANDBY_REDRAW_MS)) {
389
+            // enter standby screen
390
+            draw_standby();
391
+            last_standby_draw = now;
392
+        }
393
+        curr_brightness = STANDBY_BRIGHTNESS;
334 394
     }
335 395
     ledcAnalogWrite(LEDC_CHANNEL_0, curr_brightness);
336 396
 
@@ -372,7 +432,9 @@ void ui_run(void) {
372 432
 
373 433
         // skip touch event and just go back to full brightness
374 434
         if (curr_brightness < set_max_brightness) {
375
-            return ui_run();
435
+            tft.fillScreen(TFT_BLACK); // exit standby screen
436
+            ui_draw_menu(); // re-draw normal screen contents
437
+            return ui_run(); // skip touch and increase brightness
376 438
         }
377 439
 
378 440
         if (ui_page == UI_INFO) {

Loading…
Откажи
Сачувај