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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "hook.h"
  24. #ifdef BACKLIGHT_ENABLE
  25. static uint8_t backlight_mode;
  26. void backlight_enable(void);
  27. void backlight_disable(void);
  28. inline void backlight_set_raw(uint8_t raw);
  29. #ifndef SOFTPWM_LED_ENABLE
  30. #ifdef GH60_REV_CHN
  31. #else
  32. #define SOFTPWM_TIMER_TOP F_CPU/(256*64)
  33. uint8_t softpwm_ocr = 0;
  34. uint8_t softpwm_ocr_buff = 0;
  35. #endif
  36. #endif
  37. static const uint8_t backlight_table[] PROGMEM = {
  38. 0, 16, 128, 255
  39. };
  40. /* Backlight pin configuration
  41. * PWM: PB6 (Rev.CHN)
  42. * GPIO: PF7 PF6 PF5 PF4 (Rev.B)
  43. */
  44. void backlight_enable(void)
  45. {
  46. #ifdef SOFTPWM_LED_ENABLE
  47. softpwm_led_enable_all();
  48. #else
  49. #if defined(GH60_REV_CHN)
  50. // Turn on PWM
  51. DDRB |= (1<<PB6);
  52. cli();
  53. TCCR1A |= ((1<<WGM10) | (1<<COM1B1));
  54. TCCR1B |= ((1<<CS11) | (1<<CS10));
  55. sei();
  56. #else
  57. DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  58. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  59. cli();
  60. TCCR1B |= (1<<WGM12);
  61. TCCR1B |= (1<<CS10);
  62. OCR1AH = (SOFTPWM_TIMER_TOP>>8)&0xff;
  63. OCR1AL = SOFTPWM_TIMER_TOP&0xff;
  64. TIMSK1 |= (1<<OCIE1A);
  65. sei();
  66. #endif
  67. #endif
  68. }
  69. void backlight_disable(void)
  70. {
  71. #ifdef SOFTPWM_LED_ENABLE
  72. softpwm_led_disable_all();
  73. #else
  74. #if defined(GH60_REV_CHN)
  75. // Turn off PWM
  76. cli();
  77. DDRB &= ~(1<<PB6);
  78. TCCR1A &= ~( (1<<WGM10) | (1<<COM1B1) );
  79. TCCR1B &= ~( (1<<CS11) | (1<<CS10) );
  80. sei();
  81. OCR1B = 0;
  82. #else
  83. DDRF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  84. cli();
  85. TCCR1B &= ~(1<<WGM12);
  86. TCCR1B &= ~(1<<CS10);
  87. TIMSK1 &= ~(1<<OCIE1A);
  88. sei();
  89. OCR1A = 0;
  90. #endif
  91. #endif
  92. }
  93. void backlight_set(uint8_t level)
  94. {
  95. backlight_mode = level;
  96. softpwm_enable();
  97. #ifdef BREATHING_LED_ENABLE
  98. switch (level) {
  99. case 1:
  100. case 2:
  101. case 3:
  102. backlight_enable();
  103. #ifdef SOFTPWM_LED_ENABLE
  104. fading_led_disable_all();
  105. breathing_led_disable_all();
  106. #else
  107. breathing_led_disable();
  108. #endif
  109. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  110. break;
  111. case 4:
  112. case 5:
  113. case 6:
  114. backlight_enable();
  115. #ifdef SOFTPWM_LED_ENABLE
  116. breathing_led_enable_all();
  117. fading_led_disable_all();
  118. breathing_led_set_duration(6 - level);
  119. #else
  120. breathing_led_enable();
  121. breathing_led_set_duration(6 - level);
  122. #endif
  123. break;
  124. case 7:
  125. backlight_enable();
  126. fading_led_enable_all();
  127. breathing_led_disable_all();
  128. fading_led_set_direction_all(FADING_LED_FADE_IN);
  129. fading_led_set_duration(3);
  130. break;
  131. case 8:
  132. backlight_enable();
  133. fading_led_enable_all();
  134. breathing_led_disable_all();
  135. fading_led_set_direction_all(FADING_LED_FADE_OUT);
  136. fading_led_set_duration(3);
  137. break;
  138. case 0:
  139. default:
  140. #ifdef SOFTPWM_LED_ENABLE
  141. fading_led_disable_all();
  142. breathing_led_disable_all();
  143. #else
  144. breathing_led_disable();
  145. #endif
  146. backlight_disable();
  147. break;
  148. }
  149. #else
  150. if (level > 0) {
  151. backlight_enable();
  152. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  153. }
  154. else {
  155. backlight_disable();
  156. }
  157. #endif
  158. }
  159. #ifndef SOFTPWM_LED_ENABLE
  160. #ifdef BREATHING_LED_ENABLE
  161. void breathing_led_set_raw(uint8_t raw)
  162. {
  163. backlight_set_raw(raw);
  164. }
  165. #endif
  166. #endif
  167. inline void backlight_set_raw(uint8_t raw)
  168. {
  169. #ifdef SOFTPWM_LED_ENABLE
  170. for (uint8_t i = 0; i < LED_COUNT; i++) {
  171. softpwm_led_set(i, raw);
  172. }
  173. #else
  174. #if defined(GH60_REV_CHN)
  175. OCR1B = raw;
  176. #else
  177. softpwm_ocr_buff = raw;
  178. #endif
  179. #endif
  180. }
  181. #ifdef SOFTPWM_LED_ENABLE
  182. #ifndef LEDMAP_ENABLE
  183. void softpwm_led_init(void)
  184. {
  185. #if defined(GH60_REV_CHN)
  186. DDRB |= (1<<PB6);
  187. #else
  188. DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  189. #endif
  190. }
  191. void softpwm_led_on(uint8_t index)
  192. {
  193. #if defined(GH60_REV_CHN)
  194. PORTB |= (1<<PB6);
  195. #else
  196. PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  197. #endif
  198. }
  199. void softpwm_led_off(uint8_t index)
  200. {
  201. #if defined(GH60_REV_CHN)
  202. PORTB &= ~(1<<PB6);
  203. #else
  204. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  205. #endif
  206. }
  207. #endif
  208. #endif
  209. void hook_matrix_change(keyevent_t event)
  210. {
  211. if (backlight_mode == 7) {
  212. if (event.pressed) {
  213. fading_led_set_delay_all(32);
  214. softpwm_led_decrease_all(16);
  215. }
  216. }
  217. if (backlight_mode == 8) {
  218. if (event.pressed) {
  219. fading_led_set_delay_all(32);
  220. softpwm_led_increase_all(16);
  221. }
  222. }
  223. }
  224. #ifndef SOFTPWM_LED_ENABLE
  225. #if defined(GH60_REV_CHN)
  226. #else
  227. ISR(TIMER1_COMPA_vect)
  228. {
  229. static uint8_t pwm = 0;
  230. pwm++;
  231. // LED on
  232. if (pwm == 0) {
  233. //PORTB |= (1<<PB6);
  234. PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  235. softpwm_ocr = softpwm_ocr_buff;
  236. }
  237. // LED off
  238. if (pwm == softpwm_ocr) {
  239. //PORTB &= ~(1<<PB6);
  240. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  241. }
  242. }
  243. #endif
  244. #endif
  245. #endif