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

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