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

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