backlight.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //backlight.cpp
  2. #include "backlight.h"
  3. #include <avr/eeprom.h>
  4. #include <Arduino.h>
  5. #include "eeprom.h"
  6. #include "Marlin.h"
  7. #include "pins.h"
  8. #include "fastio.h"
  9. #include "Timer.h"
  10. #ifdef LCD_BL_PIN
  11. bool backlightSupport = 0; //only if it's true will any of the settings be visible to the user
  12. int16_t backlightLevel_HIGH = 0;
  13. int16_t backlightLevel_LOW = 0;
  14. uint8_t backlightMode = BACKLIGHT_MODE_BRIGHT;
  15. unsigned long backlightTimer_period = 10000ul;
  16. LongTimer backlightTimer;
  17. void backlight_save() //saves all backlight data to eeprom.
  18. {
  19. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH, (uint8_t)backlightLevel_HIGH);
  20. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW, (uint8_t)backlightLevel_LOW);
  21. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_MODE, backlightMode);
  22. }
  23. 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.
  24. {
  25. if (!backlightSupport) return;
  26. backlightTimer.start();
  27. }
  28. void backlight_update()
  29. {
  30. if (!backlightSupport) return;
  31. if (backlightMode == BACKLIGHT_MODE_AUTO)
  32. {
  33. if (backlightTimer.expired(backlightTimer_period)) analogWrite(LCD_BL_PIN, backlightLevel_LOW);
  34. else if (backlightTimer.running()) analogWrite(LCD_BL_PIN, backlightLevel_HIGH);
  35. else {/*do nothing*/;} //display is dimmed.
  36. }
  37. else if (backlightMode == BACKLIGHT_MODE_DIM) analogWrite(LCD_BL_PIN, backlightLevel_LOW);
  38. else analogWrite(LCD_BL_PIN, backlightLevel_HIGH);
  39. }
  40. void backlight_init()
  41. {
  42. //check for backlight support on lcd
  43. SET_INPUT(LCD_BL_PIN);
  44. WRITE(LCD_BL_PIN,HIGH);
  45. _delay(10);
  46. backlightSupport = !READ(LCD_BL_PIN);
  47. if (!backlightSupport) return;
  48. //initialize backlight
  49. backlightMode = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_MODE);
  50. if (backlightMode == 0xFF) //set default values
  51. {
  52. backlightMode = BACKLIGHT_MODE_AUTO;
  53. backlightLevel_HIGH = 130;
  54. backlightLevel_LOW = 50;
  55. backlight_save();
  56. }
  57. backlightLevel_HIGH = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH);
  58. backlightLevel_LOW = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW);
  59. SET_OUTPUT(LCD_BL_PIN);
  60. backlightTimer_reset();
  61. backlight_update(); //sets brightness
  62. }
  63. #endif //LCD_BL_PIN