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

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