backlight.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define BL_FLASH_DELAY_MS 25
  12. bool backlightSupport = 0; //only if it's true will any of the settings be visible to the user
  13. int16_t backlightLevel_HIGH = 0;
  14. int16_t backlightLevel_LOW = 0;
  15. uint8_t backlightMode = BACKLIGHT_MODE_BRIGHT;
  16. int16_t backlightTimer_period = 10;
  17. LongTimer backlightTimer;
  18. static void backlightTimer_reset() //used for resetting the timer and waking the display. Triggered on user interactions.
  19. {
  20. if (!backlightSupport) return;
  21. backlightTimer.start();
  22. backlight_update();
  23. }
  24. void force_bl_on(bool section_start)
  25. {
  26. if (section_start)
  27. {
  28. backlightMode = BACKLIGHT_MODE_BRIGHT;
  29. if (backlightLevel_HIGH < 30) backlightLevel_HIGH = 30;
  30. }
  31. else
  32. {
  33. backlightMode = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_MODE);
  34. backlightLevel_HIGH = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH);
  35. }
  36. backlight_update();
  37. }
  38. void backlight_wake(const uint8_t flashNo)
  39. {
  40. if (!backlightSupport) return;
  41. if (flashNo)
  42. {
  43. uint8_t backlightMode_bck = backlightMode;
  44. for (uint8_t i = 0; i < (((backlightMode_bck == BACKLIGHT_MODE_AUTO) && !backlightTimer.running()) + (flashNo * 2)); i++)
  45. {
  46. backlightMode = !backlightMode; //toggles between BACKLIGHT_MODE_BRIGHT and BACKLIGHT_MODE_DIM
  47. backlight_update();
  48. _delay(BL_FLASH_DELAY_MS);
  49. }
  50. backlightMode = backlightMode_bck;
  51. }
  52. backlightTimer_reset();
  53. }
  54. void backlight_save() //saves all backlight data to eeprom.
  55. {
  56. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH, (uint8_t)backlightLevel_HIGH);
  57. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW, (uint8_t)backlightLevel_LOW);
  58. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_MODE, backlightMode);
  59. eeprom_update_word((uint16_t *)EEPROM_BACKLIGHT_TIMEOUT, backlightTimer_period);
  60. }
  61. void backlight_update()
  62. {
  63. if (!backlightSupport) return;
  64. if (backlightMode == BACKLIGHT_MODE_AUTO)
  65. {
  66. if (backlightTimer.expired((uint32_t)backlightTimer_period * 1000ul)) analogWrite(LCD_BL_PIN, backlightLevel_LOW);
  67. else if (backlightTimer.running()) analogWrite(LCD_BL_PIN, backlightLevel_HIGH);
  68. else {/*do nothing*/;} //display is dimmed.
  69. }
  70. else if (backlightMode == BACKLIGHT_MODE_DIM) analogWrite(LCD_BL_PIN, backlightLevel_LOW);
  71. else analogWrite(LCD_BL_PIN, backlightLevel_HIGH);
  72. }
  73. void backlight_init()
  74. {
  75. //check for backlight support on lcd
  76. SET_INPUT(LCD_BL_PIN);
  77. WRITE(LCD_BL_PIN,HIGH);
  78. _delay(10);
  79. backlightSupport = !READ(LCD_BL_PIN);
  80. if (!backlightSupport) return;
  81. //initialize backlight
  82. backlightMode = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_MODE);
  83. if (backlightMode == 0xFF) //set default values
  84. {
  85. backlightMode = BACKLIGHT_MODE_AUTO;
  86. backlightLevel_HIGH = 130;
  87. backlightLevel_LOW = 50;
  88. backlightTimer_period = 10; //in seconds
  89. backlight_save();
  90. }
  91. backlightLevel_HIGH = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH);
  92. backlightLevel_LOW = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW);
  93. backlightTimer_period = eeprom_read_word((uint16_t *)EEPROM_BACKLIGHT_TIMEOUT);
  94. SET_OUTPUT(LCD_BL_PIN);
  95. backlightTimer_reset();
  96. }
  97. #else //LCD_BL_PIN
  98. void force_bl_on(__attribute__((unused)) bool section_start) {}
  99. void backlight_update() {}
  100. void backlight_init() {}
  101. void backlight_save() {}
  102. void backlight_wake(__attribute__((unused)) const uint8_t flashNo) {}
  103. #endif //LCD_BL_PIN