瀏覽代碼

adjust backlight according to ldr value. needs resistor replacement.

Thomas Buck 5 月之前
父節點
當前提交
3aa6f97957
共有 1 個文件被更改,包括 44 次插入27 次删除
  1. 44
    27
      src/ui.cpp

+ 44
- 27
src/ui.cpp 查看文件

@@ -4,7 +4,11 @@
4 4
  * https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/Basics/2-TouchTest/2-TouchTest.ino
5 5
  * https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/Basics/4-BacklightControlTest/4-BacklightControlTest.ino
6 6
  *
7
- * ESP8266 / ESP32 Environmental Sensor
7
+ * ESP8266 / ESP32 Environmental
8
+ * Touch UI for ESP32 CYD (Cheap Yellow Display).
9
+ *
10
+ * LDR circuit on this board is really strange and does not give me useful information.
11
+ * To get it working remove R19 and replace R15 with 100k.
8 12
  *
9 13
  * ----------------------------------------------------------------------------
10 14
  * "THE BEER-WARE LICENSE" (Revision 42):
@@ -56,13 +60,22 @@
56 60
 #define BTNS_OFF_Y ((LCD_HEIGHT - (3 * BTN_H) - (2 * BTN_GAP)) / 2)
57 61
 
58 62
 #define INVERT_BOOL(x) (x) = !(x)
63
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
64
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
65
+
66
+#define LDR_CHECK_MS 100
67
+#define LDR_DARK_VALUE 1200
68
+#define LDR_BRIGHT_VALUE 0
69
+#define LDR_LOWPASS_FACT 0.1f
70
+
71
+#define STANDBY_BRIGHTNESS 10
72
+#define LCD_MIN_BRIGHTNESS (STANDBY_BRIGHTNESS * 2)
73
+#define LCD_MAX_BRIGHTNESS 255
59 74
 
60
-#define LDR_CHECK_MS 1000
61 75
 #define MIN_TOUCH_DELAY_MS 200
62 76
 #define TOUCH_PRESSURE_MIN 200
63 77
 #define FULL_BRIGHT_MS (1000 * 30)
64 78
 #define NO_BRIGHT_MS (1000 * 2)
65
-#define STANDBY_BRIGHTNESS 10
66 79
 
67 80
 #define NTP_SERVER "pool.ntp.org"
68 81
 #define STANDBY_REDRAW_MS 500
@@ -93,8 +106,8 @@ static bool is_touched = false;
93 106
 static unsigned long last_ldr = 0;
94 107
 static int ldr_value = 0;
95 108
 static unsigned long last_touch_time = 0;
96
-static int curr_brightness = 255;
97
-static int set_max_brightness = 255;
109
+static int curr_brightness = LCD_MAX_BRIGHTNESS;
110
+static int set_max_brightness = LCD_MAX_BRIGHTNESS;
98 111
 static unsigned long last_standby_draw = 0;
99 112
 
100 113
 static TS_Point touchToScreen(TS_Point p) {
@@ -392,22 +405,6 @@ void ui_progress(enum ui_state state) {
392 405
 void ui_run(void) {
393 406
     unsigned long now = millis();
394 407
 
395
-    // adjust backlight brightness
396
-    unsigned long diff = now - last_touch_time;
397
-    if (diff < FULL_BRIGHT_MS) {
398
-        curr_brightness = set_max_brightness;
399
-    } else if (diff < (FULL_BRIGHT_MS + NO_BRIGHT_MS)) {
400
-        curr_brightness = map(diff - FULL_BRIGHT_MS, 0, NO_BRIGHT_MS, set_max_brightness, STANDBY_BRIGHTNESS);
401
-    } else {
402
-        if ((curr_brightness > STANDBY_BRIGHTNESS) || ((now - last_standby_draw) >= STANDBY_REDRAW_MS)) {
403
-            // enter standby screen
404
-            draw_standby();
405
-            last_standby_draw = now;
406
-        }
407
-        curr_brightness = STANDBY_BRIGHTNESS;
408
-    }
409
-    ledcAnalogWrite(LEDC_CHANNEL_0, curr_brightness);
410
-
411 408
     // go to info page when BOOT button is pressed
412 409
     if (!digitalRead(BTN_PIN)) {
413 410
         ui_page = UI_INFO;
@@ -418,16 +415,36 @@ void ui_run(void) {
418 415
         last_ldr = now;
419 416
         int ldr = analogRead(LDR_PIN);
420 417
 
421
-        // TODO lowpass?
422
-        //ldr_value = (ldr_value * 0.9f) + (ldr * 0.1f);
423
-        ldr_value = ldr;
418
+        ldr_value = (ldr_value * (1.0f - LDR_LOWPASS_FACT)) + (ldr * LDR_LOWPASS_FACT);
424 419
 
425
-        // refresh info page, it shows the LDR value
426
-        if (ui_page == UI_INFO) {
420
+        // adjust backlight according to ldr
421
+        int tmp = MIN(LDR_DARK_VALUE, MAX(0, ldr_value));
422
+        set_max_brightness = map(tmp, LDR_DARK_VALUE, LDR_BRIGHT_VALUE, LCD_MIN_BRIGHTNESS, LCD_MAX_BRIGHTNESS);
423
+
424
+        // refresh info page every 1s, it shows the LDR value
425
+        static int cnt = 0;
426
+        if ((ui_page == UI_INFO) && (++cnt >= (1000 / LDR_CHECK_MS))) {
427
+            cnt = 0;
427 428
             ui_draw_menu();
428 429
         }
429 430
     }
430 431
 
432
+    // adjust backlight brightness
433
+    unsigned long diff = now - last_touch_time;
434
+    if ((diff < FULL_BRIGHT_MS) || (ui_page == UI_INFO))  {
435
+        curr_brightness = set_max_brightness;
436
+    } else if (diff < (FULL_BRIGHT_MS + NO_BRIGHT_MS)) {
437
+        curr_brightness = map(diff - FULL_BRIGHT_MS, 0, NO_BRIGHT_MS, set_max_brightness, STANDBY_BRIGHTNESS);
438
+    } else {
439
+        if ((curr_brightness > STANDBY_BRIGHTNESS) || ((now - last_standby_draw) >= STANDBY_REDRAW_MS)) {
440
+            // enter standby screen
441
+            draw_standby();
442
+            last_standby_draw = now;
443
+        }
444
+        curr_brightness = STANDBY_BRIGHTNESS;
445
+    }
446
+    ledcAnalogWrite(LEDC_CHANNEL_0, curr_brightness);
447
+
431 448
     bool touched = ts.tirqTouched() && ts.touched();
432 449
     TS_Point p;
433 450
 
@@ -442,7 +459,7 @@ void ui_run(void) {
442 459
 
443 460
     if (touched && (!is_touched)) {
444 461
         is_touched = true;
445
-        last_touch_time = millis();
462
+        last_touch_time = now;
446 463
 
447 464
         // skip touch event and just go back to full brightness
448 465
         if (curr_brightness < set_max_brightness) {

Loading…
取消
儲存