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

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