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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Copyright 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. #include "kimera.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. #ifdef SOFTPWM_LED_ENABLE
  33. #ifdef FADING_LED_ENABLE
  34. static uint8_t backlight_mode;
  35. #endif
  36. #endif
  37. /* Backlight pin configuration
  38. * LED4: PB6 (D10) OC1B
  39. */
  40. #ifndef SOFTPWM_LED_ENABLE
  41. void backlight_enable(void)
  42. {
  43. // Turn on PWM
  44. LED4_DDR |= (1<<LED4_BIT);
  45. cli();
  46. TCCR1A |= ((1<<WGM10) | (1<<COM1B1));
  47. TCCR1B |= ((1<<CS11) | (1<<CS10));
  48. sei();
  49. }
  50. #endif
  51. #ifndef SOFTPWM_LED_ENABLE
  52. void backlight_disable(void)
  53. {
  54. // Turn off PWM
  55. LED4_DDR &= ~(1<<LED4_BIT);
  56. cli();
  57. TCCR1A &= ~((1<<WGM10) | (1<<COM1B1));
  58. TCCR1B &= ~((1<<CS11) | (1<<CS10));
  59. sei();
  60. LED4_OCR = 0;
  61. }
  62. #endif
  63. void backlight_set(uint8_t level)
  64. {
  65. #ifdef FADING_LED_ENABLE
  66. backlight_mode = level;
  67. #endif
  68. #ifdef BREATHING_LED_ENABLE
  69. switch (level) {
  70. case 1:
  71. case 2:
  72. case 3:
  73. #ifdef SOFTPWM_LED_ENABLE
  74. softpwm_led_enable();
  75. #ifdef FADING_LED_ENABLE
  76. fading_led_disable_all();
  77. #endif
  78. breathing_led_disable_all();
  79. #else
  80. backlight_enable();
  81. breathing_led_disable();
  82. #endif
  83. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  84. break;
  85. case 4:
  86. case 5:
  87. case 6:
  88. #ifdef SOFTPWM_LED_ENABLE
  89. softpwm_led_enable();
  90. #ifdef FADING_LED_ENABLE
  91. fading_led_disable_all();
  92. #endif
  93. breathing_led_enable_all();
  94. #else
  95. backlight_enable();
  96. breathing_led_enable();
  97. #endif
  98. breathing_led_set_duration(6 - level);
  99. break;
  100. #ifdef SOFTPWM_LED_ENABLE
  101. #ifdef FADING_LED_ENABLE
  102. case 7:
  103. softpwm_led_enable();
  104. fading_led_enable_all();
  105. breathing_led_disable_all();
  106. fading_led_set_direction_all(FADING_LED_FADE_IN);
  107. fading_led_set_duration(3);
  108. break;
  109. case 8:
  110. softpwm_led_enable();
  111. fading_led_enable_all();
  112. breathing_led_disable_all();
  113. fading_led_set_direction_all(FADING_LED_FADE_OUT);
  114. fading_led_set_duration(3);
  115. break;
  116. #endif
  117. #endif
  118. case 0:
  119. default:
  120. #ifdef SOFTPWM_LED_ENABLE
  121. #ifdef FADING_LED_ENABLE
  122. fading_led_disable_all();
  123. #endif
  124. breathing_led_disable_all();
  125. softpwm_led_disable();
  126. #else
  127. breathing_led_disable();
  128. backlight_disable();
  129. #endif
  130. break;
  131. }
  132. #else
  133. if (level > 0) {
  134. backlight_enable();
  135. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  136. }
  137. else {
  138. backlight_disable();
  139. }
  140. #endif
  141. }
  142. #ifndef SOFTPWM_LED_ENABLE
  143. #ifdef BREATHING_LED_ENABLE
  144. void breathing_led_set_raw(uint8_t raw)
  145. {
  146. backlight_set_raw(raw);
  147. }
  148. #endif
  149. #endif
  150. inline void backlight_set_raw(uint8_t raw)
  151. {
  152. #ifdef SOFTPWM_LED_ENABLE
  153. softpwm_led_set_all(raw);
  154. #else
  155. LED4_OCR = raw;
  156. #endif
  157. }
  158. #ifndef LEDMAP_ENABLE
  159. #ifdef SOFTPWM_LED_ENABLE
  160. void softpwm_led_init(void)
  161. {
  162. LED4_DDR |= (1<<LED4_BIT);
  163. }
  164. void softpwm_led_on(uint8_t index)
  165. {
  166. LED4_PORT |= (1<<LED4_BIT);
  167. }
  168. void softpwm_led_off(uint8_t index)
  169. {
  170. LED4_PORT &= ~(1<<LED4_BIT);
  171. }
  172. #endif
  173. #endif
  174. #ifdef SOFTPWM_LED_ENABLE
  175. #ifdef FADING_LED_ENABLE
  176. void action_keyevent(keyevent_t event)
  177. {
  178. if (backlight_mode == 7) {
  179. if (event.pressed) {
  180. softpwm_led_decrease_all(32);
  181. }
  182. }
  183. if (backlight_mode == 8) {
  184. if (event.pressed) {
  185. softpwm_led_increase_all(32);
  186. }
  187. }
  188. }
  189. #endif
  190. #endif
  191. #endif