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

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