Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

backlight.c 5.5KB

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