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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include "softpwm_led.h"
  19. #include "hook.h"
  20. #include "timer.h"
  21. #include "rgb.h"
  22. #ifdef BACKLIGHT_ENABLE
  23. static const uint8_t backlight_table[] PROGMEM = {
  24. 0, 16, 128, 255
  25. };
  26. extern backlight_config_t backlight_config;
  27. uint8_t backlight_brightness;
  28. void backlight_set(uint8_t level)
  29. {
  30. softpwm_enable();
  31. switch (level) {
  32. case 1:
  33. case 2:
  34. case 3:
  35. fading_led_disable_all();
  36. breathing_led_disable_all();
  37. backlight_brightness = pgm_read_byte(&backlight_table[level]);
  38. softpwm_led_set_all(backlight_brightness);
  39. rgb_set_brightness(backlight_brightness);
  40. softpwm_led_enable_all();
  41. break;
  42. case 4:
  43. case 5:
  44. case 6:
  45. fading_led_disable_all();
  46. breathing_led_set_duration(6 - level);
  47. breathing_led_set_index_all(0);
  48. breathing_led_enable_all();
  49. softpwm_led_enable_all();
  50. break;
  51. case 7:
  52. breathing_led_disable_all();
  53. breathing_led_set_duration(1);
  54. softpwm_led_set_all(0);
  55. fading_led_set_direction_all(FADING_LED_FADE_OUT);
  56. fading_led_set_duration(3);
  57. fading_led_enable_all();
  58. softpwm_led_enable_all();
  59. break;
  60. case 0:
  61. default:
  62. fading_led_disable_all();
  63. breathing_led_disable_all();
  64. backlight_brightness = 0;
  65. softpwm_led_set_all(backlight_brightness);
  66. softpwm_led_disable_all();
  67. break;
  68. }
  69. }
  70. #ifndef LEDMAP_ENABLE
  71. void softpwm_led_init(void)
  72. {
  73. DDRC |= (1<<PC2 | 1<<PC7);
  74. DDRD |= (1<<PD5 | 1<<PD6);
  75. DDRB |= (1<<PB0);
  76. }
  77. void softpwm_led_on(uint8_t index)
  78. {
  79. switch (index) {
  80. case 1:
  81. PORTC &= ~(1<<PC2);
  82. break;
  83. case 2:
  84. PORTC &= ~(1<<PC7);
  85. break;
  86. case 3:
  87. PORTD &= ~(1<<PD5);
  88. break;
  89. case 4:
  90. PORTD &= ~(1<<PD6);
  91. break;
  92. case 5:
  93. PORTB &= ~(1<<PB0);
  94. break;
  95. }
  96. }
  97. void softpwm_led_off(uint8_t index)
  98. {
  99. switch (index) {
  100. case 1:
  101. PORTC |= (1<<PC2);
  102. break;
  103. case 2:
  104. PORTC |= (1<<PC7);
  105. break;
  106. case 3:
  107. PORTD |= (1<<PD5);
  108. break;
  109. case 4:
  110. PORTD |= (1<<PD6);
  111. break;
  112. case 5:
  113. PORTB |= (1<<PB0);
  114. break;
  115. }
  116. }
  117. #endif
  118. static uint8_t idle_state = 0;
  119. static uint16_t idle_last = 0;
  120. void hook_matrix_change(keyevent_t event)
  121. {
  122. if (backlight_config.enable) {
  123. if (backlight_config.level == 7) {
  124. if (event.pressed) {
  125. if (idle_state > 1) {
  126. breathing_led_disable_all();
  127. softpwm_led_set_all(0);
  128. }
  129. idle_state = 0;
  130. uint8_t key = event.key.col + 1;
  131. fading_led_set_delay(key, 64);
  132. softpwm_led_increase(key, 32);
  133. }
  134. }
  135. }
  136. }
  137. #ifdef CUSTOM_LED_ENABLE
  138. void softpwm_led_custom(void)
  139. {
  140. rgb_fading();
  141. }
  142. void fading_led_custom(uint8_t *value)
  143. {
  144. static uint8_t index = 0;
  145. static uint16_t last = 0;
  146. if (backlight_config.level == 7) {
  147. if (idle_state == 0) {
  148. uint8_t max = value[0];
  149. for (uint8_t i = 1; i < LED_COUNT; i++) {
  150. if (value[i] > max) max = value[i];
  151. }
  152. rgb_set_brightness(max);
  153. if (max == 0) {
  154. idle_last = timer_read();
  155. idle_state = 1;
  156. }
  157. }
  158. if (idle_state == 1) {
  159. if (timer_elapsed(idle_last) > 3000) {
  160. breathing_led_set_index_all(0);
  161. index = 0;
  162. idle_state = 2;
  163. }
  164. }
  165. if (idle_state == 2) {
  166. if (timer_elapsed(last) > 500) {
  167. last = timer_read();
  168. breathing_led_enable_once(index);
  169. index = (index + 1) % 6;
  170. }
  171. }
  172. }
  173. }
  174. void breathing_led_custom(uint8_t *value)
  175. {
  176. rgb_set_brightness(value[0]);
  177. }
  178. #endif
  179. #endif