2019-10-11 16:06:37 +00:00
//backlight.cpp
# include "backlight.h"
# include <avr/eeprom.h>
# include <Arduino.h>
# include "eeprom.h"
# include "Marlin.h"
# include "pins.h"
# include "fastio.h"
2019-10-13 11:31:43 +00:00
# include "Timer.h"
2019-10-11 16:06:37 +00:00
2019-10-11 16:23:38 +00:00
# ifdef LCD_BL_PIN
2019-10-17 10:11:32 +00:00
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 ;
2019-10-13 11:31:43 +00:00
unsigned long backlightTimer_period = 10000ul ;
LongTimer backlightTimer ;
2019-10-17 10:11:32 +00:00
void backlight_save ( ) //saves all backlight data to eeprom.
2019-10-13 11:31:43 +00:00
{
2019-10-17 10:11:32 +00:00
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 ) ;
2019-10-13 11:31:43 +00:00
}
2019-10-17 10:11:32 +00:00
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.
2019-10-13 11:31:43 +00:00
{
2019-10-17 10:11:32 +00:00
if ( ! backlightSupport ) return ;
2019-10-13 11:31:43 +00:00
backlightTimer . start ( ) ;
}
2019-10-11 16:06:37 +00:00
void backlight_update ( )
{
2019-10-16 11:40:45 +00:00
if ( ! backlightSupport ) return ;
2019-10-13 11:31:43 +00:00
2019-10-17 10:11:32 +00:00
if ( backlightMode = = BACKLIGHT_MODE_AUTO )
2019-10-11 16:06:37 +00:00
{
2019-10-17 10:11:32 +00:00
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.
2019-10-11 16:06:37 +00:00
}
2019-10-17 10:11:32 +00:00
else if ( backlightMode = = BACKLIGHT_MODE_DIM ) analogWrite ( LCD_BL_PIN , backlightLevel_LOW ) ;
else analogWrite ( LCD_BL_PIN , backlightLevel_HIGH ) ;
2019-10-11 16:06:37 +00:00
}
void backlight_init ( )
{
2019-10-16 11:40:45 +00:00
//check for backlight support on lcd
SET_INPUT ( LCD_BL_PIN ) ;
WRITE ( LCD_BL_PIN , HIGH ) ;
_delay ( 10 ) ;
backlightSupport = ! READ ( LCD_BL_PIN ) ;
2019-10-17 10:11:32 +00:00
if ( ! backlightSupport ) return ;
2019-10-16 11:40:45 +00:00
2019-10-17 10:11:32 +00:00
//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 ) ;
2019-10-11 16:06:37 +00:00
SET_OUTPUT ( LCD_BL_PIN ) ;
2019-10-17 10:11:32 +00:00
backlightTimer_reset ( ) ;
backlight_update ( ) ; //sets brightness
2019-10-11 16:06:37 +00:00
}
2019-10-11 16:23:38 +00:00
# endif //LCD_BL_PIN