Browse Source

turn off backlight after a while

Thomas Buck 6 months ago
parent
commit
b271b2325d
1 changed files with 25 additions and 1 deletions
  1. 25
    1
      src/ui.cpp

+ 25
- 1
src/ui.cpp View File

@@ -58,6 +58,8 @@
58 58
 #define LDR_CHECK_MS 1000
59 59
 #define MIN_TOUCH_DELAY_MS 200
60 60
 #define TOUCH_PRESSURE_MIN 200
61
+#define FULL_BRIGHT_MS (1000 * 30)
62
+#define NO_BRIGHT_MS (1000 * 5)
61 63
 
62 64
 static SPIClass mySpi = SPIClass(HSPI);
63 65
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
@@ -80,6 +82,8 @@ static bool is_touched = false;
80 82
 static unsigned long last_ldr = 0;
81 83
 static int ldr_value = 0;
82 84
 static unsigned long last_touch_time = 0;
85
+static int curr_brightness = 255;
86
+static int set_max_brightness = 255;
83 87
 
84 88
 static TS_Point touchToScreen(TS_Point p) {
85 89
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
@@ -225,7 +229,8 @@ void ui_init(void) {
225 229
 
226 230
     ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT);
227 231
     ledcAttachPin(TFT_BL, LEDC_CHANNEL_0);
228
-    ledcAnalogWrite(LEDC_CHANNEL_0, 255);
232
+    curr_brightness = set_max_brightness;
233
+    ledcAnalogWrite(LEDC_CHANNEL_0, curr_brightness);
229 234
 
230 235
     pinMode(BTN_PIN, INPUT);
231 236
 
@@ -318,10 +323,23 @@ void ui_progress(enum ui_state state) {
318 323
 void ui_run(void) {
319 324
     unsigned long now = millis();
320 325
 
326
+    // adjust backlight brightness
327
+    unsigned long diff = now - last_touch_time;
328
+    if (diff < FULL_BRIGHT_MS) {
329
+        curr_brightness = set_max_brightness;
330
+    } 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);
332
+    } else {
333
+        curr_brightness = 0;
334
+    }
335
+    ledcAnalogWrite(LEDC_CHANNEL_0, curr_brightness);
336
+
337
+    // go to info page when BOOT button is pressed
321 338
     if (!digitalRead(BTN_PIN)) {
322 339
         ui_page = UI_INFO;
323 340
     }
324 341
 
342
+    // read out LDR in regular intervals
325 343
     if (now >= (last_ldr + LDR_CHECK_MS)) {
326 344
         last_ldr = now;
327 345
         int ldr = analogRead(LDR_PIN);
@@ -330,6 +348,7 @@ void ui_run(void) {
330 348
         //ldr_value = (ldr_value * 0.9f) + (ldr * 0.1f);
331 349
         ldr_value = ldr;
332 350
 
351
+        // refresh info page, it shows the LDR value
333 352
         if (ui_page == UI_INFO) {
334 353
             ui_draw_menu();
335 354
         }
@@ -351,6 +370,11 @@ void ui_run(void) {
351 370
         is_touched = true;
352 371
         last_touch_time = millis();
353 372
 
373
+        // skip touch event and just go back to full brightness
374
+        if (curr_brightness < set_max_brightness) {
375
+            return ui_run();
376
+        }
377
+
354 378
         if (ui_page == UI_INFO) {
355 379
             // switch to next page, skip init and info screen
356 380
             do {

Loading…
Cancel
Save