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

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