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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_led_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. #ifdef FADING_LED_ENABLE
  73. fading_led_disable_all();
  74. #endif
  75. breathing_led_disable_all();
  76. #else
  77. backlight_enable();
  78. breathing_led_disable();
  79. #endif
  80. backlight_brightness = pgm_read_byte(&backlight_table[level]);
  81. #ifdef RGB_LED_ENABLE
  82. rgb_set_brightness(backlight_brightness);
  83. #endif
  84. backlight_set_raw(backlight_brightness);
  85. break;
  86. case 4:
  87. case 5:
  88. case 6:
  89. #ifdef SOFTPWM_LED_ENABLE
  90. #ifdef FADING_LED_ENABLE
  91. fading_led_disable_all();
  92. #endif
  93. breathing_led_enable_all();
  94. #else
  95. backlight_enable();
  96. breathing_led_enable();
  97. #endif
  98. breathing_led_set_duration(6 - level);
  99. break;
  100. #ifdef SOFTPWM_LED_ENABLE
  101. #ifdef FADING_LED_ENABLE
  102. case 7:
  103. fading_led_enable_all();
  104. breathing_led_disable_all();
  105. fading_led_set_direction_all(FADING_LED_FADE_IN);
  106. fading_led_set_duration(3);
  107. break;
  108. case 8:
  109. fading_led_enable_all();
  110. breathing_led_disable_all();
  111. fading_led_set_direction_all(FADING_LED_FADE_OUT);
  112. fading_led_set_duration(3);
  113. break;
  114. #endif
  115. #endif
  116. case 0:
  117. default:
  118. #ifdef SOFTPWM_LED_ENABLE
  119. #ifdef FADING_LED_ENABLE
  120. fading_led_disable_all();
  121. #endif
  122. breathing_led_disable_all();
  123. backlight_brightness = 0;
  124. backlight_set_raw(backlight_brightness);
  125. #else
  126. breathing_led_disable();
  127. backlight_disable();
  128. #endif
  129. break;
  130. }
  131. #else
  132. if (level > 0) {
  133. backlight_enable();
  134. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  135. }
  136. else {
  137. backlight_disable();
  138. }
  139. #endif
  140. }
  141. #ifndef SOFTPWM_LED_ENABLE
  142. #ifdef BREATHING_LED_ENABLE
  143. void breathing_led_set_raw(uint8_t raw)
  144. {
  145. backlight_set_raw(raw);
  146. }
  147. #endif
  148. #endif
  149. inline void backlight_set_raw(uint8_t raw)
  150. {
  151. #ifdef SOFTPWM_LED_ENABLE
  152. softpwm_led_set_all(raw);
  153. #else
  154. LED4_OCR = raw;
  155. #endif
  156. }
  157. #ifndef LEDMAP_ENABLE
  158. #ifdef SOFTPWM_LED_ENABLE
  159. void softpwm_led_init(void)
  160. {
  161. LED4_DDR |= (1<<LED4_BIT);
  162. }
  163. void softpwm_led_on(uint8_t index)
  164. {
  165. LED4_PORT |= (1<<LED4_BIT);
  166. }
  167. void softpwm_led_off(uint8_t index)
  168. {
  169. LED4_PORT &= ~(1<<LED4_BIT);
  170. }
  171. #endif
  172. #endif
  173. #ifdef SOFTPWM_LED_ENABLE
  174. #ifdef FADING_LED_ENABLE
  175. void action_keyevent(keyevent_t event)
  176. {
  177. if (backlight_config.enable) {
  178. if (backlight_config.level == 7) {
  179. if (event.pressed) {
  180. fading_led_set_delay_all(64);
  181. softpwm_led_decrease_all(32);
  182. }
  183. }
  184. if (backlight_config.level == 8) {
  185. if (event.pressed) {
  186. fading_led_set_delay_all(64);
  187. softpwm_led_increase_all(32);
  188. }
  189. }
  190. }
  191. }
  192. #endif
  193. #ifdef RGB_LED_ENABLE
  194. #ifdef CUSTOM_LED_ENABLE
  195. void softpwm_led_custom(void)
  196. {
  197. rgb_fading();
  198. }
  199. void fading_led_custom(uint8_t *value)
  200. {
  201. rgb_set_brightness(value[0]);
  202. }
  203. void breathing_led_custom(uint8_t *value)
  204. {
  205. rgb_set_brightness(value[0]);
  206. }
  207. #endif
  208. #endif
  209. #endif
  210. #endif