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

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