Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

backlight.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifdef SOFTPWM_LED_ENABLE
  19. #include "softpwm_led.h"
  20. #else
  21. #include "breathing_led.h"
  22. #endif
  23. #include "action.h"
  24. #ifdef BACKLIGHT_ENABLE
  25. static uint8_t backlight_mode;
  26. static const uint8_t backlight_table[] PROGMEM = {
  27. 0, 16, 128, 255
  28. };
  29. void backlight_enable(void);
  30. void backlight_disable(void);
  31. inline void backlight_set_raw(uint8_t raw);
  32. /* Backlight pin configuration
  33. * PWM: PB7(OC1C)
  34. */
  35. void backlight_enable(void)
  36. {
  37. #ifdef SOFTPWM_LED_ENABLE
  38. DDRB |= (1<<PB7);
  39. softpwm_led_enable();
  40. #else
  41. // Turn on PWM
  42. DDRB |= (1<<PB7);
  43. cli();
  44. TCCR1A |= ((1<<WGM10) | (1<<COM1C1));
  45. TCCR1B |= ((1<<CS11) | (1<<CS10));
  46. sei();
  47. #endif
  48. }
  49. void backlight_disable(void)
  50. {
  51. #ifdef SOFTPWM_LED_ENABLE
  52. DDRB &= ~(1<<PB7);
  53. softpwm_led_disable();
  54. #else
  55. // Turn off PWM
  56. cli();
  57. DDRB &= ~(1<<PB7);
  58. TCCR1A &= ~( (1<<WGM10) | (1<<COM1C1) );
  59. TCCR1B &= ~( (1<<CS11) | (1<<CS10) );
  60. sei();
  61. OCR1C = 0;
  62. #endif
  63. }
  64. void backlight_set(uint8_t level)
  65. {
  66. #ifdef BREATHING_LED_ENABLE
  67. switch (level) {
  68. case 1:
  69. case 2:
  70. case 3:
  71. backlight_enable();
  72. #ifdef SOFTPWM_LED_ENABLE
  73. #ifdef FADING_LED_ENABLE
  74. fading_led_disable_all();
  75. #endif
  76. breathing_led_disable_all();
  77. #else
  78. breathing_led_disable();
  79. #endif
  80. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  81. break;
  82. case 4:
  83. case 5:
  84. case 6:
  85. backlight_enable();
  86. #ifdef SOFTPWM_LED_ENABLE
  87. #ifdef FADING_LED_ENABLE
  88. fading_led_disable_all();
  89. #endif
  90. breathing_led_enable_all();
  91. #else
  92. breathing_led_enable();
  93. #endif
  94. breathing_led_set_duration(6 - level);
  95. break;
  96. #ifdef FADING_LED_ENABLE
  97. case 7:
  98. backlight_enable();
  99. fading_led_enable_all();
  100. breathing_led_disable_all();
  101. fading_led_set_direction(FADING_LED_FADE_IN);
  102. fading_led_set_duration(3);
  103. case 8:
  104. backlight_enable();
  105. fading_led_enable_all();
  106. breathing_led_disable_all();
  107. fading_led_set_direction(FADING_LED_FADE_OUT);
  108. fading_led_set_duration(3);
  109. #endif
  110. case 0:
  111. default:
  112. #ifdef SOFTPWM_LED_ENABLE
  113. #ifdef FADING_LED_ENABLE
  114. fading_led_disable_all();
  115. #endif
  116. breathing_led_disable_all();
  117. #else
  118. breathing_led_disable();
  119. #endif
  120. backlight_disable();
  121. break;
  122. }
  123. #else
  124. if (level > 0) {
  125. backlight_enable();
  126. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  127. }
  128. else {
  129. backlight_disable();
  130. }
  131. #endif
  132. }
  133. #ifndef SOFTPWM_LED_ENABLE
  134. #ifdef BREATHING_LED_ENABLE
  135. void breathing_led_set_raw(uint8_t raw)
  136. {
  137. backlight_set_raw(raw);
  138. }
  139. #endif
  140. #endif
  141. inline void backlight_set_raw(uint8_t raw)
  142. {
  143. #ifdef SOFTPWM_LED_ENABLE
  144. softpwm_led_set_all(raw);
  145. #else
  146. OCR1C = raw;
  147. #endif
  148. }
  149. #ifndef LEDMAP_ENABLE
  150. #ifdef SOFTPWM_LED_ENABLE
  151. void softpwm_led_on(uint8_t index)
  152. {
  153. PORTB |= (1<<PB7);
  154. }
  155. void softpwm_led_off(uint8_t index)
  156. {
  157. PORTB &= ~(1<<PB7);
  158. }
  159. #endif
  160. #endif
  161. #ifdef SOFTPWM_LED_ENABLE
  162. #ifdef FADING_LED_ENABLE
  163. void action_keyevent(keyevent_t event)
  164. {
  165. if (backlight_mode == 7) {
  166. if (event.pressed) {
  167. softpwm_led_decrease_all(32);
  168. }
  169. }
  170. if (backlight_mode == 8) {
  171. if (event.pressed) {
  172. softpwm_led_increase_all(32);
  173. }
  174. }
  175. }
  176. #endif
  177. #endif
  178. #endif