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.

rgb.c 5.8KB

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