Browse Source

Auto-Dimming initial

leptun 5 năm trước cách đây
mục cha
commit
75f389a71d
3 tập tin đã thay đổi với 39 bổ sung7 xóa
  1. 31 6
      Firmware/backlight.cpp
  2. 1 0
      Firmware/backlight.h
  3. 7 1
      Firmware/ultralcd.cpp

+ 31 - 6
Firmware/backlight.cpp

@@ -7,22 +7,47 @@
 #include "Marlin.h"
 #include "pins.h"
 #include "fastio.h"
-// #include "Timer.h"
+#include "Timer.h"
 // #include "Configuration.h"
 
 #ifdef LCD_BL_PIN
 
 int16_t backlightLevel = 0;
 int16_t backlightLevel_old = 0;
-// uint16_t backlightCounter = 0;
+unsigned long backlightTimer_period = 10000ul;
+bool backlightIsDimmed = true;
+LongTimer backlightTimer;
+
+static void backlightDim()
+{
+    // if (backlightIsDimmed) return;
+    backlightLevel /= 4; //make the display half as bright.
+    backlightIsDimmed = true;
+}
+
+static void backlightWake()
+{
+    // if (!backlightIsDimmed) return;
+    backlightLevel = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL);
+    backlightIsDimmed = false;
+}
+
+void backlightTimer_reset() //used for resetting the timer and waking the display
+{
+    backlightTimer.start();
+    if (backlightIsDimmed) backlightWake();
+}
 
 void backlight_update()
 {
-    if (backlightLevel != backlightLevel_old) //update level
+    if (backlightTimer.expired(backlightTimer_period)) backlightDim();
+    
+    if (backlightLevel != backlightLevel_old) //update pwm duty cycle
     {
         analogWrite(LCD_BL_PIN, backlightLevel);
         backlightLevel_old = backlightLevel;
-        eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL, backlightLevel);
+        
+        if (!backlightIsDimmed) eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL, backlightLevel); //update eeprom value
     }
 }
 
@@ -30,8 +55,8 @@ void backlight_init()
 {
     SET_OUTPUT(LCD_BL_PIN);
     WRITE(LCD_BL_PIN,0);
-    backlightLevel = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL);
-    backlight_update();
+    backlightTimer_reset(); //initializes eeprom data and starts backlightTimer
+    backlight_update(); //actually sets the backlight to the correct level
 }
 
 #endif //LCD_BL_PIN

+ 1 - 0
Firmware/backlight.h

@@ -8,6 +8,7 @@ extern int16_t backlightLevel;
 
 extern void backlight_update();
 extern void backlight_init();
+extern void backlightTimer_reset();
 
 
 #endif //_BACKLIGHT_H

+ 7 - 1
Firmware/ultralcd.cpp

@@ -8726,6 +8726,7 @@ uint8_t get_message_level()
 
 void menu_lcd_longpress_func(void)
 {
+	backlightTimer_reset();
     if (homing_flag || mesh_bed_leveling_flag || menu_menu == lcd_babystep_z || menu_menu == lcd_move_z)
     {
         // disable longpress during re-entry, while homing or calibration
@@ -8837,9 +8838,14 @@ void menu_lcd_lcdupdate_func(void)
 			Sound_MakeSound(e_SOUND_TYPE_EncoderMove);
 			lcd_encoder_diff = 0;
 			lcd_timeoutToStatus.start();
+			backlightTimer_reset();
 		}
 
-		if (LCD_CLICKED) lcd_timeoutToStatus.start();
+		if (LCD_CLICKED)
+		{
+			lcd_timeoutToStatus.start();
+			backlightTimer_reset();
+		}
 
 		(*menu_menu)();