aboutsummaryrefslogtreecommitdiff
path: root/quantum/rgblight.c
diff options
context:
space:
mode:
authorJordi Orlando <0xdec@users.noreply.github.com>2016-07-26 14:43:45 -0500
committerGitHub <noreply@github.com>2016-07-26 14:43:45 -0500
commit3a860c4bc210857f03ef9fae5043d6d5736d140d (patch)
treee6b2be213dcffca0b1a3651d06f1ec50e93ea9d3 /quantum/rgblight.c
parentf9aadd17e8fce17fe1e135cf8022e71a35ae5adf (diff)
downloadqmk_firmware-3a860c4bc210857f03ef9fae5043d6d5736d140d.tar.gz
qmk_firmware-3a860c4bc210857f03ef9fae5043d6d5736d140d.zip
Clean up rgblight.c
Fix formatting issues, clarify comments
Diffstat (limited to 'quantum/rgblight.c')
-rw-r--r--quantum/rgblight.c731
1 files changed, 377 insertions, 354 deletions
diff --git a/quantum/rgblight.c b/quantum/rgblight.c
index b1b0f035d..1b3c576d1 100644
--- a/quantum/rgblight.c
+++ b/quantum/rgblight.c
@@ -7,24 +7,41 @@
7#include "debug.h" 7#include "debug.h"
8 8
9const uint8_t DIM_CURVE[] PROGMEM = { 9const uint8_t DIM_CURVE[] PROGMEM = {
10 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 10 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
11 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 11 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
12 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 12 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
13 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 13 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
14 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 14 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11,
15 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
16 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 16 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20,
17 20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26, 17 20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
18 27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35, 18 27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35,
19 36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47, 19 36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47,
20 48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 20 48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
21 63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82, 21 63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82,
22 83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109, 22 83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109,
23 110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144, 23 110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
24 146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190, 24 146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
25 193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255, 25 193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255
26};
27const uint8_t RGBLED_BREATHING_TABLE[] PROGMEM = {
28 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9,
29 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35,
30 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
31 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124,
32 127, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173,
33 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, 213, 215,
34 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240, 241, 243, 244,
35 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
36 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, 250, 250, 249, 248, 246,
37 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, 230, 228, 226, 224, 222, 220,
38 218, 215, 213, 211, 208, 206, 203, 201, 198, 196, 193, 190, 188, 185, 182, 179,
39 176, 173, 170, 167, 165, 162, 158, 155, 152, 149, 146, 143, 140, 137, 134, 131,
40 128, 124, 121, 118, 115, 112, 109, 106, 103, 100, 97, 93, 90, 88, 85, 82,
41 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40,
42 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11,
43 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0
26}; 44};
27const uint8_t RGBLED_BREATHING_TABLE[] PROGMEM = {0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,17,18,20,21,23,25,27,29,31,33,35,37,40,42,44,47,49,52,54,57,59,62,65,67,70,73,76,79,82,85,88,90,93,97,100,103,106,109,112,115,118,121,124,127,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173,176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215,218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244,245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255,255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246,245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220,218,215,213,211,208,206,203,201,198,196,193,190,188,185,182,179,176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131,128,124,121,118,115,112,109,106,103,100,97,93,90,88,85,82,79,76,73,70,67,65,62,59,57,54,52,49,47,44,42,40,37,35,33,31,29,27,25,23,21,20,18,17,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0};
28const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5}; 45const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
29const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30}; 46const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
30const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20}; 47const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
@@ -38,62 +55,56 @@ uint8_t rgblight_inited = 0;
38 55
39 56
40void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) { 57void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
41 /* convert hue, saturation and brightness ( HSB/HSV ) to RGB 58 /* Convert hue, saturation and brightness ( HSB/HSV ) to RGB. The DIM_CURVE is
42 The DIM_CURVE is used only on brightness/value and on saturation (inverted). 59 used only on brightness/value and on saturation (inverted). This looks the
43 This looks the most natural. 60 most natural. */
44 */
45 uint8_t r = 0, g = 0, b = 0; 61 uint8_t r = 0, g = 0, b = 0;
46 62
47 val = pgm_read_byte(&DIM_CURVE[val]); 63 val = pgm_read_byte(&DIM_CURVE[val]);
48 sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]); 64 sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]);
49 65
50 uint8_t base; 66 uint8_t base;
51 67
52 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind. 68 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
53 r = val; 69 r = val;
54 g = val; 70 g = val;
55 b = val; 71 b = val;
56 } else { 72 } else {
57 base = ((255 - sat) * val) >> 8; 73 base = ((255 - sat) * val) >> 8;
58 74
59 switch (hue / 60) { 75 switch (hue / 60) {
60 case 0: 76 case 0:
61 r = val; 77 r = val;
62 g = (((val - base)*hue) / 60) + base; 78 g = (((val - base) * hue) / 60) + base;
63 b = base; 79 b = base;
64 break; 80 break;
65 81 case 1:
66 case 1: 82 r = (((val - base) * (60 - (hue % 60))) / 60) + base;
67 r = (((val - base)*(60 - (hue % 60))) / 60) + base; 83 g = val;
68 g = val; 84 b = base;
69 b = base; 85 break;
70 break; 86 case 2:
71 87 r = base;
72 case 2: 88 g = val;
73 r = base; 89 b = (((val - base) * (hue % 60)) / 60) + base;
74 g = val; 90 break;
75 b = (((val - base)*(hue % 60)) / 60) + base; 91 case 3:
76 break; 92 r = base;
77 93 g = (((val - base) * (60 - (hue % 60))) / 60) + base;
78 case 3: 94 b = val;
79 r = base; 95 break;
80 g = (((val - base)*(60 - (hue % 60))) / 60) + base; 96 case 4:
81 b = val; 97 r = (((val - base) * (hue % 60)) / 60) + base;
82 break; 98 g = base;
83 99 b = val;
84 case 4: 100 break;
85 r = (((val - base)*(hue % 60)) / 60) + base; 101 case 5:
86 g = base; 102 r = val;
87 b = val; 103 g = base;
88 break; 104 b = (((val - base) * (60 - (hue % 60))) / 60) + base;
89 105 break;
90 case 5: 106 }
91 r = val; 107 }
92 g = base;
93 b = (((val - base)*(60 - (hue % 60))) / 60) + base;
94 break;
95 }
96 }
97 setrgb(r,g,b, led1); 108 setrgb(r,g,b, led1);
98} 109}
99 110
@@ -111,44 +122,44 @@ void eeconfig_update_rgblight(uint32_t val) {
111 eeprom_update_dword(EECONFIG_RGBLIGHT, val); 122 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
112} 123}
113void eeconfig_update_rgblight_default(void) { 124void eeconfig_update_rgblight_default(void) {
114 dprintf("eeconfig_update_rgblight_default\n"); 125 dprintf("eeconfig_update_rgblight_default\n");
115 rgblight_config.enable = 1; 126 rgblight_config.enable = 1;
116 rgblight_config.mode = 1; 127 rgblight_config.mode = 1;
117 rgblight_config.hue = 200; 128 rgblight_config.hue = 200;
118 rgblight_config.sat = 204; 129 rgblight_config.sat = 204;
119 rgblight_config.val = 204; 130 rgblight_config.val = 204;
120 eeconfig_update_rgblight(rgblight_config.raw); 131 eeconfig_update_rgblight(rgblight_config.raw);
121} 132}
122void eeconfig_debug_rgblight(void) { 133void eeconfig_debug_rgblight(void) {
123 dprintf("rgblight_config eprom\n"); 134 dprintf("rgblight_config eprom\n");
124 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable); 135 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
125 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode); 136 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
126 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue); 137 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
127 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat); 138 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
128 dprintf("rgblight_config.val = %d\n", rgblight_config.val); 139 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
129} 140}
130 141
131void rgblight_init(void) { 142void rgblight_init(void) {
132 debug_enable = 1; // Debug ON! 143 debug_enable = 1; // Debug ON!
133 dprintf("rgblight_init called.\n"); 144 dprintf("rgblight_init called.\n");
134 rgblight_inited = 1; 145 rgblight_inited = 1;
135 dprintf("rgblight_init start!\n"); 146 dprintf("rgblight_init start!\n");
136 if (!eeconfig_is_enabled()) { 147 if (!eeconfig_is_enabled()) {
137 dprintf("rgblight_init eeconfig is not enabled.\n"); 148 dprintf("rgblight_init eeconfig is not enabled.\n");
138 eeconfig_init(); 149 eeconfig_init();
139 eeconfig_update_rgblight_default(); 150 eeconfig_update_rgblight_default();
140 } 151 }
141 rgblight_config.raw = eeconfig_read_rgblight(); 152 rgblight_config.raw = eeconfig_read_rgblight();
142 if (!rgblight_config.mode) { 153 if (!rgblight_config.mode) {
143 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n"); 154 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
144 eeconfig_update_rgblight_default(); 155 eeconfig_update_rgblight_default();
145 rgblight_config.raw = eeconfig_read_rgblight(); 156 rgblight_config.raw = eeconfig_read_rgblight();
146 } 157 }
147 eeconfig_debug_rgblight(); // display current eeprom values 158 eeconfig_debug_rgblight(); // display current eeprom values
148 159
149 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 160 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
150 rgblight_timer_init(); // setup the timer 161 rgblight_timer_init(); // setup the timer
151 #endif 162 #endif
152 163
153 if (rgblight_config.enable) { 164 if (rgblight_config.enable) {
154 rgblight_mode(rgblight_config.mode); 165 rgblight_mode(rgblight_config.mode);
@@ -156,58 +167,57 @@ void rgblight_init(void) {
156} 167}
157 168
158void rgblight_increase(void) { 169void rgblight_increase(void) {
159 uint8_t mode = 0; 170 uint8_t mode = 0;
160 if (rgblight_config.mode < RGBLIGHT_MODES) { 171 if (rgblight_config.mode < RGBLIGHT_MODES) {
161 mode = rgblight_config.mode + 1; 172 mode = rgblight_config.mode + 1;
162 } 173 }
163 rgblight_mode(mode); 174 rgblight_mode(mode);
164} 175}
165
166void rgblight_decrease(void) { 176void rgblight_decrease(void) {
167 uint8_t mode = 0; 177 uint8_t mode = 0;
168 if (rgblight_config.mode > 1) { //mode will never < 1, if mode is less than 1, eeprom need to be initialized. 178 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
169 mode = rgblight_config.mode-1; 179 if (rgblight_config.mode > 1) {
180 mode = rgblight_config.mode - 1;
170 } 181 }
171 rgblight_mode(mode); 182 rgblight_mode(mode);
172} 183}
173
174void rgblight_step(void) { 184void rgblight_step(void) {
175 uint8_t mode = 0; 185 uint8_t mode = 0;
176 mode = rgblight_config.mode + 1; 186 mode = rgblight_config.mode + 1;
177 if (mode > RGBLIGHT_MODES) { 187 if (mode > RGBLIGHT_MODES) {
178 mode = 1; 188 mode = 1;
179 } 189 }
180 rgblight_mode(mode); 190 rgblight_mode(mode);
181} 191}
182 192
183void rgblight_mode(uint8_t mode) { 193void rgblight_mode(uint8_t mode) {
184 if (!rgblight_config.enable) { 194 if (!rgblight_config.enable) {
185 return; 195 return;
186 } 196 }
187 if (mode<1) { 197 if (mode < 1) {
188 rgblight_config.mode = 1; 198 rgblight_config.mode = 1;
189 } else if (mode > RGBLIGHT_MODES) { 199 } else if (mode > RGBLIGHT_MODES) {
190 rgblight_config.mode = RGBLIGHT_MODES; 200 rgblight_config.mode = RGBLIGHT_MODES;
191 } else { 201 } else {
192 rgblight_config.mode = mode; 202 rgblight_config.mode = mode;
193 } 203 }
194 eeconfig_update_rgblight(rgblight_config.raw); 204 eeconfig_update_rgblight(rgblight_config.raw);
195 xprintf("rgblight mode: %u\n", rgblight_config.mode); 205 xprintf("rgblight mode: %u\n", rgblight_config.mode);
196 if (rgblight_config.mode == 1) { 206 if (rgblight_config.mode == 1) {
197 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 207 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
198 rgblight_timer_disable(); 208 rgblight_timer_disable();
199 #endif 209 #endif
200 } else if (rgblight_config.mode >=2 && rgblight_config.mode <=23) { 210 } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 23) {
201 // MODE 2-5, breathing 211 // MODE 2-5, breathing
202 // MODE 6-8, rainbow mood 212 // MODE 6-8, rainbow mood
203 // MODE 9-14, rainbow swirl 213 // MODE 9-14, rainbow swirl
204 // MODE 15-20, snake 214 // MODE 15-20, snake
205 // MODE 21-23, knight 215 // MODE 21-23, knight
206 216
207 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 217 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
208 rgblight_timer_enable(); 218 rgblight_timer_enable();
209 #endif 219 #endif
210 } 220 }
211 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); 221 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
212} 222}
213 223
@@ -215,127 +225,125 @@ void rgblight_toggle(void) {
215 rgblight_config.enable ^= 1; 225 rgblight_config.enable ^= 1;
216 eeconfig_update_rgblight(rgblight_config.raw); 226 eeconfig_update_rgblight(rgblight_config.raw);
217 xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable); 227 xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable);
218 if (rgblight_config.enable) { 228 if (rgblight_config.enable) {
219 rgblight_mode(rgblight_config.mode); 229 rgblight_mode(rgblight_config.mode);
220 } else { 230 } else {
221 231 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
222 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 232 rgblight_timer_disable();
223 rgblight_timer_disable(); 233 #endif
224 #endif 234 _delay_ms(50);
225 _delay_ms(50); 235 rgblight_set();
226 rgblight_set(); 236 }
227 }
228} 237}
229 238
230 239
231void rgblight_increase_hue(void){ 240void rgblight_increase_hue(void) {
232 uint16_t hue; 241 uint16_t hue;
233 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360; 242 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
234 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val); 243 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
235} 244}
236void rgblight_decrease_hue(void){ 245void rgblight_decrease_hue(void) {
237 uint16_t hue; 246 uint16_t hue;
238 if (rgblight_config.hue-RGBLIGHT_HUE_STEP <0 ) { 247 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
239 hue = (rgblight_config.hue+360-RGBLIGHT_HUE_STEP) % 360; 248 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
240 } else { 249 } else {
241 hue = (rgblight_config.hue-RGBLIGHT_HUE_STEP) % 360; 250 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
242 } 251 }
243 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val); 252 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
244} 253}
245void rgblight_increase_sat(void) { 254void rgblight_increase_sat(void) {
246 uint8_t sat; 255 uint8_t sat;
247 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) { 256 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
248 sat = 255; 257 sat = 255;
249 } else { 258 } else {
250 sat = rgblight_config.sat+RGBLIGHT_SAT_STEP; 259 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
251 } 260 }
252 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val); 261 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
253} 262}
254void rgblight_decrease_sat(void){ 263void rgblight_decrease_sat(void) {
255 uint8_t sat; 264 uint8_t sat;
256 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) { 265 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
257 sat = 0; 266 sat = 0;
258 } else { 267 } else {
259 sat = rgblight_config.sat-RGBLIGHT_SAT_STEP; 268 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
260 } 269 }
261 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val); 270 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
262} 271}
263void rgblight_increase_val(void){ 272void rgblight_increase_val(void) {
264 uint8_t val; 273 uint8_t val;
265 if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) { 274 if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
266 val = 255; 275 val = 255;
267 } else { 276 } else {
268 val = rgblight_config.val+RGBLIGHT_VAL_STEP; 277 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
269 } 278 }
270 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val); 279 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
271} 280}
272void rgblight_decrease_val(void) { 281void rgblight_decrease_val(void) {
273 uint8_t val; 282 uint8_t val;
274 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) { 283 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
275 val = 0; 284 val = 0;
276 } else { 285 } else {
277 val = rgblight_config.val-RGBLIGHT_VAL_STEP; 286 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
278 } 287 }
279 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val); 288 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
280} 289}
281 290
282void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val){ 291void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
283 inmem_config.raw = rgblight_config.raw; 292 inmem_config.raw = rgblight_config.raw;
284 if (rgblight_config.enable) { 293 if (rgblight_config.enable) {
285 struct cRGB tmp_led; 294 struct cRGB tmp_led;
286 sethsv(hue, sat, val, &tmp_led); 295 sethsv(hue, sat, val, &tmp_led);
287 inmem_config.hue = hue; 296 inmem_config.hue = hue;
288 inmem_config.sat = sat; 297 inmem_config.sat = sat;
289 inmem_config.val = val; 298 inmem_config.val = val;
290 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val); 299 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
291 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b); 300 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
292 } 301 }
293} 302}
294void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val){ 303void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
295 if (rgblight_config.enable) { 304 if (rgblight_config.enable) {
296 if (rgblight_config.mode == 1) { 305 if (rgblight_config.mode == 1) {
297 // same static color 306 // same static color
298 rgblight_sethsv_noeeprom(hue, sat, val); 307 rgblight_sethsv_noeeprom(hue, sat, val);
299 } else { 308 } else {
300 // all LEDs in same color 309 // all LEDs in same color
301 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { 310 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
302 // breathing mode, ignore the change of val, use in memory value instead 311 // breathing mode, ignore the change of val, use in memory value instead
303 val = rgblight_config.val; 312 val = rgblight_config.val;
304 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) { 313 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
305 // rainbow mood and rainbow swirl, ignore the change of hue 314 // rainbow mood and rainbow swirl, ignore the change of hue
306 hue = rgblight_config.hue; 315 hue = rgblight_config.hue;
307 } 316 }
308 } 317 }
309 rgblight_config.hue = hue; 318 rgblight_config.hue = hue;
310 rgblight_config.sat = sat; 319 rgblight_config.sat = sat;
311 rgblight_config.val = val; 320 rgblight_config.val = val;
312 eeconfig_update_rgblight(rgblight_config.raw); 321 eeconfig_update_rgblight(rgblight_config.raw);
313 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); 322 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
314 } 323 }
315} 324}
316 325
317void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b){ 326void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
318 // dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b); 327 // dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b);
319 for (uint8_t i=0;i<RGBLED_NUM;i++) { 328 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
320 led[i].r = r; 329 led[i].r = r;
321 led[i].g = g; 330 led[i].g = g;
322 led[i].b = b; 331 led[i].b = b;
323 } 332 }
324 rgblight_set(); 333 rgblight_set();
325
326} 334}
327 335
328void rgblight_set(void) { 336void rgblight_set(void) {
329 if (rgblight_config.enable) { 337 if (rgblight_config.enable) {
330 ws2812_setleds(led, RGBLED_NUM); 338 ws2812_setleds(led, RGBLED_NUM);
331 } else { 339 } else {
332 for (uint8_t i=0;i<RGBLED_NUM;i++) { 340 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
333 led[i].r = 0; 341 led[i].r = 0;
334 led[i].g = 0; 342 led[i].g = 0;
335 led[i].b = 0; 343 led[i].b = 0;
336 } 344 }
337 ws2812_setleds(led, RGBLED_NUM); 345 ws2812_setleds(led, RGBLED_NUM);
338 } 346 }
339} 347}
340 348
341 349
@@ -343,178 +351,193 @@ void rgblight_set(void) {
343 351
344// Animation timer -- AVR Timer3 352// Animation timer -- AVR Timer3
345void rgblight_timer_init(void) { 353void rgblight_timer_init(void) {
346 static uint8_t rgblight_timer_is_init = 0; 354 static uint8_t rgblight_timer_is_init = 0;
347 if (rgblight_timer_is_init) { 355 if (rgblight_timer_is_init) {
348 return; 356 return;
349 } 357 }
350 rgblight_timer_is_init = 1; 358 rgblight_timer_is_init = 1;
351 /* Timer 3 setup */ 359 /* Timer 3 setup */
352 TCCR3B = _BV(WGM32) //CTC mode OCR3A as TOP 360 TCCR3B = _BV(WGM32) //CTC mode OCR3A as TOP
353 | _BV(CS30); //Clock selelct: clk/1 361 | _BV(CS30); //Clock selelct: clk/1
354 /* Set TOP value */ 362 /* Set TOP value */
355 uint8_t sreg = SREG; 363 uint8_t sreg = SREG;
356 cli(); 364 cli();
357 OCR3AH = (RGBLED_TIMER_TOP>>8)&0xff; 365 OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
358 OCR3AL = RGBLED_TIMER_TOP&0xff; 366 OCR3AL = RGBLED_TIMER_TOP & 0xff;
359 SREG = sreg; 367 SREG = sreg;
360} 368}
361void rgblight_timer_enable(void) { 369void rgblight_timer_enable(void) {
362 TIMSK3 |= _BV(OCIE3A); 370 TIMSK3 |= _BV(OCIE3A);
363 dprintf("TIMER3 enabled.\n"); 371 dprintf("TIMER3 enabled.\n");
364} 372}
365void rgblight_timer_disable(void) { 373void rgblight_timer_disable(void) {
366 TIMSK3 &= ~_BV(OCIE3A); 374 TIMSK3 &= ~_BV(OCIE3A);
367 dprintf("TIMER3 disabled.\n"); 375 dprintf("TIMER3 disabled.\n");
368} 376}
369void rgblight_timer_toggle(void) { 377void rgblight_timer_toggle(void) {
370 TIMSK3 ^= _BV(OCIE3A); 378 TIMSK3 ^= _BV(OCIE3A);
371 dprintf("TIMER3 toggled.\n"); 379 dprintf("TIMER3 toggled.\n");
372} 380}
373 381
374ISR(TIMER3_COMPA_vect) { 382ISR(TIMER3_COMPA_vect) {
375 // Mode = 1, static light, do nothing here 383 // mode = 1, static light, do nothing here
376 if (rgblight_config.mode>=2 && rgblight_config.mode<=5) { 384 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
377 // mode = 2 to 5, breathing mode 385 // mode = 2 to 5, breathing mode
378 rgblight_effect_breathing(rgblight_config.mode-2); 386 rgblight_effect_breathing(rgblight_config.mode - 2);
379 387 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
380 } else if (rgblight_config.mode>=6 && rgblight_config.mode<=8) { 388 // mode = 6 to 8, rainbow mood mod
381 rgblight_effect_rainbow_mood(rgblight_config.mode-6); 389 rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
382 } else if (rgblight_config.mode>=9 && rgblight_config.mode<=14) { 390 } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
383 rgblight_effect_rainbow_swirl(rgblight_config.mode-9); 391 // mode = 9 to 14, rainbow swirl mode
384 } else if (rgblight_config.mode>=15 && rgblight_config.mode<=20) { 392 rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
385 rgblight_effect_snake(rgblight_config.mode-15); 393 } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
386 } else if (rgblight_config.mode>=21 && rgblight_config.mode<=23) { 394 // mode = 15 to 20, snake mode
387 rgblight_effect_knight(rgblight_config.mode-21); 395 rgblight_effect_snake(rgblight_config.mode - 15);
388 } 396 } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
389} 397 // mode = 21 to 23, knight mode
390 398 rgblight_effect_knight(rgblight_config.mode - 21);
391// effects 399 }
400}
401
402// Effects
392void rgblight_effect_breathing(uint8_t interval) { 403void rgblight_effect_breathing(uint8_t interval) {
393 static uint8_t pos = 0; 404 static uint8_t pos = 0;
394 static uint16_t last_timer = 0; 405 static uint16_t last_timer = 0;
395 406
396 if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) return; 407 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
397 last_timer = timer_read(); 408 return;
409 }
410 last_timer = timer_read();
398 411
399 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos])); 412 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos]));
400 pos = (pos+1) % 256; 413 pos = (pos + 1) % 256;
401} 414}
402
403void rgblight_effect_rainbow_mood(uint8_t interval) { 415void rgblight_effect_rainbow_mood(uint8_t interval) {
404 static uint16_t current_hue=0; 416 static uint16_t current_hue = 0;
405 static uint16_t last_timer = 0; 417 static uint16_t last_timer = 0;
406 418
407 if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) return; 419 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
408 last_timer = timer_read(); 420 return;
409 rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val); 421 }
410 current_hue = (current_hue+1) % 360; 422 last_timer = timer_read();
423 rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
424 current_hue = (current_hue + 1) % 360;
411} 425}
412
413void rgblight_effect_rainbow_swirl(uint8_t interval) { 426void rgblight_effect_rainbow_swirl(uint8_t interval) {
414 static uint16_t current_hue=0; 427 static uint16_t current_hue = 0;
415 static uint16_t last_timer = 0; 428 static uint16_t last_timer = 0;
416 uint16_t hue; 429 uint16_t hue;
417 uint8_t i; 430 uint8_t i;
418 if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval/2])) return; 431 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval / 2])) {
419 last_timer = timer_read(); 432 return;
420 for (i=0; i<RGBLED_NUM; i++) { 433 }
421 hue = (360/RGBLED_NUM*i+current_hue)%360; 434 last_timer = timer_read();
422 sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]); 435 for (i = 0; i < RGBLED_NUM; i++) {
423 } 436 hue = (360 / RGBLED_NUM * i + current_hue) % 360;
424 rgblight_set(); 437 sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]);
425 438 }
426 if (interval % 2) { 439 rgblight_set();
427 current_hue = (current_hue+1) % 360; 440
428 } else { 441 if (interval % 2) {
429 if (current_hue -1 < 0) { 442 current_hue = (current_hue + 1) % 360;
430 current_hue = 359; 443 } else {
431 } else { 444 if (current_hue - 1 < 0) {
432 current_hue = current_hue - 1; 445 current_hue = 359;
433 } 446 } else {
434 447 current_hue = current_hue - 1;
435 } 448 }
449 }
436} 450}
437void rgblight_effect_snake(uint8_t interval) { 451void rgblight_effect_snake(uint8_t interval) {
438 static uint8_t pos=0; 452 static uint8_t pos = 0;
439 static uint16_t last_timer = 0; 453 static uint16_t last_timer = 0;
440 uint8_t i,j; 454 uint8_t i, j;
441 int8_t k; 455 int8_t k;
442 int8_t increament = 1; 456 int8_t increament = 1;
443 if (interval%2) increament = -1; 457 if (interval % 2) {
444 if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval/2])) return; 458 increament = -1;
445 last_timer = timer_read(); 459 }
446 for (i=0;i<RGBLED_NUM;i++) { 460 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
447 led[i].r=0; 461 return;
448 led[i].g=0; 462 }
449 led[i].b=0; 463 last_timer = timer_read();
450 for (j=0;j<RGBLIGHT_EFFECT_SNAKE_LENGTH;j++) { 464 for (i = 0; i < RGBLED_NUM; i++) {
451 k = pos+j*increament; 465 led[i].r = 0;
452 if (k<0) k = k+RGBLED_NUM; 466 led[i].g = 0;
453 if (i==k) { 467 led[i].b = 0;
454 sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), &led[i]); 468 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
455 } 469 k = pos + j * increament;
456 } 470 if (k < 0) {
457 } 471 k = k + RGBLED_NUM;
458 rgblight_set(); 472 }
459 if (increament == 1) { 473 if (i == k) {
460 if (pos - 1 < 0) { 474 sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), &led[i]);
461 pos = RGBLED_NUM-1; 475 }
462 } else { 476 }
463 pos -= 1; 477 }
464 } 478 rgblight_set();
465 } else { 479 if (increament == 1) {
466 pos = (pos+1)%RGBLED_NUM; 480 if (pos - 1 < 0) {
467 } 481 pos = RGBLED_NUM - 1;
468 482 } else {
483 pos -= 1;
484 }
485 } else {
486 pos = (pos + 1) % RGBLED_NUM;
487 }
469} 488}
470
471void rgblight_effect_knight(uint8_t interval) { 489void rgblight_effect_knight(uint8_t interval) {
472 static int8_t pos=0; 490 static int8_t pos = 0;
473 static uint16_t last_timer = 0; 491 static uint16_t last_timer = 0;
474 uint8_t i,j,cur; 492 uint8_t i, j, cur;
475 int8_t k; 493 int8_t k;
476 struct cRGB preled[RGBLED_NUM]; 494 struct cRGB preled[RGBLED_NUM];
477 static int8_t increament = -1; 495 static int8_t increament = -1;
478 if (timer_elapsed(last_timer)<pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) return; 496 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
479 last_timer = timer_read(); 497 return;
480 for (i=0;i<RGBLED_NUM;i++) { 498 }
481 preled[i].r=0; 499 last_timer = timer_read();
482 preled[i].g=0; 500 for (i = 0; i < RGBLED_NUM; i++) {
483 preled[i].b=0; 501 preled[i].r = 0;
484 for (j=0;j<RGBLIGHT_EFFECT_KNIGHT_LENGTH;j++) { 502 preled[i].g = 0;
485 k = pos+j*increament; 503 preled[i].b = 0;
486 if (k<0) k = 0; 504 for (j = 0; j < RGBLIGHT_EFFECT_KNIGHT_LENGTH; j++) {
487 if (k>=RGBLED_NUM) k=RGBLED_NUM-1; 505 k = pos + j * increament;
488 if (i==k) { 506 if (k < 0) {
489 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]); 507 k = 0;
490 } 508 }
491 } 509 if (k >= RGBLED_NUM) {
492 } 510 k = RGBLED_NUM - 1;
493 if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) { 511 }
494 for (i=0;i<RGBLED_NUM;i++) { 512 if (i == k) {
495 cur = (i+RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM; 513 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]);
496 led[i].r = preled[cur].r; 514 }
497 led[i].g = preled[cur].g; 515 }
498 led[i].b = preled[cur].b; 516 }
499 } 517 if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) {
500 } 518 for (i = 0; i < RGBLED_NUM; i++) {
501 rgblight_set(); 519 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
502 if (increament == 1) { 520 led[i].r = preled[cur].r;
503 if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) { 521 led[i].g = preled[cur].g;
504 pos = 0- RGBLIGHT_EFFECT_KNIGHT_LENGTH; 522 led[i].b = preled[cur].b;
505 increament = -1; 523 }
506 } else { 524 }
507 pos -= 1; 525 rgblight_set();
508 } 526 if (increament == 1) {
509 } else { 527 if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
510 if (pos+1>RGBLED_NUM+RGBLIGHT_EFFECT_KNIGHT_LENGTH) { 528 pos = 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH;
511 pos = RGBLED_NUM+RGBLIGHT_EFFECT_KNIGHT_LENGTH-1; 529 increament = -1;
512 increament = 1; 530 } else {
513 } else { 531 pos -= 1;
514 pos += 1; 532 }
515 } 533 } else {
516 } 534 if (pos + 1 > RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
517 535 pos = RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
518} 536 increament = 1;
519 537 } else {
520#endif \ No newline at end of file 538 pos += 1;
539 }
540 }
541}
542
543#endif