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

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