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 6.5KB

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