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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifdef BACKLIGHT_ENABLE
  24. void backlight_enable(void);
  25. void backlight_disable(void);
  26. inline void backlight_set_raw(uint8_t raw);
  27. #ifndef SOFTPWM_LED_ENABLE
  28. #ifdef GH60_REV_CHN
  29. #else
  30. #define SOFTPWM_TIMER_TOP F_CPU/(256*64)
  31. uint8_t softpwm_ocr = 0;
  32. uint8_t softpwm_ocr_buff = 0;
  33. #endif
  34. #endif
  35. static const uint8_t backlight_table[] PROGMEM = {
  36. 0, 16, 128, 255
  37. };
  38. /* Backlight pin configuration
  39. * PWM: PB6 (Rev.CHN)
  40. * GPIO: PF7 PF6 PF5 PF4 (Rev.B)
  41. */
  42. void backlight_enable(void)
  43. {
  44. #ifdef SOFTPWM_LED_ENABLE
  45. #if defined(GH60_REV_CHN)
  46. DDRB |= (1<<PB6);
  47. PORTB &= ~(1<<PB6);
  48. #else
  49. DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  50. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  51. #endif
  52. softpwm_led_enable();
  53. #else
  54. #if defined(GH60_REV_CHN)
  55. // Turn on PWM
  56. DDRB |= (1<<PB6);
  57. cli();
  58. TCCR1A |= ((1<<WGM10) | (1<<COM1B1));
  59. TCCR1B |= ((1<<CS11) | (1<<CS10));
  60. sei();
  61. #else
  62. DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  63. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  64. cli();
  65. TCCR1B |= (1<<WGM12);
  66. TCCR1B |= (1<<CS10);
  67. OCR1AH = (SOFTPWM_TIMER_TOP>>8)&0xff;
  68. OCR1AL = SOFTPWM_TIMER_TOP&0xff;
  69. TIMSK1 |= (1<<OCIE1A);
  70. sei();
  71. #endif
  72. #endif
  73. }
  74. void backlight_disable(void)
  75. {
  76. #ifdef SOFTPWM_LED_ENABLE
  77. softpwm_led_disable();
  78. #if defined(GH60_REV_CHN)
  79. DDRB &= ~(1<<PB6);
  80. #else
  81. DDRF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  82. #endif
  83. #else
  84. #if defined(GH60_REV_CHN)
  85. // Turn off PWM
  86. cli();
  87. DDRB &= ~(1<<PB6);
  88. TCCR1A &= ~( (1<<WGM10) | (1<<COM1B1) );
  89. TCCR1B &= ~( (1<<CS11) | (1<<CS10) );
  90. sei();
  91. OCR1B = 0;
  92. #else
  93. DDRF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  94. cli();
  95. TCCR1B &= ~(1<<WGM12);
  96. TCCR1B &= ~(1<<CS10);
  97. TIMSK1 &= ~(1<<OCIE1A);
  98. sei();
  99. OCR1A = 0;
  100. #endif
  101. #endif
  102. }
  103. void backlight_set(uint8_t level)
  104. {
  105. #ifdef BREATHING_LED_ENABLE
  106. switch (level) {
  107. case 1:
  108. case 2:
  109. case 3:
  110. backlight_enable();
  111. breathing_led_disable();
  112. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  113. break;
  114. case 4:
  115. case 5:
  116. case 6:
  117. backlight_enable();
  118. breathing_led_enable();
  119. breathing_led_set_duration(6 - level);
  120. break;
  121. case 0:
  122. default:
  123. breathing_led_disable();
  124. backlight_disable();
  125. break;
  126. }
  127. #else
  128. if (level > 0) {
  129. backlight_enable();
  130. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  131. }
  132. else {
  133. backlight_disable();
  134. }
  135. #endif
  136. }
  137. #ifndef SOFTPWM_LED_ENABLE
  138. #ifdef BREATHING_LED_ENABLE
  139. void breathing_led_set_raw(uint8_t raw)
  140. {
  141. backlight_set_raw(raw);
  142. }
  143. #endif
  144. #endif
  145. inline void backlight_set_raw(uint8_t raw)
  146. {
  147. #ifdef SOFTPWM_LED_ENABLE
  148. softpwm_led_set(raw);
  149. #else
  150. #if defined(GH60_REV_CHN)
  151. OCR1B = raw;
  152. #else
  153. softpwm_ocr_buff = raw;
  154. #endif
  155. #endif
  156. }
  157. #ifdef SOFTPWM_LED_ENABLE
  158. void softpwm_led_on(void)
  159. {
  160. #if defined(GH60_REV_CHN)
  161. PORTB |= (1<<PB6);
  162. #else
  163. PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  164. #endif
  165. }
  166. void softpwm_led_off(void)
  167. {
  168. #if defined(GH60_REV_CHN)
  169. PORTB &= ~(1<<PB6);
  170. #else
  171. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  172. #endif
  173. }
  174. #endif
  175. #ifndef SOFTPWM_LED_ENABLE
  176. #if defined(GH60_REV_CHN)
  177. #else
  178. ISR(TIMER1_COMPA_vect)
  179. {
  180. static uint8_t pwm = 0;
  181. pwm++;
  182. // LED on
  183. if (pwm == 0) {
  184. //PORTB |= (1<<PB6);
  185. PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  186. softpwm_ocr = softpwm_ocr_buff;
  187. }
  188. // LED off
  189. if (pwm == softpwm_ocr) {
  190. //PORTB &= ~(1<<PB6);
  191. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  192. }
  193. }
  194. #endif
  195. #endif
  196. #endif