您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Copyright 2015 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/pgmspace.h>
  15. #include <avr/eeprom.h>
  16. #include "softpwm_led.h"
  17. #include "backlight.h"
  18. #include "rgb.h"
  19. #include "light_ws2812.h"
  20. volatile static uint8_t rgb_fading_enable = 0;
  21. static rgb_config_t rgb_config;
  22. static uint16_t rgb_hue = 0;
  23. static uint8_t rgb_saturation = 255;
  24. static uint8_t rgb_brightness = 16;
  25. extern backlight_config_t backlight_config;
  26. extern uint8_t backlight_brightness;
  27. static void rgb_write_config(void);
  28. static void rgb_read_config(void);
  29. static void rgb_set_level(uint8_t level);
  30. static void hue_to_rgb(uint16_t hue, struct cRGB *rgb);
  31. static void hsb_to_rgb(uint16_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *rgb);
  32. void rgb_init(void)
  33. {
  34. rgb_read_config();
  35. if (rgb_config.raw == RGB_UNCONFIGURED) {
  36. rgb_config.enable = 0;
  37. rgb_config.level = RGB_OFF;
  38. rgb_write_config();
  39. }
  40. if (rgb_config.enable) {
  41. rgb_set_level(rgb_config.level);
  42. }
  43. }
  44. void rgb_read_config(void)
  45. {
  46. rgb_config.raw = eeprom_read_byte(EECONFIG_RGB);
  47. }
  48. void rgb_write_config(void)
  49. {
  50. eeprom_write_byte(EECONFIG_RGB, rgb_config.raw);
  51. }
  52. void rgb_toggle(void)
  53. {
  54. if (rgb_config.enable) {
  55. rgb_off();
  56. }
  57. else {
  58. rgb_on();
  59. }
  60. }
  61. void rgb_on(void)
  62. {
  63. rgb_config.enable = 1;
  64. rgb_set_level(rgb_config.level);
  65. rgb_write_config();
  66. }
  67. void rgb_off(void)
  68. {
  69. rgb_config.enable = 0;
  70. rgb_set_level(RGB_OFF);
  71. rgb_write_config();
  72. }
  73. void rgb_decrease(void)
  74. {
  75. if(rgb_config.level > 0) {
  76. rgb_config.level--;
  77. rgb_config.enable = (rgb_config.level != 0);
  78. rgb_write_config();
  79. }
  80. rgb_set_level(rgb_config.level);
  81. }
  82. void rgb_increase(void)
  83. {
  84. if(rgb_config.level < RGB_LEVELS) {
  85. rgb_config.level++;
  86. rgb_config.enable = 1;
  87. rgb_write_config();
  88. }
  89. rgb_set_level(rgb_config.level);
  90. }
  91. void rgb_step(void)
  92. {
  93. rgb_config.level++;
  94. if(rgb_config.level > RGB_LEVELS)
  95. {
  96. rgb_config.level = 0;
  97. }
  98. rgb_config.enable = (rgb_config.level != 0);
  99. rgb_set_level(rgb_config.level);
  100. }
  101. void rgb_set_level(uint8_t level)
  102. {
  103. if (level <= RGB_WHITE) {
  104. rgb_fading_enable = 0;
  105. if (level == RGB_OFF) {
  106. rgb_brightness = 0;
  107. }
  108. else {
  109. if (level == RGB_WHITE) {
  110. rgb_saturation = 0;
  111. }
  112. else {
  113. rgb_hue = (level - 1) * 128;
  114. rgb_saturation = 255;
  115. }
  116. if (backlight_config.enable) {
  117. if (backlight_config.level >= 1 && backlight_config.level <= 3) {
  118. rgb_brightness = backlight_brightness;
  119. }
  120. }
  121. else {
  122. rgb_brightness = 16;
  123. }
  124. }
  125. rgb_refresh();
  126. }
  127. else {
  128. rgb_saturation = 255;
  129. rgb_fading_enable = 3 - (level - RGB_FADE_SLOW);
  130. }
  131. }
  132. void rgb_set_brightness(uint8_t brightness)
  133. {
  134. rgb_brightness = brightness;
  135. rgb_refresh();
  136. }
  137. void rgb_refresh(void)
  138. {
  139. struct cRGB rgb_color[1];
  140. hsb_to_rgb(rgb_hue, rgb_saturation, rgb_brightness, rgb_color);
  141. ws2812_setleds(rgb_color, 1);
  142. }
  143. void hue_to_rgb(uint16_t hue, struct cRGB *rgb)
  144. {
  145. uint8_t hi = hue / 60;
  146. uint16_t f = (hue % 60) * 425 / 100;
  147. uint8_t q = 255 - f;
  148. switch (hi) {
  149. case 0: rgb->r = 255; rgb->g = f; rgb->b = 0; break;
  150. case 1: rgb->r = q; rgb->g = 255; rgb->b = 0; break;
  151. case 2: rgb->r = 0; rgb->g = 255; rgb->b = f; break;
  152. case 3: rgb->r = 0; rgb->g = q; rgb->b = 255; break;
  153. case 4: rgb->r = f; rgb->g = 0; rgb->b = 255; break;
  154. case 5: rgb->r = 255; rgb->g = 0; rgb->b = q; break;
  155. }
  156. }
  157. /*
  158. * original code: https://blog.adafruit.com/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/
  159. */
  160. void hsb_to_rgb(uint16_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *rgb)
  161. {
  162. uint8_t temp[5];
  163. uint8_t n = (hue >> 8) % 3;
  164. uint8_t x = ((((hue & 255) * saturation) >> 8) * brightness) >> 8;
  165. uint8_t s = ((256 - saturation) * brightness) >> 8;
  166. temp[0] = temp[3] = s;
  167. temp[1] = temp[4] = x + s;
  168. temp[2] = brightness - x;
  169. rgb->r = temp[n + 2];
  170. rgb->g = temp[n + 1];
  171. rgb->b = temp[n];
  172. }
  173. #ifdef CUSTOM_LED_ENABLE
  174. void softpwm_led_custom(void)
  175. {
  176. static uint8_t step = 0;
  177. static uint16_t hue = 0;
  178. if (rgb_fading_enable) {
  179. if (++step > rgb_fading_enable) {
  180. step = 0;
  181. rgb_hue = hue;
  182. rgb_refresh();
  183. if (++hue >= 768) {
  184. hue = 0;
  185. }
  186. }
  187. }
  188. }
  189. #endif