backlight.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. int16_t backlightTimer_period = 10;
  16. LongTimer backlightTimer;
  17. void force_bl_on(bool section_start)
  18. {
  19. if (section_start)
  20. {
  21. backlightMode = BACKLIGHT_MODE_BRIGHT;
  22. if (backlightLevel_HIGH < 30) backlightLevel_HIGH = 30;
  23. }
  24. else
  25. {
  26. backlightMode = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_MODE);
  27. backlightLevel_HIGH = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH);
  28. }
  29. backlight_update();
  30. }
  31. void backlight_save() //saves all backlight data to eeprom.
  32. {
  33. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH, (uint8_t)backlightLevel_HIGH);
  34. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW, (uint8_t)backlightLevel_LOW);
  35. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_MODE, backlightMode);
  36. eeprom_update_word((uint16_t *)EEPROM_BACKLIGHT_TIMEOUT, backlightTimer_period);
  37. }
  38. 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.
  39. {
  40. if (!backlightSupport) return;
  41. backlightTimer.start();
  42. }
  43. void backlight_update()
  44. {
  45. if (!backlightSupport) return;
  46. if (backlightMode == BACKLIGHT_MODE_AUTO)
  47. {
  48. if (backlightTimer.expired((uint32_t)backlightTimer_period * 1000ul)) analogWrite(LCD_BL_PIN, backlightLevel_LOW);
  49. else if (backlightTimer.running()) analogWrite(LCD_BL_PIN, backlightLevel_HIGH);
  50. else {/*do nothing*/;} //display is dimmed.
  51. }
  52. else if (backlightMode == BACKLIGHT_MODE_DIM) analogWrite(LCD_BL_PIN, backlightLevel_LOW);
  53. else analogWrite(LCD_BL_PIN, backlightLevel_HIGH);
  54. }
  55. void backlight_init()
  56. {
  57. //check for backlight support on lcd
  58. SET_INPUT(LCD_BL_PIN);
  59. WRITE(LCD_BL_PIN,HIGH);
  60. _delay(10);
  61. backlightSupport = !READ(LCD_BL_PIN);
  62. if (!backlightSupport) return;
  63. //initialize backlight
  64. backlightMode = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_MODE);
  65. if (backlightMode == 0xFF) //set default values
  66. {
  67. backlightMode = BACKLIGHT_MODE_AUTO;
  68. backlightLevel_HIGH = 130;
  69. backlightLevel_LOW = 50;
  70. backlightTimer_period = 10; //in seconds
  71. backlight_save();
  72. }
  73. backlightLevel_HIGH = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH);
  74. backlightLevel_LOW = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW);
  75. backlightTimer_period = eeprom_read_word((uint16_t *)EEPROM_BACKLIGHT_TIMEOUT);
  76. SET_OUTPUT(LCD_BL_PIN);
  77. backlightTimer_reset();
  78. backlight_update(); //sets brightness
  79. }
  80. #endif //LCD_BL_PIN