Browse Source

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

Thomas Buck 4 months ago
parent
commit
dbac88668a
1 changed files with 66 additions and 4 deletions
  1. 66
    4
      src/ui.cpp

+ 66
- 4
src/ui.cpp View File

16
 
16
 
17
 #include <Arduino.h>
17
 #include <Arduino.h>
18
 #include <WiFi.h>
18
 #include <WiFi.h>
19
+#include <time.h>
19
 
20
 
20
 #include "config.h"
21
 #include "config.h"
22
+#include "DebugLog.h"
21
 #include "mqtt.h"
23
 #include "mqtt.h"
22
 #include "ui.h"
24
 #include "ui.h"
23
 
25
 
59
 #define MIN_TOUCH_DELAY_MS 200
61
 #define MIN_TOUCH_DELAY_MS 200
60
 #define TOUCH_PRESSURE_MIN 200
62
 #define TOUCH_PRESSURE_MIN 200
61
 #define FULL_BRIGHT_MS (1000 * 30)
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
 static SPIClass mySpi = SPIClass(HSPI);
74
 static SPIClass mySpi = SPIClass(HSPI);
65
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
75
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
84
 static unsigned long last_touch_time = 0;
94
 static unsigned long last_touch_time = 0;
85
 static int curr_brightness = 255;
95
 static int curr_brightness = 255;
86
 static int set_max_brightness = 255;
96
 static int set_max_brightness = 255;
97
+static unsigned long last_standby_draw = 0;
87
 
98
 
88
 static TS_Point touchToScreen(TS_Point p) {
99
 static TS_Point touchToScreen(TS_Point p) {
89
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
100
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
219
     tft.drawString("LDR: " + String(ldr_value), 0, 40 + 16 * 12, 1);
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
 void ui_init(void) {
274
 void ui_init(void) {
223
     mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
275
     mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
224
     ts.begin(mySpi);
276
     ts.begin(mySpi);
310
         } break;
362
         } break;
311
 
363
 
312
         case UI_READY: {
364
         case UI_READY: {
365
+            // get time via NTP
366
+            configTime(gmtOffset_sec, daylightOffset_sec, NTP_SERVER);
367
+
313
             ui_page = UI_START;
368
             ui_page = UI_START;
314
             ui_draw_menu();
369
             ui_draw_menu();
315
         } break;
370
         } break;
328
     if (diff < FULL_BRIGHT_MS) {
383
     if (diff < FULL_BRIGHT_MS) {
329
         curr_brightness = set_max_brightness;
384
         curr_brightness = set_max_brightness;
330
     } else if (diff < (FULL_BRIGHT_MS + NO_BRIGHT_MS)) {
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
     } else {
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
     ledcAnalogWrite(LEDC_CHANNEL_0, curr_brightness);
395
     ledcAnalogWrite(LEDC_CHANNEL_0, curr_brightness);
336
 
396
 
372
 
432
 
373
         // skip touch event and just go back to full brightness
433
         // skip touch event and just go back to full brightness
374
         if (curr_brightness < set_max_brightness) {
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
         if (ui_page == UI_INFO) {
440
         if (ui_page == UI_INFO) {

Loading…
Cancel
Save