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

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