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 4.7KB

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