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))
23
25
#define STRIP_NUM_PIXELS DT_PROP(DT_CHOSEN(zmk_underglow), chain_length)
24
26
27
+ #define HUE_MAX 360
28
+ #define SAT_MAX 100
29
+ #define BRT_MAX 100
30
+
25
31
enum rgb_underglow_effect {
26
32
UNDERGLOW_EFFECT_SOLID ,
27
33
UNDERGLOW_EFFECT_BREATHE ,
@@ -30,16 +36,8 @@ enum rgb_underglow_effect {
30
36
UNDERGLOW_EFFECT_NUMBER // Used to track number of underglow effects
31
37
};
32
38
33
- struct led_hsb {
34
- uint16_t h ;
35
- uint8_t s ;
36
- uint8_t b ;
37
- };
38
-
39
39
struct rgb_underglow_state {
40
- uint16_t hue ;
41
- uint8_t saturation ;
42
- uint8_t brightness ;
40
+ struct zmk_led_hsb color ;
43
41
uint8_t animation_speed ;
44
42
uint8_t current_effect ;
45
43
uint16_t animation_step ;
@@ -56,13 +54,13 @@ static struct rgb_underglow_state state;
56
54
static const struct device * ext_power ;
57
55
#endif
58
56
59
- static struct led_rgb hsb_to_rgb (struct led_hsb hsb ) {
57
+ static struct led_rgb hsb_to_rgb (struct zmk_led_hsb hsb ) {
60
58
double r , g , b ;
61
59
62
60
uint8_t i = hsb .h / 60 ;
63
- double v = hsb .b / 100.0 ;
64
- double s = hsb .s / 100.0 ;
65
- double f = hsb .h / 360.0 * 6 - i ;
61
+ double v = hsb .b / (( float ) BRT_MAX ) ;
62
+ double s = hsb .s / (( float ) SAT_MAX ) ;
63
+ double f = hsb .h / (( float ) HUE_MAX ) * 6 - i ;
66
64
double p = v * (1 - s );
67
65
double q = v * (1 - f * s );
68
66
double t = v * (1 - (1 - f ) * s );
@@ -107,23 +105,14 @@ static struct led_rgb hsb_to_rgb(struct led_hsb hsb) {
107
105
108
106
static void zmk_rgb_underglow_effect_solid () {
109
107
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 );
108
+ pixels [i ] = hsb_to_rgb (state .color );
117
109
}
118
110
}
119
111
120
112
static void zmk_rgb_underglow_effect_breathe () {
121
113
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 };
114
+ struct zmk_led_hsb hsb = state .color ;
115
+ hsb .b = abs (state .animation_step - 1200 ) / 12 ;
127
116
128
117
pixels [i ] = hsb_to_rgb (hsb );
129
118
}
@@ -137,32 +126,26 @@ static void zmk_rgb_underglow_effect_breathe() {
137
126
138
127
static void zmk_rgb_underglow_effect_spectrum () {
139
128
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 };
129
+ struct zmk_led_hsb hsb = state .color ;
130
+ hsb .h = state .animation_speed ;
145
131
146
132
pixels [i ] = hsb_to_rgb (hsb );
147
133
}
148
134
149
135
state .animation_step += state .animation_speed ;
150
- state .animation_step = state .animation_step % 360 ;
136
+ state .animation_step = state .animation_step % HUE_MAX ;
151
137
}
152
138
153
139
static void zmk_rgb_underglow_effect_swirl () {
154
140
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 };
141
+ struct zmk_led_hsb hsb = state .color ;
142
+ hsb .h = (HUE_MAX / STRIP_NUM_PIXELS * i + state .animation_step ) % HUE_MAX ;
160
143
161
144
pixels [i ] = hsb_to_rgb (hsb );
162
145
}
163
146
164
147
state .animation_step += state .animation_speed * 2 ;
165
- state .animation_step = state .animation_step % 360 ;
148
+ state .animation_step = state .animation_step % HUE_MAX ;
166
149
}
167
150
168
151
static void zmk_rgb_underglow_tick (struct k_work * work ) {
@@ -243,9 +226,11 @@ static int zmk_rgb_underglow_init(const struct device *_arg) {
243
226
#endif
244
227
245
228
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 ,
229
+ color : {
230
+ h : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START ,
231
+ s : CONFIG_ZMK_RGB_UNDERGLOW_SAT_START ,
232
+ b : CONFIG_ZMK_RGB_UNDERGLOW_BRT_START ,
233
+ },
249
234
animation_speed : CONFIG_ZMK_RGB_UNDERGLOW_SPD_START ,
250
235
current_effect : CONFIG_ZMK_RGB_UNDERGLOW_EFF_START ,
251
236
animation_step : 0 ,
@@ -337,16 +322,8 @@ int zmk_rgb_underglow_cycle_effect(int direction) {
337
322
if (!led_strip )
338
323
return - ENODEV ;
339
324
340
- if (state .current_effect == 0 && direction < 0 ) {
341
- state .current_effect = UNDERGLOW_EFFECT_NUMBER - 1 ;
342
- return 0 ;
343
- }
344
-
345
- state .current_effect += direction ;
346
-
347
- if (state .current_effect >= UNDERGLOW_EFFECT_NUMBER ) {
348
- state .current_effect = 0 ;
349
- }
325
+ state .current_effect += UNDERGLOW_EFFECT_NUMBER + direction ;
326
+ state .current_effect %= UNDERGLOW_EFFECT_NUMBER ;
350
327
351
328
state .animation_step = 0 ;
352
329
@@ -357,47 +334,67 @@ int zmk_rgb_underglow_toggle() {
357
334
return state .on ? zmk_rgb_underglow_off () : zmk_rgb_underglow_on ();
358
335
}
359
336
360
- int zmk_rgb_underglow_set_hsb (uint16_t hue , uint8_t saturation , uint8_t brightness ) {
361
- if (hue > 360 || saturation > 100 || brightness > 100 ) {
337
+ int zmk_rgb_underglow_set_hsb (struct zmk_led_hsb color ) {
338
+ if (color . h > HUE_MAX || color . s > SAT_MAX || color . b > BRT_MAX ) {
362
339
return - ENOTSUP ;
363
340
}
364
341
365
- state .hue = hue ;
366
- state .saturation = saturation ;
367
- state .brightness = brightness ;
342
+ state .color = color ;
368
343
369
344
return 0 ;
370
345
}
371
346
372
- int zmk_rgb_underglow_change_hue (int direction ) {
373
- if (!led_strip )
374
- return - ENODEV ;
347
+ struct zmk_led_hsb zmk_rgb_underglow_calc_hue (int direction ) {
348
+ struct zmk_led_hsb color = state .color ;
375
349
376
- if (state .hue == 0 && direction < 0 ) {
377
- state .hue = 360 - CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP ;
378
- return 0 ;
350
+ color .h += HUE_MAX + (direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP );
351
+ color .h %= HUE_MAX ;
352
+
353
+ return color ;
354
+ }
355
+
356
+ struct zmk_led_hsb zmk_rgb_underglow_calc_sat (int direction ) {
357
+ struct zmk_led_hsb color = state .color ;
358
+
359
+ int s = color .s + (direction * CONFIG_ZMK_RGB_UNDERGLOW_SAT_STEP );
360
+ if (s < 0 ) {
361
+ s = 0 ;
362
+ } else if (s > SAT_MAX ) {
363
+ s = SAT_MAX ;
379
364
}
365
+ color .s = s ;
380
366
381
- state .hue += direction * CONFIG_ZMK_RGB_UNDERGLOW_HUE_STEP ;
367
+ return color ;
368
+ }
382
369
383
- state .hue = state .hue % 360 ;
370
+ struct zmk_led_hsb zmk_rgb_underglow_calc_brt (int direction ) {
371
+ struct zmk_led_hsb color = state .color ;
384
372
385
- return zmk_rgb_underglow_save_state ();
373
+ int b = color .b + (direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP );
374
+ if (b < 0 ) {
375
+ b = 0 ;
376
+ } else if (b > BRT_MAX ) {
377
+ b = BRT_MAX ;
378
+ }
379
+ color .b = b ;
380
+
381
+ return color ;
386
382
}
387
383
388
- int zmk_rgb_underglow_change_sat (int direction ) {
384
+ int zmk_rgb_underglow_change_hue (int direction ) {
389
385
if (!led_strip )
390
386
return - ENODEV ;
391
387
392
- if (state .saturation == 0 && direction < 0 ) {
393
- return 0 ;
394
- }
388
+ state .color = zmk_rgb_underglow_calc_hue (direction );
395
389
396
- state .saturation += direction * CONFIG_ZMK_RGB_UNDERGLOW_SAT_STEP ;
390
+ return zmk_rgb_underglow_save_state ();
391
+ }
397
392
398
- if (state .saturation > 100 ) {
399
- state .saturation = 100 ;
400
- }
393
+ int zmk_rgb_underglow_change_sat (int direction ) {
394
+ if (!led_strip )
395
+ return - ENODEV ;
396
+
397
+ state .color = zmk_rgb_underglow_calc_sat (direction );
401
398
402
399
return zmk_rgb_underglow_save_state ();
403
400
}
@@ -406,15 +403,7 @@ int zmk_rgb_underglow_change_brt(int direction) {
406
403
if (!led_strip )
407
404
return - ENODEV ;
408
405
409
- if (state .brightness == 0 && direction < 0 ) {
410
- return 0 ;
411
- }
412
-
413
- state .brightness += direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP ;
414
-
415
- if (state .brightness > 100 ) {
416
- state .brightness = 100 ;
417
- }
406
+ state .color = zmk_rgb_underglow_calc_brt (direction );
418
407
419
408
return zmk_rgb_underglow_save_state ();
420
409
}
0 commit comments