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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #define BACKLIGHT 0
  23. extern backlight_config_t backlight_config;
  24. uint8_t backlight_brightness;
  25. static const uint8_t backlight_table[] PROGMEM = {
  26. 0, 16, 128, 255
  27. };
  28. void backlight_set(uint8_t level)
  29. {
  30. #ifdef BREATHING_LED_ENABLE
  31. switch (level) {
  32. case 1:
  33. case 2:
  34. case 3:
  35. #ifdef FADING_LED_ENABLE
  36. fading_led_disable(BACKLIGHT);
  37. #endif
  38. breathing_led_disable(BACKLIGHT);
  39. backlight_brightness = pgm_read_byte(&backlight_table[level]);
  40. softpwm_led_set(BACKLIGHT, backlight_brightness);
  41. rgb_set_brightness(backlight_brightness);
  42. break;
  43. case 4:
  44. case 5:
  45. case 6:
  46. #ifdef FADING_LED_ENABLE
  47. fading_led_disable(BACKLIGHT);
  48. #endif
  49. breathing_led_enable(BACKLIGHT);
  50. breathing_led_set_duration(6 - level);
  51. break;
  52. #ifdef FADING_LED_ENABLE
  53. case 7:
  54. fading_led_enable(BACKLIGHT);
  55. breathing_led_disable(BACKLIGHT);
  56. fading_led_set_direction(BACKLIGHT, FADING_LED_FADE_IN);
  57. fading_led_set_duration(3);
  58. break;
  59. case 8:
  60. fading_led_enable(BACKLIGHT);
  61. breathing_led_disable(BACKLIGHT);
  62. fading_led_set_direction(BACKLIGHT, FADING_LED_FADE_OUT);
  63. fading_led_set_duration(3);
  64. break;
  65. #endif
  66. case 0:
  67. default:
  68. #ifdef FADING_LED_ENABLE
  69. fading_led_disable(BACKLIGHT);
  70. #endif
  71. breathing_led_disable(BACKLIGHT);
  72. backlight_brightness = 0;
  73. softpwm_led_set(BACKLIGHT, backlight_brightness);
  74. break;
  75. }
  76. #else
  77. if (level > 0) {
  78. softpwm_led_set(BACKLIGHT, pgm_read_byte(&backlight_table[level]));
  79. }
  80. else {
  81. softpwm_led_set(BACKLIGHT, 0);
  82. }
  83. #endif
  84. }
  85. #ifndef LEDMAP_ENABLE
  86. #ifdef SOFTPWM_LED_ENABLE
  87. /* Backlight pin configuration
  88. * Backlight: PF7
  89. * RGB R: PF6
  90. * RGB G: PF5
  91. * RGB B: PF4
  92. */
  93. void softpwm_led_init()
  94. {
  95. DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  96. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  97. }
  98. void softpwm_led_on(uint8_t index)
  99. {
  100. if (index) {
  101. PORTF &= ~((1<<PF7) >> index);
  102. }
  103. else {
  104. PORTF |= (1<<PF7);
  105. }
  106. /*
  107. switch (index) {
  108. case 0:
  109. PORTF &= ~(1<<PF7);
  110. break;
  111. case 1:
  112. PORTE &= ~(1<<PE2);
  113. break;
  114. case 2:
  115. PORTC &= ~(1<<PC6);
  116. break;
  117. case 3:
  118. PORTC &= ~(1<<PC7);
  119. break;
  120. }
  121. */
  122. }
  123. void softpwm_led_off(uint8_t index)
  124. {
  125. if (index) {
  126. PORTF |= ((1<<PF7) >> index);
  127. }
  128. else {
  129. PORTF &= ~(1<<PF7);
  130. }
  131. /*
  132. switch (index) {
  133. case 0:
  134. PORTF |= (1<<PF7);
  135. break;
  136. case 1:
  137. PORTE |= (1<<PE2);
  138. break;
  139. case 2:
  140. PORTC |= (1<<PC6);
  141. break;
  142. case 3:
  143. PORTC |= (1<<PC7);
  144. break;
  145. }
  146. */
  147. }
  148. #endif
  149. #endif
  150. #ifdef FADING_LED_ENABLE
  151. void action_keyevent(keyevent_t event)
  152. {
  153. if (backlight_config.enable) {
  154. if (backlight_config.level == 7) {
  155. if (event.pressed) {
  156. fading_led_set_delay(BACKLIGHT, 64);
  157. softpwm_led_decrease(BACKLIGHT, 32);
  158. }
  159. }
  160. if (backlight_config.level == 8) {
  161. if (event.pressed) {
  162. fading_led_set_delay(BACKLIGHT, 64);
  163. softpwm_led_increase(BACKLIGHT, 32);
  164. }
  165. }
  166. }
  167. }
  168. #endif
  169. #ifdef CUSTOM_LED_ENABLE
  170. void softpwm_led_custom(void)
  171. {
  172. rgb_fading();
  173. }
  174. #ifdef FADING_LED_ENABLE
  175. void fading_led_custom(uint8_t *value)
  176. {
  177. rgb_set_brightness(value[BACKLIGHT]);
  178. }
  179. #endif
  180. void breathing_led_custom(uint8_t *value)
  181. {
  182. rgb_set_brightness(value[BACKLIGHT]);
  183. }
  184. #endif
  185. #endif