backlight.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. void force_bl_on(bool section_start)
  19. {
  20. if (section_start)
  21. {
  22. backlightMode = BACKLIGHT_MODE_BRIGHT;
  23. if (backlightLevel_HIGH < 30) backlightLevel_HIGH = 30;
  24. }
  25. else
  26. {
  27. backlightMode = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_MODE);
  28. backlightLevel_HIGH = eeprom_read_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH);
  29. }
  30. backlight_update();
  31. }
  32. void backlight_wake(const uint8_t flashNo)
  33. {
  34. if (!backlightSupport) return;
  35. if (flashNo)
  36. {
  37. uint8_t backlightMode_bck = backlightMode;
  38. for (uint8_t i = 0; i < (((backlightMode_bck == BACKLIGHT_MODE_AUTO) && !backlightTimer.running()) + (flashNo * 2)); i++)
  39. {
  40. backlightMode = !backlightMode; //toggles between BACKLIGHT_MODE_BRIGHT and BACKLIGHT_MODE_DIM
  41. backlight_update();
  42. _delay(BL_FLASH_DELAY_MS);
  43. }
  44. backlightMode = backlightMode_bck;
  45. }
  46. backlightTimer_reset();
  47. }
  48. void backlight_save() //saves all backlight data to eeprom.
  49. {
  50. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_HIGH, (uint8_t)backlightLevel_HIGH);
  51. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_LEVEL_LOW, (uint8_t)backlightLevel_LOW);
  52. eeprom_update_byte((uint8_t *)EEPROM_BACKLIGHT_MODE, backlightMode);
  53. eeprom_update_word((uint16_t *)EEPROM_BACKLIGHT_TIMEOUT, backlightTimer_period);
  54. }
  55. void backlightTimer_reset() //used for resetting the timer and waking the display. Triggered on user interactions.
  56. {
  57. if (!backlightSupport) return;
  58. backlightTimer.start();
  59. backlight_update();
  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. #endif //LCD_BL_PIN