Prusa-Firmware/Firmware/backlight.cpp

72 lines
1.8 KiB
C++
Raw Normal View History

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
// #include "Configuration.h"
#ifdef LCD_BL_PIN
2019-10-16 11:40:45 +00:00
bool backlightSupport = 0;
2019-10-11 16:06:37 +00:00
int16_t backlightLevel = 0;
int16_t backlightLevel_old = 0;
2019-10-13 11:31:43 +00:00
unsigned long backlightTimer_period = 10000ul;
bool backlightIsDimmed = true;
LongTimer backlightTimer;
static void backlightDim()
{
// if (backlightIsDimmed) return;
2019-10-16 11:40:45 +00:00
backlightLevel /= 4; //make the display dimmer.
2019-10-13 11:31:43 +00:00
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();
}
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
if (backlightTimer.expired(backlightTimer_period)) backlightDim();
if (backlightLevel != backlightLevel_old) //update pwm duty cycle
2019-10-11 16:06:37 +00:00
{
analogWrite(LCD_BL_PIN, backlightLevel);
backlightLevel_old = backlightLevel;
2019-10-13 11:31:43 +00:00
if (!backlightIsDimmed) eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL, backlightLevel); //update eeprom value
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);
if (backlightSupport == 0) return;
//initialize backlight pin
2019-10-11 16:06:37 +00:00
SET_OUTPUT(LCD_BL_PIN);
WRITE(LCD_BL_PIN,0);
2019-10-13 11:31:43 +00:00
backlightTimer_reset(); //initializes eeprom data and starts backlightTimer
backlight_update(); //actually sets the backlight to the correct level
2019-10-11 16:06:37 +00:00
}
#endif //LCD_BL_PIN