backlight.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2013 Mathias Andersson <wraul@dbox.se>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "backlight.h"
  15. #include "eeconfig.h"
  16. #include "debug.h"
  17. backlight_config_t backlight_config;
  18. void backlight_init(void)
  19. {
  20. /* check signature */
  21. if (!eeconfig_is_enabled()) {
  22. eeconfig_init();
  23. }
  24. backlight_config.raw = eeconfig_read_backlight();
  25. if (backlight_config.level > BACKLIGHT_LEVELS) {
  26. backlight_config.level = BACKLIGHT_LEVELS;
  27. }
  28. backlight_set(backlight_config.enable ? backlight_config.level : 0);
  29. }
  30. void backlight_increase(void)
  31. {
  32. if(backlight_config.level < BACKLIGHT_LEVELS)
  33. {
  34. backlight_config.level++;
  35. }
  36. backlight_config.enable = 1;
  37. eeconfig_update_backlight(backlight_config.raw);
  38. dprintf("backlight increase: %u\n", backlight_config.level);
  39. backlight_set(backlight_config.level);
  40. }
  41. void backlight_decrease(void)
  42. {
  43. if(backlight_config.level > 0)
  44. {
  45. backlight_config.level--;
  46. backlight_config.enable = !!backlight_config.level;
  47. eeconfig_update_backlight(backlight_config.raw);
  48. }
  49. dprintf("backlight decrease: %u\n", backlight_config.level);
  50. backlight_set(backlight_config.level);
  51. }
  52. void backlight_toggle(void)
  53. {
  54. backlight_config.enable ^= 1;
  55. if (backlight_config.raw == 1) // enabled but level = 0
  56. backlight_config.level = 1;
  57. eeconfig_update_backlight(backlight_config.raw);
  58. dprintf("backlight toggle: %u\n", backlight_config.enable);
  59. backlight_set(backlight_config.enable ? backlight_config.level : 0);
  60. }
  61. void backlight_step(void)
  62. {
  63. backlight_config.level++;
  64. if(backlight_config.level > BACKLIGHT_LEVELS)
  65. {
  66. backlight_config.level = 0;
  67. }
  68. backlight_config.enable = !!backlight_config.level;
  69. eeconfig_update_backlight(backlight_config.raw);
  70. dprintf("backlight step: %u\n", backlight_config.level);
  71. backlight_set(backlight_config.level);
  72. }
  73. void backlight_level(uint8_t level)
  74. {
  75. if (level > BACKLIGHT_LEVELS)
  76. level = BACKLIGHT_LEVELS;
  77. backlight_config.level = level;
  78. backlight_config.enable = !!backlight_config.level;
  79. eeconfig_update_backlight(backlight_config.raw);
  80. backlight_set(backlight_config.level);
  81. }
  82. uint8_t get_backlight_level(void)
  83. {
  84. return backlight_config.level;
  85. }