forked from Keychron/qmk_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeychron_bluetooth_playground.patch
959 lines (884 loc) · 33.6 KB
/
keychron_bluetooth_playground.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
diff --git a/quantum/led_matrix/led_matrix.c b/quantum/led_matrix/led_matrix.c
index 1676a60a..1f0b2bef 100644
--- a/quantum/led_matrix/led_matrix.c
+++ b/quantum/led_matrix/led_matrix.c
@@ -68,7 +68,7 @@ const led_point_t k_led_matrix_center = LED_MATRIX_CENTER;
#endif
#if !defined(LED_MATRIX_VAL_STEP)
-# define LED_MATRIX_VAL_STEP 8
+# define LED_MATRIX_VAL_STEP 16
#endif
#if !defined(LED_MATRIX_SPD_STEP)
@@ -87,6 +87,9 @@ const led_point_t k_led_matrix_center = LED_MATRIX_CENTER;
# define LED_MATRIX_DEFAULT_SPD UINT8_MAX / 2
#endif
+#if defined(LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL) && (LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL >= LED_MATRIX_MAXIMUM_BRIGHTNESS)
+# pragma error("LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL must be less than LED_MATRIX_MAXIMUM_BRIGHTNESS")
+#endif
// globals
led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
uint32_t g_led_timer;
@@ -98,6 +101,9 @@ last_hit_t g_last_hit_tracker;
#endif // LED_MATRIX_KEYREACTIVE_ENABLED
// internals
+#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE
+static bool driver_shutdown = false;
+#endif
static bool suspend_state = false;
static uint8_t led_last_enable = UINT8_MAX;
static uint8_t led_last_effect = UINT8_MAX;
@@ -105,7 +111,8 @@ static effect_params_t led_effect_params = {0, LED_FLAG_ALL, false};
static led_task_states led_task_state = SYNCING;
#if LED_MATRIX_TIMEOUT > 0
static uint32_t led_anykey_timer;
-#endif // LED_MATRIX_TIMEOUT > 0
+static uint32_t led_matrix_timeout = LED_MATRIX_TIMEOUT;
+#endif // LED_MATRIX_TIMEOUT > 0
// double buffers
static uint32_t led_timer_buffer;
@@ -120,6 +127,8 @@ const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT;
EECONFIG_DEBOUNCE_HELPER(led_matrix, EECONFIG_LED_MATRIX, led_matrix_eeconfig);
+void led_matrix_increase_val_helper(bool write_to_eeprom);
+
void eeconfig_update_led_matrix(void) {
eeconfig_flush_led_matrix(true);
}
@@ -227,12 +236,22 @@ void process_led_matrix(uint8_t row, uint8_t col, bool pressed) {
#endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_LED_MATRIX_TYPING_HEATMAP)
}
+void led_matrix_none_indicators(void) {
+ led_matrix_none_indicators_kb();
+ led_matrix_none_indicators_user();
+}
+
+__attribute__((weak)) void led_matrix_none_indicators_kb(void) {}
+
+__attribute__((weak)) void led_matrix_none_indicators_user(void) {}
+
static bool led_matrix_none(effect_params_t *params) {
if (!params->init) {
return false;
}
led_matrix_set_value_all(0);
+ led_matrix_none_indicators();
return false;
}
@@ -343,10 +362,24 @@ static void led_task_flush(uint8_t effect) {
// update last trackers after the first full render so we can init over several frames
led_last_effect = effect;
led_last_enable = led_matrix_eeconfig.enable;
+#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE
+ // exit from shutdown to if neccesary
+ if (driver_shutdown) {
+ led_matrix_driver.exit_shutdown();
+ driver_shutdown = false;
+ }
+#endif
// update pwm buffers
led_matrix_update_pwm_buffers();
+#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE
+ // shutdown if neccesary
+ if (effect == LED_MATRIX_NONE && !driver_shutdown && led_matrix_driver_allow_shutdown()) {
+ led_matrix_driver_shutdown();
+ }
+#endif
+
// next task
led_task_state = SYNCING;
}
@@ -358,8 +391,8 @@ void led_matrix_task(void) {
// while suspended and just do a software shutdown. This is a cheap hack for now.
bool suspend_backlight = suspend_state ||
#if LED_MATRIX_TIMEOUT > 0
- (led_anykey_timer > (uint32_t)LED_MATRIX_TIMEOUT) ||
-#endif // LED_MATRIX_TIMEOUT > 0
+ (led_anykey_timer > led_matrix_timeout) ||
+#endif // led_matrix_timeout > 0
false;
uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode;
@@ -428,6 +461,9 @@ __attribute__((weak)) bool led_matrix_indicators_advanced_user(uint8_t led_min,
void led_matrix_init(void) {
led_matrix_driver.init();
+#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE
+ driver_shutdown = false;
+#endif
#ifdef LED_MATRIX_KEYREACTIVE_ENABLED
g_last_hit_tracker.count = 0;
@@ -474,6 +510,11 @@ void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
led_task_state = STARTING;
eeconfig_flag_led_matrix(write_to_eeprom);
dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable);
+#ifdef LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ while (led_matrix_eeconfig.enable && led_matrix_eeconfig.val <= LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
+ led_matrix_increase_val_helper(write_to_eeprom);
+ }
+#endif
}
void led_matrix_toggle_noeeprom(void) {
led_matrix_toggle_eeprom_helper(false);
@@ -485,11 +526,21 @@ void led_matrix_toggle(void) {
void led_matrix_enable(void) {
led_matrix_enable_noeeprom();
eeconfig_flag_led_matrix(true);
+#ifdef LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ while (led_matrix_eeconfig.val <= LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
+ led_matrix_increase_val_helper(true);
+ }
+#endif
}
void led_matrix_enable_noeeprom(void) {
if (!led_matrix_eeconfig.enable) led_task_state = STARTING;
led_matrix_eeconfig.enable = 1;
+#ifdef LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ while (led_matrix_eeconfig.val <= LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
+ led_matrix_increase_val_helper(false);
+ }
+#endif
}
void led_matrix_disable(void) {
@@ -574,6 +625,12 @@ uint8_t led_matrix_get_val(void) {
}
void led_matrix_increase_val_helper(bool write_to_eeprom) {
+#ifdef LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ if (!led_matrix_eeconfig.enable) {
+ led_matrix_toggle_eeprom_helper(write_to_eeprom);
+ return;
+ }
+#endif
led_matrix_set_val_eeprom_helper(qadd8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom);
}
void led_matrix_increase_val_noeeprom(void) {
@@ -585,6 +642,11 @@ void led_matrix_increase_val(void) {
void led_matrix_decrease_val_helper(bool write_to_eeprom) {
led_matrix_set_val_eeprom_helper(qsub8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom);
+#ifdef LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ if (led_matrix_eeconfig.enable && led_matrix_eeconfig.val <= LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
+ led_matrix_toggle_eeprom_helper(write_to_eeprom);
+ }
+#endif
}
void led_matrix_decrease_val_noeeprom(void) {
led_matrix_decrease_val_helper(false);
@@ -646,3 +708,25 @@ void led_matrix_set_flags(led_flags_t flags) {
void led_matrix_set_flags_noeeprom(led_flags_t flags) {
led_matrix_set_flags_eeprom_helper(flags, false);
}
+
+#if LED_MATRIX_TIMEOUT > 0
+void led_matrix_disable_timeout_set(uint32_t timeout) {
+ led_matrix_timeout = timeout;
+}
+void led_matrix_disable_time_reset(void){
+ led_anykey_timer = 0;
+}
+#endif
+
+#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE
+void led_matrix_driver_shutdown(void) {
+ led_matrix_driver.shutdown();
+ driver_shutdown = true;
+};
+
+bool led_matrix_is_driver_shutdown(void) {
+ return driver_shutdown;
+}
+
+__attribute__((weak)) bool led_matrix_driver_allow_shutdown(void) { return true; };
+#endif
diff --git a/quantum/led_matrix/led_matrix.h b/quantum/led_matrix/led_matrix.h
index c2533ca4..8f16ec0d 100644
--- a/quantum/led_matrix/led_matrix.h
+++ b/quantum/led_matrix/led_matrix.h
@@ -79,6 +79,8 @@
#define LED_MATRIX_TEST_LED_FLAGS() \
if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue
+#define LED_MATRIX_TIMEOUT_INFINITE (UINT32_MAX)
+
enum led_matrix_effects {
LED_MATRIX_NONE = 0,
@@ -118,6 +120,9 @@ void process_led_matrix(uint8_t row, uint8_t col, bool pressed);
void led_matrix_task(void);
+void led_matrix_none_indicators_kb(void);
+void led_matrix_none_indicators_user(void);
+
// This runs after another backlight effect and replaces
// values already set
void led_matrix_indicators(void);
@@ -164,6 +169,19 @@ led_flags_t led_matrix_get_flags(void);
void led_matrix_set_flags(led_flags_t flags);
void led_matrix_set_flags_noeeprom(led_flags_t flags);
+#ifdef LED_MATRIX_TIMEOUT
+# if LED_MATRIX_TIMEOUT > 0
+void led_matrix_disable_timeout_set(uint32_t timeout);
+void led_matrix_disable_time_reset(void);
+# endif
+#endif
+
+#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE
+void led_matrix_driver_shutdown(void);
+bool led_matrix_is_driver_shutdown(void);
+bool led_matrix_driver_allow_shutdown(void);
+#endif
+
typedef struct {
/* Perform any initialisation required for the other driver functions to work. */
void (*init)(void);
@@ -174,6 +192,12 @@ typedef struct {
void (*set_value_all)(uint8_t value);
/* Flush any buffered changes to the hardware. */
void (*flush)(void);
+#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE
+ /* Shutdown the driver. */
+ void (*shutdown)(void);
+ /* Exit from shutdown state. */
+ void (*exit_shutdown)(void);
+#endif
} led_matrix_driver_t;
static inline bool led_matrix_check_finished_leds(uint8_t led_idx) {
diff --git a/quantum/led_matrix/led_matrix_drivers.c b/quantum/led_matrix/led_matrix_drivers.c
index 13c8935d..8f4c332e 100644
--- a/quantum/led_matrix/led_matrix_drivers.c
+++ b/quantum/led_matrix/led_matrix_drivers.c
@@ -17,6 +17,8 @@
#include "led_matrix.h"
+#include "gpio.h"
+
/* Each driver needs to define a struct:
*
* const led_matrix_driver_t led_matrix_driver;
@@ -197,9 +199,9 @@ static void flush(void) {
}
const led_matrix_driver_t led_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_value = is31fl3733_set_value,
+ .init = init,
+ .flush = flush,
+ .set_value = is31fl3733_set_value,
.set_value_all = is31fl3733_set_value_all,
};
@@ -218,9 +220,9 @@ static void flush(void) {
}
const led_matrix_driver_t led_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_value = IS31FL_simple_set_brightness,
+ .init = init,
+ .flush = flush,
+ .set_value = IS31FL_simple_set_brightness,
.set_value_all = IS31FL_simple_set_brigntness_all,
};
# elif defined(CKLED2001)
@@ -237,11 +239,41 @@ static void flush(void) {
# endif
}
-const led_matrix_driver_t led_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_value = ckled2001_set_value,
- .set_value_all = ckled2001_set_value_all,
+# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE)
+static void shutdown(void) {
+# if defined(LED_DRIVER_SHUTDOWN_PIN)
+ writePinLow(LED_DRIVER_SHUTDOWN_PIN);
+# else
+ CKLED2001_sw_shutdown(DRIVER_ADDR_1);
+# if defined(DRIVER_ADDR_2)
+ CKLED2001_sw_shutdown(DRIVER_ADDR_2);
+# if defined(DRIVER_ADDR_3)
+ CKLED2001_sw_shutdown(DRIVER_ADDR_3);
+# if defined(DRIVER_ADDR_4)
+ CKLED2001_sw_shutdown(DRIVER_ADDR_4);
+# endif
+# endif
+# endif
+# endif
+}
+
++ static void exit_shutdown(void) {
+ +#if defined (LED_DRIVER_SHUTDOWN_PIN) + writePinHigh(LED_DRIVER_SHUTDOWN_PIN);
+ +#else + CKLED2001_sw_return_normal(DRIVER_ADDR_1);
+ +#if defined (DRIVER_ADDR_2) + CKLED2001_sw_return_normal(DRIVER_ADDR_2);
+ +#if defined (DRIVER_ADDR_3) + CKLED2001_sw_return_normal(DRIVER_ADDR_3);
+ +#if defined (DRIVER_ADDR_4) + CKLED2001_sw_return_normal(DRIVER_ADDR_4);
+ +#endif + #endif + #endif + #endif +
+}
++ #endif +
+ const led_matrix_driver_t led_matrix_driver = {.init = init,
+ .flush = flush,
+ .set_value = ckled2001_set_value,
+ .set_value_all = ckled2001_set_value_all,
+# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE)
+ .shutdown = shutdown,
+ .exit_shutdown = exit_shutdown
+# endif
};
+# endif
# endif
-#endif
diff --git a/quantum/rgb_matrix/rgb_matrix.c b/quantum/rgb_matrix/rgb_matrix.c
index 96be6151..606173be 100644
--- a/quantum/rgb_matrix/rgb_matrix.c
+++ b/quantum/rgb_matrix/rgb_matrix.c
@@ -110,6 +110,10 @@ __attribute__((weak)) RGB rgb_matrix_hsv_to_rgb(HSV hsv) {
# define RGB_MATRIX_DEFAULT_SPD UINT8_MAX / 2
#endif
+#if defined(RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL) && (RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL >= RGB_MATRIX_MAXIMUM_BRIGHTNESS)
+# pragma error("RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL must be less than RGB_MATRIX_MAXIMUM_BRIGHTNESS")
+#endif
+
// globals
rgb_config_t rgb_matrix_config; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
uint32_t g_rgb_timer;
@@ -121,6 +125,9 @@ last_hit_t g_last_hit_tracker;
#endif // RGB_MATRIX_KEYREACTIVE_ENABLED
// internals
+#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE
+static bool driver_shutdown = false;
+#endif
static bool suspend_state = false;
static uint8_t rgb_last_enable = UINT8_MAX;
static uint8_t rgb_last_effect = UINT8_MAX;
@@ -128,7 +135,8 @@ static effect_params_t rgb_effect_params = {0, LED_FLAG_ALL, false};
static rgb_task_states rgb_task_state = SYNCING;
#if RGB_MATRIX_TIMEOUT > 0
static uint32_t rgb_anykey_timer;
-#endif // RGB_MATRIX_TIMEOUT > 0
+static uint32_t rgb_matrix_timeout = RGB_MATRIX_TIMEOUT;
+#endif // RGB_MATRIX_TIMEOUT > 0
// double buffers
static uint32_t rgb_timer_buffer;
@@ -143,6 +151,8 @@ const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
EECONFIG_DEBOUNCE_HELPER(rgb_matrix, EECONFIG_RGB_MATRIX, rgb_matrix_config);
+void rgb_matrix_increase_val_helper(bool write_to_eeprom);
+
void eeconfig_update_rgb_matrix(void) {
eeconfig_flush_rgb_matrix(true);
}
@@ -286,12 +296,22 @@ void rgb_matrix_test(void) {
}
}
+void rgb_matrix_none_indicators(void) {
+ rgb_matrix_none_indicators_kb();
+ rgb_matrix_none_indicators_user();
+}
+
+__attribute__((weak)) void rgb_matrix_none_indicators_kb(void) {}
+
+__attribute__((weak)) void rgb_matrix_none_indicators_user(void) {}
+
static bool rgb_matrix_none(effect_params_t *params) {
if (!params->init) {
return false;
}
rgb_matrix_set_color_all(0, 0, 0);
+ rgb_matrix_none_indicators();
return false;
}
@@ -405,9 +425,21 @@ static void rgb_task_flush(uint8_t effect) {
// update last trackers after the first full render so we can init over several frames
rgb_last_effect = effect;
rgb_last_enable = rgb_matrix_config.enable;
-
+#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE
+ // exit from shutdown to if neccesary
+ if (driver_shutdown) {
+ rgb_matrix_driver.exit_shutdown();
+ driver_shutdown = false;
+ }
+#endif
// update pwm buffers
rgb_matrix_update_pwm_buffers();
+#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE
+ // shutdown to if neccesary
+ if (effect == RGB_MATRIX_NONE && !driver_shutdown && rgb_matrix_driver_allow_shutdown()) {
+ rgb_matrix_driver_shutdown();
+ }
+#endif
// next task
rgb_task_state = SYNCING;
@@ -420,7 +452,7 @@ void rgb_matrix_task(void) {
// while suspended and just do a software shutdown. This is a cheap hack for now.
bool suspend_backlight = suspend_state ||
#if RGB_MATRIX_TIMEOUT > 0
- (rgb_anykey_timer > (uint32_t)RGB_MATRIX_TIMEOUT) ||
+ (rgb_anykey_timer > rgb_matrix_timeout) ||
#endif // RGB_MATRIX_TIMEOUT > 0
false;
@@ -481,6 +513,9 @@ __attribute__((weak)) bool rgb_matrix_indicators_advanced_user(uint8_t led_min,
void rgb_matrix_init(void) {
rgb_matrix_driver.init();
+#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE
+ driver_shutdown = false;
+#endif
#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
g_last_hit_tracker.count = 0;
@@ -527,6 +562,11 @@ void rgb_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
rgb_task_state = STARTING;
eeconfig_flag_rgb_matrix(write_to_eeprom);
dprintf("rgb matrix toggle [%s]: rgb_matrix_config.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.enable);
+#ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ while (rgb_matrix_config.enable && rgb_matrix_config.hsv.v < RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
+ rgb_matrix_increase_val_helper(write_to_eeprom);
+ }
+#endif
}
void rgb_matrix_toggle_noeeprom(void) {
rgb_matrix_toggle_eeprom_helper(false);
@@ -538,11 +578,21 @@ void rgb_matrix_toggle(void) {
void rgb_matrix_enable(void) {
rgb_matrix_enable_noeeprom();
eeconfig_flag_rgb_matrix(true);
+#ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ while (rgb_matrix_config.hsv.v < RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
+ rgb_matrix_increase_val_helper(true);
+ }
+#endif
}
void rgb_matrix_enable_noeeprom(void) {
if (!rgb_matrix_config.enable) rgb_task_state = STARTING;
rgb_matrix_config.enable = 1;
+#ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ while (rgb_matrix_config.hsv.v < RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
+ rgb_matrix_increase_val_helper(false);
+ }
+#endif
}
void rgb_matrix_disable(void) {
@@ -678,6 +728,12 @@ void rgb_matrix_decrease_sat(void) {
}
void rgb_matrix_increase_val_helper(bool write_to_eeprom) {
+#ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ if (!rgb_matrix_config.enable) {
+ rgb_matrix_toggle_eeprom_helper(write_to_eeprom);
+ return;
+ }
+#endif
rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom);
}
void rgb_matrix_increase_val_noeeprom(void) {
@@ -689,6 +745,11 @@ void rgb_matrix_increase_val(void) {
void rgb_matrix_decrease_val_helper(bool write_to_eeprom) {
rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom);
+#ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
+ if (rgb_matrix_config.enable && rgb_matrix_config.hsv.v <= RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
+ rgb_matrix_toggle_eeprom_helper(write_to_eeprom);
+ }
+#endif
}
void rgb_matrix_decrease_val_noeeprom(void) {
rgb_matrix_decrease_val_helper(false);
@@ -750,3 +811,22 @@ void rgb_matrix_set_flags(led_flags_t flags) {
void rgb_matrix_set_flags_noeeprom(led_flags_t flags) {
rgb_matrix_set_flags_eeprom_helper(flags, false);
}
+
+
+#if RGB_MATRIX_TIMEOUT > 0
+void rgb_matrix_disable_timeout_set(uint32_t timeout) {
+ rgb_matrix_timeout = timeout;
+}
+void rgb_matrix_disable_time_reset(void){ rgb_anykey_timer = 0; }
+#endif
+
+#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE
+void rgb_matrix_driver_shutdown(void) {
+ rgb_matrix_driver.shutdown();
+ driver_shutdown = true;
+};
+
+bool rgb_matrix_is_driver_shutdown(void) { return driver_shutdown; }
+
+__attribute__((weak)) bool rgb_matrix_driver_allow_shutdown(void) { return true; };
+#endif
diff --git a/quantum/rgb_matrix/rgb_matrix.h b/quantum/rgb_matrix/rgb_matrix.h
index 38040fb0..ebaed359 100644
--- a/quantum/rgb_matrix/rgb_matrix.h
+++ b/quantum/rgb_matrix/rgb_matrix.h
@@ -93,6 +93,8 @@
#define RGB_MATRIX_TEST_LED_FLAGS() \
if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue
+#define RGB_MATRIX_TIMEOUT_INFINITE (UINT32_MAX)
+
enum rgb_matrix_effects {
RGB_MATRIX_NONE = 0,
@@ -131,6 +133,9 @@ void process_rgb_matrix(uint8_t row, uint8_t col, bool pressed);
void rgb_matrix_task(void);
+void rgb_matrix_none_indicators_kb(void);
+void rgb_matrix_none_indicators_user(void);
+
// This runs after another backlight effect and replaces
// colors already set
void rgb_matrix_indicators(void);
@@ -189,6 +194,17 @@ void rgb_matrix_decrease_speed_noeeprom(void);
led_flags_t rgb_matrix_get_flags(void);
void rgb_matrix_set_flags(led_flags_t flags);
void rgb_matrix_set_flags_noeeprom(led_flags_t flags);
+#ifdef RGB_MATRIX_TIMEOUT
+# if RGB_MATRIX_TIMEOUT > 0
+void rgb_matrix_disable_timeout_set(uint32_t timeout);
+void rgb_matrix_disable_time_reset(void);
+# endif
+#endif
+#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE
+void rgb_matrix_driver_shutdown(void);
+bool rgb_matrix_is_driver_shutdown(void);
+bool rgb_matrix_driver_allow_shutdown(void);
+#endif
#ifndef RGBLIGHT_ENABLE
# define eeconfig_update_rgblight_current eeconfig_update_rgb_matrix
@@ -243,6 +259,12 @@ typedef struct {
void (*set_color_all)(uint8_t r, uint8_t g, uint8_t b);
/* Flush any buffered changes to the hardware. */
void (*flush)(void);
+#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE
+ /* Shutdown the driver. */
+ void (*shutdown)(void);
+ /* Exit from shutdown state. */
+ void (*exit_shutdown)(void);
+#endif
} rgb_matrix_driver_t;
static inline bool rgb_matrix_check_finished_leds(uint8_t led_idx) {
diff --git a/quantum/rgb_matrix/rgb_matrix_drivers.c b/quantum/rgb_matrix/rgb_matrix_drivers.c
index 695ecc78..e225a318 100644
--- a/quantum/rgb_matrix/rgb_matrix_drivers.c
+++ b/quantum/rgb_matrix/rgb_matrix_drivers.c
@@ -17,6 +17,8 @@
#include "rgb_matrix.h"
#include "util.h"
+#include "gpio.h"
+
/* Each driver needs to define the struct
* const rgb_matrix_driver_t rgb_matrix_driver;
* All members must be provided.
@@ -281,9 +283,9 @@ static void flush(void) {
}
const rgb_matrix_driver_t rgb_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_color = is31fl3733_set_color,
+ .init = init,
+ .flush = flush,
+ .set_color = is31fl3733_set_color,
.set_color_all = is31fl3733_set_color_all,
};
@@ -302,9 +304,9 @@ static void flush(void) {
}
const rgb_matrix_driver_t rgb_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_color = is31fl3736_set_color,
+ .init = init,
+ .flush = flush,
+ .set_color = is31fl3736_set_color,
.set_color_all = is31fl3736_set_color_all,
};
@@ -323,9 +325,9 @@ static void flush(void) {
}
const rgb_matrix_driver_t rgb_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_color = is31fl3737_set_color,
+ .init = init,
+ .flush = flush,
+ .set_color = is31fl3737_set_color,
.set_color_all = is31fl3737_set_color_all,
};
@@ -344,9 +346,9 @@ static void flush(void) {
}
const rgb_matrix_driver_t rgb_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_color = is31fl3741_set_color,
+ .init = init,
+ .flush = flush,
+ .set_color = is31fl3741_set_color,
.set_color_all = is31fl3741_set_color_all,
};
@@ -365,9 +367,9 @@ static void flush(void) {
}
const rgb_matrix_driver_t rgb_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_color = IS31FL_RGB_set_color,
+ .init = init,
+ .flush = flush,
+ .set_color = IS31FL_RGB_set_color,
.set_color_all = IS31FL_RGB_set_color_all,
};
@@ -385,11 +387,50 @@ static void flush(void) {
# endif
}
-const rgb_matrix_driver_t rgb_matrix_driver = {
- .init = init,
- .flush = flush,
- .set_color = ckled2001_set_color,
- .set_color_all = ckled2001_set_color_all,
+# if defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
+static void shutdown(void) {
+# if defined(LED_DRIVER_SHUTDOWN_PIN)
+ writePinLow(LED_DRIVER_SHUTDOWN_PIN);
+# else
+ CKLED2001_sw_shutdown(DRIVER_ADDR_1);
+# if defined(DRIVER_ADDR_2)
+ CKLED2001_sw_shutdown(DRIVER_ADDR_2);
+# if defined(DRIVER_ADDR_3)
+ CKLED2001_sw_shutdown(DRIVER_ADDR_3);
+# if defined(DRIVER_ADDR_4)
+ CKLED2001_sw_shutdown(DRIVER_ADDR_4);
+# endif
+# endif
+# endif
+# endif
+}
+
+static void exit_shutdown(void) {
+# if defined(LED_DRIVER_SHUTDOWN_PIN)
+ writePinHigh(LED_DRIVER_SHUTDOWN_PIN);
+# else
+ CKLED2001_sw_return_normal(DRIVER_ADDR_1);
+# if defined(DRIVER_ADDR_2)
+ CKLED2001_sw_return_normal(DRIVER_ADDR_2);
+# if defined(DRIVER_ADDR_3)
+ CKLED2001_sw_return_normal(DRIVER_ADDR_3);
+# if defined(DRIVER_ADDR_4)
+ CKLED2001_sw_return_normal(DRIVER_ADDR_4);
+# endif
+# endif
+# endif
+# endif
+}
+# endif
+
+const rgb_matrix_driver_t rgb_matrix_driver = {.init = init,
+ .flush = flush,
+ .set_color = ckled2001_set_color,
+ .set_color_all = ckled2001_set_color_all,
+# if defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
+ .shutdown = shutdown,
+ .exit_shutdown = exit_shutdown
+# endif
};
# endif
diff --git a/quantum/via.c b/quantum/via.c
index 2acd7aa9..41c2b2b7 100644
--- a/quantum/via.c
+++ b/quantum/via.c
@@ -695,8 +695,18 @@ void via_qmk_rgb_matrix_set_value(uint8_t *data) {
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
- case id_qmk_rgb_matrix_brightness: {
+ case id_qmk_rgblight_brightness: {
+#ifdef RGB_MATRIX_TURN_OFF_VAL
+ if (!rgb_matrix_is_enabled() && value_data[0] >= RGB_MATRIX_TURN_OFF_VAL) {
+ rgb_matrix_toggle_noeeprom();
+ }
+#endif
rgb_matrix_sethsv_noeeprom(rgb_matrix_get_hue(), rgb_matrix_get_sat(), scale8(value_data[0], RGB_MATRIX_MAXIMUM_BRIGHTNESS));
+#ifdef RGB_MATRIX_TURN_OFF_VAL
+ if (rgb_matrix_is_enabled() && value_data[0] < RGB_MATRIX_TURN_OFF_VAL) {
+ rgb_matrix_toggle_noeeprom();
+ }
+#endif
break;
}
case id_qmk_rgb_matrix_effect: {
diff --git a/tmk_core/protocol.mk b/tmk_core/protocol.mk
index d159dacc..c82508cc 100644
--- a/tmk_core/protocol.mk
+++ b/tmk_core/protocol.mk
@@ -48,8 +48,6 @@ endif
ifeq ($(strip $(NKRO_ENABLE)), yes)
ifeq ($(PROTOCOL), VUSB)
$(info NKRO is not currently supported on V-USB, and has been disabled.)
- else ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)
- $(info NKRO is not currently supported with Bluetooth, and has been disabled.)
else
OPT_DEFS += -DNKRO_ENABLE
SHARED_EP_ENABLE = yes
diff --git a/tmk_core/protocol/chibios/chibios.c b/tmk_core/protocol/chibios/chibios.c
index 4d97f1cd..22f9601e 100644
--- a/tmk_core/protocol/chibios/chibios.c
+++ b/tmk_core/protocol/chibios/chibios.c
@@ -191,7 +191,7 @@ void protocol_pre_task(void) {
/* Remote wakeup */
if ((USB_DRIVER.status & USB_GETSTATUS_REMOTE_WAKEUP_ENABLED) && suspend_wakeup_condition()) {
usbWakeupHost(&USB_DRIVER);
- restart_usb_driver(&USB_DRIVER);
+ usb_wakeup(&USB_DRIVER);
}
}
/* Woken up */
diff --git a/tmk_core/protocol/chibios/usb_driver.c b/tmk_core/protocol/chibios/usb_driver.c
index ad45f9b1..0e3f28e3 100644
--- a/tmk_core/protocol/chibios/usb_driver.c
+++ b/tmk_core/protocol/chibios/usb_driver.c
@@ -408,7 +408,7 @@ void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep) {
size. Otherwise the recipient may expect more data coming soon and
not return buffered data to app. See section 5.8.3 Bulk Transfer
Packet Size Constraints of the USB Specification document.*/
- if (!qmkusbp->config->fixed_size) {
+ if (!qmkusbp->config->fixed_size && (usbp->epc[ep]->ep_mode & USB_EP_MODE_TYPE) == USB_EP_MODE_TYPE_BULK) {
usbStartTransmitI(usbp, ep, usbp->setup, 0);
}
diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c
index b14ca30c..ddeeab43 100644
--- a/tmk_core/protocol/chibios/usb_main.c
+++ b/tmk_core/protocol/chibios/usb_main.c
@@ -798,20 +798,23 @@ void init_usb_driver(USBDriver *usbp) {
chVTObjectInit(&keyboard_idle_timer);
}
-__attribute__((weak)) void restart_usb_driver(USBDriver *usbp) {
- usbDisconnectBus(usbp);
- usbStop(usbp);
+__attribute__((weak)) void usb_wakeup(USBDriver *usbp) {
+#if STM32_USB_USE_OTG1 || STM32_USB_USE_OTG1
+ stm32_otg_t *otgp = usbp->otg;
-#if USB_SUSPEND_WAKEUP_DELAY > 0
- // Some hubs, kvm switches, and monitors do
- // weird things, with USB device state bouncing
- // around wildly on wakeup, yielding race
- // conditions that can corrupt the keyboard state.
- //
- // Pause for a while to let things settle...
- wait_ms(USB_SUSPEND_WAKEUP_DELAY);
+ osalSysLock();
+ /* If clocks are gated off, turn them back on (may be the case if
+ coming out of suspend mode).*/
+ if (otgp->PCGCCTL & (PCGCCTL_STPPCLK | PCGCCTL_GATEHCLK)) {
+ /* Set to zero to un-gate the USB core clocks.*/
+ otgp->PCGCCTL &= ~(PCGCCTL_STPPCLK | PCGCCTL_GATEHCLK);
+ }
+ _usb_wakeup(usbp);
+ osalSysUnlock();
#endif
+}
+__attribute__((weak)) void usb_start(USBDriver *usbp) {
usbStart(usbp, &usbcfg);
usbConnectBus(usbp);
}
diff --git a/tmk_core/protocol/chibios/usb_main.h b/tmk_core/protocol/chibios/usb_main.h
index 07186f76..70b91121 100644
--- a/tmk_core/protocol/chibios/usb_main.h
+++ b/tmk_core/protocol/chibios/usb_main.h
@@ -33,8 +33,11 @@
/* Initialize the USB driver and bus */
void init_usb_driver(USBDriver *usbp);
-/* Restart the USB driver and bus */
-void restart_usb_driver(USBDriver *usbp);
+/* Wakeup the USB driver */
+void usb_wakeup(USBDriver *usbp);
+
+/* Start the USB driver */
+void usb_start(USBDriver *usbp);
/* ---------------
* USB Event queue
diff --git a/tmk_core/protocol/host.h b/tmk_core/protocol/host.h
index add408ca..48374567 100644
--- a/tmk_core/protocol/host.h
+++ b/tmk_core/protocol/host.h
@@ -32,6 +32,7 @@ extern "C" {
extern uint8_t keyboard_idle;
extern uint8_t keyboard_protocol;
+extern uint8_t bluetooth_report_protocol;
/* host driver */
void host_set_driver(host_driver_t *driver);
diff --git a/tmk_core/protocol/report.c b/tmk_core/protocol/report.c
index 1ba3be46..172cdec3 100644
--- a/tmk_core/protocol/report.c
+++ b/tmk_core/protocol/report.c
@@ -20,6 +20,9 @@
#include "debug.h"
#include "util.h"
#include <string.h>
+#ifdef BLUETOOTH_ENABLE
+#include "transport.h"
+#endif
#ifdef RING_BUFFERED_6KRO_REPORT_ENABLE
# define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
@@ -40,7 +43,14 @@ uint8_t has_anykey(report_keyboard_t* keyboard_report) {
uint8_t* p = keyboard_report->keys;
uint8_t lp = sizeof(keyboard_report->keys);
#ifdef NKRO_ENABLE
+#ifdef BLUETOOTH_ENABLE
+ if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) ||
+ ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol))
+ && keymap_config.nkro) {
+#else
if (keyboard_protocol && keymap_config.nkro) {
+#endif
+
p = keyboard_report->nkro.bits;
lp = sizeof(keyboard_report->nkro.bits);
}
@@ -57,7 +67,13 @@ uint8_t has_anykey(report_keyboard_t* keyboard_report) {
*/
uint8_t get_first_key(report_keyboard_t* keyboard_report) {
#ifdef NKRO_ENABLE
+#ifdef BLUETOOTH_ENABLE
+ if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) ||
+ ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol))
+ && keymap_config.nkro) {
+#else
if (keyboard_protocol && keymap_config.nkro) {
+#endif
uint8_t i = 0;
for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
;
@@ -88,7 +104,13 @@ bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) {
return false;
}
#ifdef NKRO_ENABLE
+#ifdef BLUETOOTH_ENABLE
+ if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) ||
+ ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol))
+ && keymap_config.nkro) {
+#else
if (keyboard_protocol && keymap_config.nkro) {
+#endif
if ((key >> 3) < KEYBOARD_REPORT_BITS) {
return keyboard_report->nkro.bits[key >> 3] & 1 << (key & 7);
} else {
@@ -242,7 +264,13 @@ void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) {
*/
void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) {
#ifdef NKRO_ENABLE
+#ifdef BLUETOOTH_ENABLE
+ if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) ||
+ ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol))
+ && keymap_config.nkro) {
+#else
if (keyboard_protocol && keymap_config.nkro) {
+#endif
add_key_bit(keyboard_report, key);
return;
}
@@ -256,7 +284,13 @@ void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) {
*/
void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) {
#ifdef NKRO_ENABLE
+#ifdef BLUETOOTH_ENABLE
+ if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) ||
+ ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol))
+ && keymap_config.nkro) {
+#else
if (keyboard_protocol && keymap_config.nkro) {
+#endif
del_key_bit(keyboard_report, key);
return;
}
@@ -271,7 +305,13 @@ void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) {
void clear_keys_from_report(report_keyboard_t* keyboard_report) {
// not clear mods
#ifdef NKRO_ENABLE
+#ifdef BLUETOOTH_ENABLE
+ if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) ||
+ ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol))
+ && keymap_config.nkro) {
+#else
if (keyboard_protocol && keymap_config.nkro) {
+#endif
memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits));
return;
}