You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

backlight.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2014 Kai Ryu <[email protected]>
  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 <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <avr/pgmspace.h>
  17. #include "backlight.h"
  18. #include "breathing_led.h"
  19. #include "kimera.h"
  20. #ifdef BACKLIGHT_ENABLE
  21. void backlight_enable(void);
  22. void backlight_disable(void);
  23. inline void backlight_set_raw(uint8_t raw);
  24. static const uint8_t backlight_table[] PROGMEM = {
  25. 0, 16, 128, 255
  26. };
  27. /* Backlight pin configuration
  28. * LED4: PB6 (D10) OC1B
  29. */
  30. void backlight_enable(void)
  31. {
  32. // Turn on PWM
  33. LED4_DDR |= (1<<LED4_BIT);
  34. cli();
  35. TCCR1A |= ((1<<WGM10) | (1<<COM1B1));
  36. TCCR1B |= ((1<<CS11) | (1<<CS10));
  37. sei();
  38. }
  39. void backlight_disable(void)
  40. {
  41. // Turn off PWM
  42. LED4_DDR &= ~(1<<LED4_BIT);
  43. cli();
  44. TCCR1A &= ~((1<<WGM10) | (1<<COM1B1));
  45. TCCR1B &= ~((1<<CS11) | (1<<CS10));
  46. sei();
  47. LED4_OCR = 0;
  48. }
  49. void backlight_set(uint8_t level)
  50. {
  51. #ifdef BREATHING_LED_ENABLE
  52. switch (level) {
  53. case 1:
  54. case 2:
  55. case 3:
  56. backlight_enable();
  57. breathing_led_disable();
  58. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  59. break;
  60. case 4:
  61. case 5:
  62. case 6:
  63. backlight_enable();
  64. breathing_led_enable();
  65. breathing_led_set_duration(6 - level);
  66. break;
  67. case 0:
  68. default:
  69. breathing_led_disable();
  70. backlight_disable();
  71. break;
  72. }
  73. #else
  74. if (level > 0) {
  75. backlight_enable();
  76. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  77. }
  78. else {
  79. backlight_disable();
  80. }
  81. #endif
  82. }
  83. #ifdef BREATHING_LED_ENABLE
  84. void breathing_led_set_raw(uint8_t raw)
  85. {
  86. backlight_set_raw(raw);
  87. }
  88. #endif
  89. inline void backlight_set_raw(uint8_t raw)
  90. {
  91. LED4_OCR = raw;
  92. }
  93. #endif