您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

backlight.c 2.3KB

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