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

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