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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "action.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();
  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();
  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. #ifdef BREATHING_LED_ENABLE
  97. switch (level) {
  98. case 1:
  99. case 2:
  100. case 3:
  101. backlight_enable();
  102. #ifdef SOFTPWM_LED_ENABLE
  103. fading_led_disable_all();
  104. breathing_led_disable_all();
  105. #else
  106. breathing_led_disable();
  107. #endif
  108. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  109. break;
  110. case 4:
  111. case 5:
  112. case 6:
  113. backlight_enable();
  114. #ifdef SOFTPWM_LED_ENABLE
  115. breathing_led_enable_all();
  116. fading_led_disable_all();
  117. breathing_led_set_duration(6 - level);
  118. #else
  119. breathing_led_enable();
  120. breathing_led_set_duration(6 - level);
  121. #endif
  122. break;
  123. case 7:
  124. backlight_enable();
  125. fading_led_enable_all();
  126. breathing_led_disable_all();
  127. fading_led_set_direction_all(FADING_LED_FADE_IN);
  128. fading_led_set_duration(3);
  129. break;
  130. case 8:
  131. backlight_enable();
  132. fading_led_enable_all();
  133. breathing_led_disable_all();
  134. fading_led_set_direction_all(FADING_LED_FADE_OUT);
  135. fading_led_set_duration(3);
  136. break;
  137. case 0:
  138. default:
  139. #ifdef SOFTPWM_LED_ENABLE
  140. fading_led_disable_all();
  141. breathing_led_disable_all();
  142. #else
  143. breathing_led_disable();
  144. #endif
  145. backlight_disable();
  146. break;
  147. }
  148. #else
  149. if (level > 0) {
  150. backlight_enable();
  151. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  152. }
  153. else {
  154. backlight_disable();
  155. }
  156. #endif
  157. }
  158. #ifndef SOFTPWM_LED_ENABLE
  159. #ifdef BREATHING_LED_ENABLE
  160. void breathing_led_set_raw(uint8_t raw)
  161. {
  162. backlight_set_raw(raw);
  163. }
  164. #endif
  165. #endif
  166. inline void backlight_set_raw(uint8_t raw)
  167. {
  168. #ifdef SOFTPWM_LED_ENABLE
  169. for (uint8_t i = 0; i < LED_COUNT; i++) {
  170. softpwm_led_set(i, raw);
  171. }
  172. #else
  173. #if defined(GH60_REV_CHN)
  174. OCR1B = raw;
  175. #else
  176. softpwm_ocr_buff = raw;
  177. #endif
  178. #endif
  179. }
  180. #ifdef SOFTPWM_LED_ENABLE
  181. #ifndef LEDMAP_ENABLE
  182. void softpwm_led_init(void)
  183. {
  184. #if defined(GH60_REV_CHN)
  185. DDRB |= (1<<PB6);
  186. #else
  187. DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  188. #endif
  189. }
  190. void softpwm_led_on(uint8_t index)
  191. {
  192. #if defined(GH60_REV_CHN)
  193. PORTB |= (1<<PB6);
  194. #else
  195. PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  196. #endif
  197. }
  198. void softpwm_led_off(uint8_t index)
  199. {
  200. #if defined(GH60_REV_CHN)
  201. PORTB &= ~(1<<PB6);
  202. #else
  203. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  204. #endif
  205. }
  206. #endif
  207. #endif
  208. void action_keyevent(keyevent_t event)
  209. {
  210. if (backlight_mode == 7) {
  211. if (event.pressed) {
  212. softpwm_led_decrease_all(32);
  213. }
  214. }
  215. if (backlight_mode == 8) {
  216. if (event.pressed) {
  217. softpwm_led_increase_all(32);
  218. }
  219. }
  220. }
  221. #ifndef SOFTPWM_LED_ENABLE
  222. #if defined(GH60_REV_CHN)
  223. #else
  224. ISR(TIMER1_COMPA_vect)
  225. {
  226. static uint8_t pwm = 0;
  227. pwm++;
  228. // LED on
  229. if (pwm == 0) {
  230. //PORTB |= (1<<PB6);
  231. PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  232. softpwm_ocr = softpwm_ocr_buff;
  233. }
  234. // LED off
  235. if (pwm == softpwm_ocr) {
  236. //PORTB &= ~(1<<PB6);
  237. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  238. }
  239. }
  240. #endif
  241. #endif
  242. #endif