17
17
#include <drivers/led_strip.h>
18
18
#include <drivers/ext_power.h>
19
19
20
+ #include <zmk/rgb_underglow.h>
21
+
20
22
LOG_MODULE_DECLARE (zmk , CONFIG_ZMK_LOG_LEVEL );
21
23
22
24
#define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow))
@@ -30,16 +32,8 @@ enum rgb_underglow_effect {
30
32
UNDERGLOW_EFFECT_NUMBER // Used to track number of underglow effects
31
33
};
32
34
33
- struct led_hsb {
34
- uint16_t h ;
35
- uint8_t s ;
36
- uint8_t b ;
37
- };
38
-
39
35
struct rgb_underglow_state {
40
- uint16_t hue ;
41
- uint8_t saturation ;
42
- uint8_t brightness ;
36
+ struct zmk_led_hsb color ;
43
37
uint8_t animation_speed ;
44
38
uint8_t current_effect ;
45
39
uint16_t animation_step ;
@@ -56,7 +50,7 @@ static struct rgb_underglow_state state;
56
50
static const struct device * ext_power ;
57
51
#endif
58
52
59
- static struct led_rgb hsb_to_rgb (struct led_hsb hsb ) {
53
+ static struct led_rgb hsb_to_rgb (struct zmk_led_hsb hsb ) {
60
54
double r , g , b ;
61
55
62
56
uint8_t i = hsb .h / 60 ;
@@ -107,23 +101,14 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) {
107
101
108
102
static void zmk_rgb_underglow_effect_solid () {
109
103
for (int i = 0 ; i < STRIP_NUM_PIXELS ; i ++ ) {
110
- int hue = state .hue ;
111
- int sat = state .saturation ;
112
- int brt = state .brightness ;
113
-
114
- struct led_hsb hsb = {hue , sat , brt };
115
-
116
- pixels [i ] = hsb_to_rgb (hsb );
104
+ pixels [i ] = hsb_to_rgb (state .color );
117
105
}
118
106
}
119
107
120
108
static void zmk_rgb_underglow_effect_breathe () {
121
109
for (int i = 0 ; i < STRIP_NUM_PIXELS ; i ++ ) {
122
- int hue = state .hue ;
123
- int sat = state .saturation ;
124
- int brt = abs (state .animation_step - 1200 ) / 12 ;
125
-
126
- struct led_hsb hsb = {hue , sat , brt };
110
+ struct zmk_led_hsb hsb = state .color ;
111
+ hsb .b = abs (state .animation_step - 1200 ) / 12 ;
127
112
128
113
pixels [i ] = hsb_to_rgb (hsb );
129
114
}
@@ -137,11 +122,8 @@ static void zmk_rgb_underglow_effect_breathe() {
137
122
138
123
static void zmk_rgb_underglow_effect_spectrum () {
139
124
for (int i = 0 ; i < STRIP_NUM_PIXELS ; i ++ ) {
140
- int hue = state .animation_step ;
141
- int sat = state .saturation ;
142
- int brt = state .brightness ;
143
-
144
- struct led_hsb hsb = {hue , sat , brt };
125
+ struct zmk_led_hsb hsb = state .color ;
126
+ hsb .h = state .animation_speed ;
145
127
146
128
pixels [i ] = hsb_to_rgb (hsb );
147
129
}
@@ -152,11 +134,8 @@ static void zmk_rgb_underglow_effect_spectrum() {
152
134
153
135
static void zmk_rgb_underglow_effect_swirl () {
154
136
for (int i = 0 ; i < STRIP_NUM_PIXELS ; i ++ ) {
155
- int hue = (360 / STRIP_NUM_PIXELS * i + state .animation_step ) % 360 ;
156
- int sat = state .saturation ;
157
- int brt = state .brightness ;
158
-
159
- struct led_hsb hsb = {hue , sat , brt };
137
+ struct zmk_led_hsb hsb = state .color ;
138
+ hsb .h = (360 / STRIP_NUM_PIXELS * i + state .animation_step ) % 360 ;
160
139
161
140
pixels [i ] = hsb_to_rgb (hsb );
162
141
}
@@ -243,9 +222,11 @@ static int zmk_rgb_underglow_init(const struct device *_arg) {
243
222
#endif
244
223
245
224
state = (struct rgb_underglow_state ){
246
- hue : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START ,
247
- saturation : CONFIG_ZMK_RGB_UNDERGLOW_SAT_START ,
248
- brightness : CONFIG_ZMK_RGB_UNDERGLOW_BRT_START ,
225
+ color : {
226
+ h : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START ,
227
+ s : CONFIG_ZMK_RGB_UNDERGLOW_SAT_START ,
228
+ b : CONFIG_ZMK_RGB_UNDERGLOW_BRT_START ,
229
+ },
249
230
animation_speed : CONFIG_ZMK_RGB_UNDERGLOW_SPD_START ,
250
231
current_effect : CONFIG_ZMK_RGB_UNDERGLOW_EFF_START ,
251
232
animation_step : 0 ,
@@ -357,64 +338,85 @@ int zmk_rgb_underglow_toggle() {
357
338
return state .on ? zmk_rgb_underglow_off () : zmk_rgb_underglow_on ();
358
339
}
359
340
360
- int zmk_rgb_underglow_set_hsb (uint16_t hue , uint8_t saturation , uint8_t brightness ) {
361
- if (hue > 360 || saturation > 100 || brightness > 100 ) {
341
+ int zmk_rgb_underglow_set_hsb (struct zmk_led_hsb color ) {
342
+ if (color . h > 360 || color . s > 100 || color . b > 100 ) {
362
343
return - ENOTSUP ;
363
344
}
364
345
365
- state .hue = hue ;
366
- state .saturation = saturation ;
367
- state .brightness = brightness ;
346
+ state .color = color ;
368
347
369
348
return 0 ;
370
349
}
371
350
372
- int zmk_rgb_underglow_change_hue (int direction ) {
373
- if (!led_strip )
374
- return - ENODEV ;
351
+ struct zmk_led_hsb zmk_rgb_underglow_calc_hue (int direction ) {
352
+ struct zmk_led_hsb color = state .color ;
375
353
376
- if (state .hue == 0 && direction < 0 ) {
377
- state .hue = 360 - CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP ;
378
- return 0 ;
354
+ if (color .h == 0 && direction < 0 ) {
355
+ return color ;
379
356
}
380
357
381
- state . hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP ;
358
+ color . h += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP ;
382
359
383
- state . hue = state . hue % 360 ;
360
+ color . h = color . h % 360 ;
384
361
385
- return zmk_rgb_underglow_save_state () ;
362
+ return color ;
386
363
}
387
364
388
- int zmk_rgb_underglow_change_sat (int direction ) {
389
- if (!led_strip )
390
- return - ENODEV ;
365
+ struct zmk_led_hsb zmk_rgb_underglow_calc_sat (int direction ) {
366
+ struct zmk_led_hsb color = state .color ;
391
367
392
- if (state .saturation == 0 && direction < 0 ) {
393
- return 0 ;
368
+ if (color .s == 0 && direction < 0 ) {
369
+ return color ;
370
+ }
371
+
372
+ color .s += direction * CONFIG_ZMK_RGB_UNDERGLOW_SAT_STEP ;
373
+
374
+ if (color .s > 100 ) {
375
+ color .s = 100 ;
376
+ }
377
+
378
+ return color ;
379
+ }
380
+
381
+ struct zmk_led_hsb zmk_rgb_underglow_calc_brt (int direction ) {
382
+ struct zmk_led_hsb color = state .color ;
383
+
384
+ if (color .b == 0 && direction < 0 ) {
385
+ return color ;
394
386
}
395
387
396
- state . saturation += direction * CONFIG_ZMK_RGB_UNDERGLOW_SAT_STEP ;
388
+ color . b += direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP ;
397
389
398
- if (state . saturation > 100 ) {
399
- state . saturation = 100 ;
390
+ if (color . b > 100 ) {
391
+ color . b = 100 ;
400
392
}
401
393
394
+ return color ;
395
+ }
396
+
397
+ int zmk_rgb_underglow_change_hue (int direction ) {
398
+ if (!led_strip )
399
+ return - ENODEV ;
400
+
401
+ state .color = zmk_rgb_underglow_calc_hue (direction );
402
+
402
403
return zmk_rgb_underglow_save_state ();
403
404
}
404
405
405
- int zmk_rgb_underglow_change_brt (int direction ) {
406
+ int zmk_rgb_underglow_change_sat (int direction ) {
406
407
if (!led_strip )
407
408
return - ENODEV ;
408
409
409
- if (state .brightness == 0 && direction < 0 ) {
410
- return 0 ;
411
- }
410
+ state .color = zmk_rgb_underglow_calc_sat (direction );
412
411
413
- state .brightness += direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP ;
412
+ return zmk_rgb_underglow_save_state ();
413
+ }
414
414
415
- if (state .brightness > 100 ) {
416
- state .brightness = 100 ;
417
- }
415
+ int zmk_rgb_underglow_change_brt (int direction ) {
416
+ if (!led_strip )
417
+ return - ENODEV ;
418
+
419
+ state .color = zmk_rgb_underglow_calc_brt (direction );
418
420
419
421
return zmk_rgb_underglow_save_state ();
420
422
}
0 commit comments