espd

esp32 - backlight control and cell colors

Apr 3rd, 2025 (edited)
15
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. /*******************************************************************
  2.     Combined LDR and Backlight Control with Smoothing and Presets
  3.     - Automatically adjusts screen brightness based on ambient light
  4.     - Adds smoothing to avoid flicker
  5.     - Includes Day/Night preset brightness levels
  6.  *******************************************************************/
  7.  
  8. #include <TFT_eSPI.h>
  9. TFT_eSPI tft = TFT_eSPI();
  10.  
  11. #define LCD_BACK_LIGHT_PIN 21  // PWM pin for screen backlight
  12. #define LDR_PIN 34             // Analog pin connected to LDR
  13.  
  14. // PWM settings
  15. const int pwmChannel = 0;
  16. const int pwmFreq = 5000;        // 5kHz PWM frequency
  17. const int pwmResolution = 12;    // 12-bit resolution (0-4095)
  18.  
  19. // Smoothing settings
  20. const int numReadings = 10;
  21. int readings[numReadings];       // Array to store LDR readings
  22. int readIndex = 0;
  23. long total = 0;
  24. int average = 0;
  25. string curr_bg_color = "";
  26. string curr_text_color = "";
  27.  
  28. // Brightness presets
  29. const int DAY_BRIGHTNESS = 4095;  // Full brightness
  30. const int NIGHT_BRIGHTNESS = 300; // Low brightness
  31.  
  32. void setup() {
  33.   Serial.begin(115200);
  34.   tft.init();
  35.   tft.setRotation(1);
  36.   tft.fillScreen(TFT_BLACK);
  37.   tft.setTextColor(TFT_WHITE, TFT_BLACK);
  38.   tft.setTextSize(2);
  39.   tft.setCursor(0, 0);
  40.   tft.println("Auto Brightness Display");
  41.  
  42.   analogReadResolution(12); // Ensure analog input is 0–4095
  43.  
  44.   // Setup PWM for backlight
  45.   ledcSetup(pwmChannel, pwmFreq, pwmResolution);
  46.   ledcAttachPin(LCD_BACK_LIGHT_PIN, pwmChannel);
  47.  
  48.   // Initialize smoothing array
  49.   for (int i = 0; i < numReadings; i++) {
  50.     readings[i] = 0;
  51.   }
  52. }
  53.  
  54. void loop() {
  55.   total = total - readings[readIndex];
  56.   readings[readIndex] = analogRead(LDR_PIN);
  57.   total = total + readings[readIndex];
  58.   readIndex = (readIndex + 1) % numReadings;
  59.   average = total / numReadings;
  60.  
  61.   Serial.print("Smoothed LDR Value: ");
  62.   Serial.println(average);
  63.  
  64.   // Determine brightness preset based on ambient light
  65.   int brightness;
  66.   if (average < 1500) {
  67.     brightness = DAY_BRIGHTNESS; // Bright outside
  68.     lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(255, 255, 255), LV_PART_MAIN);
  69.     lv_obj_set_style_text_color(table, lv_color_black(), LV_PART_ITEMS);
  70.     lv_obj_set_style_bg_color(table, lv_color_white(), LV_PART_MAIN);
  71.     curr_bg_color = "255, 255, 255";
  72.     curr_text_color = "0, 0, 0";
  73.   } else {
  74.     brightness = NIGHT_BRIGHTNESS; // Dark environment
  75.     lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(30, 30, 30), LV_PART_MAIN);
  76.     lv_obj_set_style_text_color(table, lv_color_white(), LV_PART_ITEMS);
  77.     //lv_obj_set_style_bg_color(table, lv_color_black(), LV_PART_MAIN);
  78.     lv_obj_set_style_bg_color(table, lv_color_make(30, 30, 30), LV_PART_MAIN);
  79.     curr_bg_color = "30, 30, 30";
  80.     curr_text_color = "255, 255, 255";
  81.   }
  82.   ledcWrite(pwmChannel, brightness);
  83.  
  84.   delay(200);
  85. }
  86.  
  87. void cell_color_mod(int row, int col, string col_color, string text_color) {
  88.     lv_table_set_cell_text_color(table, row, col, lv_color_make(text_color));
  89.     lv_table_set_cell_bg_color(table, row, col, lv_color_make(col_color));
  90.     //lv_table_set_cell_bg_color(table, row, col, lv_palette_main(LV_PALETTE_RED));
  91. }
Advertisement
Comments
  • espd
    107 days
    # text 0.24 KB | 0 0
    1. https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/Basics/4-BacklightControlTest/4-BacklightControlTest.ino
    2.  
    3. https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/Basics/5-LDRTest/5-LDRTest.ino
Add Comment
Please, Sign In to add comment