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

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