Browse Source

turn off backlight after a while

Thomas Buck 5 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
 #define LDR_CHECK_MS 1000
58
 #define LDR_CHECK_MS 1000
59
 #define MIN_TOUCH_DELAY_MS 200
59
 #define MIN_TOUCH_DELAY_MS 200
60
 #define TOUCH_PRESSURE_MIN 200
60
 #define TOUCH_PRESSURE_MIN 200
61
+#define FULL_BRIGHT_MS (1000 * 30)
62
+#define NO_BRIGHT_MS (1000 * 5)
61
 
63
 
62
 static SPIClass mySpi = SPIClass(HSPI);
64
 static SPIClass mySpi = SPIClass(HSPI);
63
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
65
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
80
 static unsigned long last_ldr = 0;
82
 static unsigned long last_ldr = 0;
81
 static int ldr_value = 0;
83
 static int ldr_value = 0;
82
 static unsigned long last_touch_time = 0;
84
 static unsigned long last_touch_time = 0;
85
+static int curr_brightness = 255;
86
+static int set_max_brightness = 255;
83
 
87
 
84
 static TS_Point touchToScreen(TS_Point p) {
88
 static TS_Point touchToScreen(TS_Point p) {
85
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
89
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
225
 
229
 
226
     ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT);
230
     ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT);
227
     ledcAttachPin(TFT_BL, LEDC_CHANNEL_0);
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
     pinMode(BTN_PIN, INPUT);
235
     pinMode(BTN_PIN, INPUT);
231
 
236
 
318
 void ui_run(void) {
323
 void ui_run(void) {
319
     unsigned long now = millis();
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
     if (!digitalRead(BTN_PIN)) {
338
     if (!digitalRead(BTN_PIN)) {
322
         ui_page = UI_INFO;
339
         ui_page = UI_INFO;
323
     }
340
     }
324
 
341
 
342
+    // read out LDR in regular intervals
325
     if (now >= (last_ldr + LDR_CHECK_MS)) {
343
     if (now >= (last_ldr + LDR_CHECK_MS)) {
326
         last_ldr = now;
344
         last_ldr = now;
327
         int ldr = analogRead(LDR_PIN);
345
         int ldr = analogRead(LDR_PIN);
330
         //ldr_value = (ldr_value * 0.9f) + (ldr * 0.1f);
348
         //ldr_value = (ldr_value * 0.9f) + (ldr * 0.1f);
331
         ldr_value = ldr;
349
         ldr_value = ldr;
332
 
350
 
351
+        // refresh info page, it shows the LDR value
333
         if (ui_page == UI_INFO) {
352
         if (ui_page == UI_INFO) {
334
             ui_draw_menu();
353
             ui_draw_menu();
335
         }
354
         }
351
         is_touched = true;
370
         is_touched = true;
352
         last_touch_time = millis();
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
         if (ui_page == UI_INFO) {
378
         if (ui_page == UI_INFO) {
355
             // switch to next page, skip init and info screen
379
             // switch to next page, skip init and info screen
356
             do {
380
             do {

Loading…
Cancel
Save