Sfoglia il codice sorgente

Backlight overhaul and UI changes

leptun 5 anni fa
parent
commit
25e0d23efe
4 ha cambiato i file con 80 aggiunte e 33 eliminazioni
  1. 31 29
      Firmware/backlight.cpp
  2. 11 1
      Firmware/backlight.h
  3. 4 2
      Firmware/eeprom.h
  4. 34 1
      Firmware/ultralcd.cpp

+ 31 - 29
Firmware/backlight.cpp

@@ -8,49 +8,41 @@
 #include "pins.h"
 #include "fastio.h"
 #include "Timer.h"
-// #include "Configuration.h"
 
 #ifdef LCD_BL_PIN
 
-bool backlightSupport = 0;
-int16_t backlightLevel = 0;
-int16_t backlightLevel_old = 0;
+bool backlightSupport = 0; //only if it's true will any of the settings be visible to the user
+int16_t backlightLevel_HIGH = 0;
+int16_t backlightLevel_LOW = 0;
+uint8_t backlightMode = BACKLIGHT_MODE_BRIGHT;
 unsigned long backlightTimer_period = 10000ul;
-bool backlightIsDimmed = true;
 LongTimer backlightTimer;
 
-static void backlightDim()
+void backlight_save() //saves all backlight data to eeprom.
 {
-    // if (backlightIsDimmed) return;
-    backlightLevel /= 4; //make the display dimmer.
-    backlightIsDimmed = true;
+    eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH, (uint8_t)backlightLevel_HIGH);
+    eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW, (uint8_t)backlightLevel_LOW);
+    eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_MODE, backlightMode);
 }
 
-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
+void backlightTimer_reset() //used for resetting the timer and waking the display. Triggered on events such as knob click, rotate and on full screen notifications.
 {
+    if (!backlightSupport) return;
     backlightTimer.start();
-    if (backlightIsDimmed) backlightWake();
 }
 
 void backlight_update()
 {
     if (!backlightSupport) return;
-    if (backlightTimer.expired(backlightTimer_period)) backlightDim();
     
-    if (backlightLevel != backlightLevel_old) //update pwm duty cycle
+    if (backlightMode == BACKLIGHT_MODE_AUTO)
     {
-        analogWrite(LCD_BL_PIN, backlightLevel);
-        backlightLevel_old = backlightLevel;
-        
-        if (!backlightIsDimmed) eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL, backlightLevel); //update eeprom value
+        if (backlightTimer.expired(backlightTimer_period)) analogWrite(LCD_BL_PIN, backlightLevel_LOW);
+        else if (backlightTimer.running()) analogWrite(LCD_BL_PIN, backlightLevel_HIGH);
+        else {/*do nothing*/;} //display is dimmed.
     }
+    else if (backlightMode == BACKLIGHT_MODE_DIM) analogWrite(LCD_BL_PIN, backlightLevel_LOW);
+    else analogWrite(LCD_BL_PIN, backlightLevel_HIGH);
 }
 
 void backlight_init()
@@ -60,13 +52,23 @@ void backlight_init()
     WRITE(LCD_BL_PIN,HIGH);
     _delay(10);
     backlightSupport = !READ(LCD_BL_PIN);
-    if (backlightSupport == 0) return;
+    if (!backlightSupport) return;
 
-//initialize backlight pin
+//initialize backlight
+    backlightMode = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_MODE);
+    if (backlightMode == 0xFF) //set default values
+    {
+        backlightMode = BACKLIGHT_MODE_AUTO;
+        backlightLevel_HIGH = 130;
+        backlightLevel_LOW = 50;
+        backlight_save();
+    }
+    backlightLevel_HIGH = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH);
+    backlightLevel_LOW = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW);
+    
     SET_OUTPUT(LCD_BL_PIN);
-    WRITE(LCD_BL_PIN,0);
-    backlightTimer_reset(); //initializes eeprom data and starts backlightTimer
-    backlight_update(); //actually sets the backlight to the correct level
+    backlightTimer_reset();
+    backlight_update(); //sets brightness
 }
 
 #endif //LCD_BL_PIN

+ 11 - 1
Firmware/backlight.h

@@ -4,12 +4,22 @@
 
 #include <inttypes.h>
 
-extern int16_t backlightLevel;
+enum Backlight_Mode
+{
+	BACKLIGHT_MODE_BRIGHT  = 0,
+	BACKLIGHT_MODE_DIM     = 1,
+	BACKLIGHT_MODE_AUTO    = 2,
+};
+
+extern int16_t backlightLevel_HIGH;
+extern int16_t backlightLevel_LOW;
+extern uint8_t backlightMode;
 extern bool backlightSupport;
 
 extern void backlight_update();
 extern void backlight_init();
 extern void backlightTimer_reset();
+extern void backlight_save();
 
 
 #endif //_BACKLIGHT_H

+ 4 - 2
Firmware/eeprom.h

@@ -201,10 +201,12 @@ static_assert(sizeof(Sheets) == EEPROM_SHEETS_SIZEOF, "Sizeof(Sheets) is not EEP
 #define EEPROM_SHEETS_BASE (EEPROM_CHECK_GCODE - EEPROM_SHEETS_SIZEOF) // Sheets
 static Sheets * const EEPROM_Sheets_base = (Sheets*)(EEPROM_SHEETS_BASE);
 
-#define EEPROM_BACKLIGHT_LEVEL (EEPROM_SHEETS_BASE-1) // uint8
+#define EEPROM_BACKLIGHT_LEVEL_HIGH (EEPROM_SHEETS_BASE-1) // uint8
+#define EEPROM_BACKLIGHT_LEVEL_LOW (EEPROM_BACKLIGHT_LEVEL_HIGH-1) // uint8
+#define EEPROM_BACKLIGHT_MODE (EEPROM_BACKLIGHT_LEVEL_LOW-1) // uint8
 
 //This is supposed to point to last item to allow EEPROM overrun check. Please update when adding new items.
-#define EEPROM_LAST_ITEM EEPROM_BACKLIGHT_LEVEL
+#define EEPROM_LAST_ITEM EEPROM_BACKLIGHT_MODE
 // !!!!!
 // !!!!! this is end of EEPROM section ... all updates MUST BE inserted before this mark !!!!!
 // !!!!!

+ 34 - 1
Firmware/ultralcd.cpp

@@ -56,6 +56,7 @@ char longFilenameOLD[LONG_FILENAME_LENGTH];
 
 static void lcd_sd_updir();
 static void lcd_mesh_bed_leveling_settings();
+static void lcd_backlight_menu();
 
 int8_t ReInitLCD = 0;
 
@@ -5776,7 +5777,7 @@ static void lcd_settings_menu()
 #ifdef LCD_BL_PIN
     if (backlightSupport)
     {
-        MENU_ITEM_EDIT_int3_P(_i("Backlight"), &backlightLevel, 0, 255);
+        MENU_ITEM_SUBMENU_P(_i("Brightness"), lcd_backlight_menu);
     }
 #endif //LCD_BL_PIN
 
@@ -7165,6 +7166,38 @@ static void lcd_mesh_bed_leveling_settings()
 	//SETTINGS_MBL_MODE;
 }
 
+static void backlight_mode_toggle()
+{
+    switch (backlightMode)
+    {
+        case BACKLIGHT_MODE_BRIGHT: backlightMode = BACKLIGHT_MODE_DIM; break;
+        case BACKLIGHT_MODE_DIM: backlightMode = BACKLIGHT_MODE_AUTO; break;
+        case BACKLIGHT_MODE_AUTO: backlightMode = BACKLIGHT_MODE_BRIGHT; break;
+        default: backlightMode = BACKLIGHT_MODE_BRIGHT; break;
+    }
+    backlight_save();
+}
+
+static void lcd_backlight_menu()
+{
+    MENU_BEGIN();
+    ON_MENU_LEAVE(
+        backlight_save();
+    );
+    
+    MENU_ITEM_BACK_P(_T(MSG_BACK));
+    MENU_ITEM_EDIT_int3_P(_i("Level Bright"), &backlightLevel_HIGH, 0, 255);
+    MENU_ITEM_EDIT_int3_P(_i("Level Dimmed"), &backlightLevel_LOW, 0, 255);
+    switch (backlightMode)
+    {
+        case BACKLIGHT_MODE_BRIGHT: MENU_ITEM_FUNCTION_P(_i("Mode   [Always on]"), backlight_mode_toggle); break;
+        case BACKLIGHT_MODE_DIM: MENU_ITEM_FUNCTION_P(_i("Mode  [Always off]"), backlight_mode_toggle); break;
+        default: MENU_ITEM_FUNCTION_P(_i("Mode        [Auto]"), backlight_mode_toggle); break;
+    }
+    
+    MENU_END();
+}
+
 static void lcd_control_temperature_menu()
 {
 #ifdef PIDTEMP