forked from kiibohd/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel.c
3709 lines (3156 loc) · 92.7 KB
/
pixel.c
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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2015-2019 by Jacob Alexander
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/MacroLib.h>
// Project Includes
#include <Lib/storage.h>
#include <cli.h>
#include <layer.h>
#include <trigger.h>
#include <kll_defs.h>
#include <latency.h>
#include <led.h>
#include <print.h>
#include <output_com.h>
// Interconnect module if compiled in
#if defined(ConnectEnabled_define)
#include <connect_scan.h>
#endif
// Local Includes
#include "pixel.h"
// ----- Function Declarations -----
void Pixel_loadConfig();
void Pixel_saveConfig();
void Pixel_printConfig();
void cliFunc_aniAdd ( char* args );
void cliFunc_aniDel ( char* args );
void cliFunc_aniStack ( char* args );
void cliFunc_chanTest ( char* args );
void cliFunc_pixelList ( char* args );
void cliFunc_pixelSCTest( char* args );
void cliFunc_pixelTest ( char* args );
void cliFunc_pixelXYTest( char* args );
void cliFunc_rectDisp ( char* args );
// ----- Enums -----
typedef enum PixelTest {
PixelTest_Off = 0, // Disabled
PixelTest_Chan_Single = 1,
PixelTest_Chan_SingleReverse = 2,
PixelTest_Chan_All = 3, // Enable all positions
PixelTest_Chan_Roll = 4, // Iterate over all positions
PixelTest_Chan_Full = 5, // Turn on all pixels
PixelTest_Chan_Off = 6, // Turn off all pixels
PixelTest_Pixel_Single = 10,
PixelTest_Pixel_SingleReverse = 11,
PixelTest_Pixel_All = 12, // Enable all positions
PixelTest_Pixel_Roll = 13, // Iterate over all positions
PixelTest_Pixel_Full = 14, // Turn on all pixels
PixelTest_Pixel_Off = 15, // Turn off all pixels
PixelTest_Scan_Single = 20,
PixelTest_Scan_SingleReverse = 21,
PixelTest_Scan_All = 22,
PixelTest_Scan_Roll = 23,
PixelTest_XY_Single = 30,
PixelTest_XY_SingleReverse = 31,
PixelTest_XY_All = 32,
PixelTest_XY_Roll = 33,
} PixelTest;
typedef enum PixelFadeControl {
PixelFadeControl_Reset = 0, // Resets fade profile to defaults (arg ignored)
PixelFadeControl_Reset_All = 1, // Resets all fade profiles to defaults (profile, arg ignored)
PixelFadeControl_Brightness_Set = 2, // Sets fade profile to a given brightness
PixelFadeControl_Brightness_Increment = 3, // Increment brightness by given amount
PixelFadeControl_Brightness_Decrement = 4, // Decrement brightness by given amount
PixelFadeControl_Brightness_Default = 5, // Set profile brightness to default
PixelFadeControl_LAST,
} PixelFadeControl;
// ----- Variables -----
typedef struct {
uint8_t index;
uint8_t pos;
} PixelConfigElem;
typedef struct {
PixelConfigElem animations[Pixel_AnimationStackSize];
PixelPeriodConfig fade_periods[4][4];
uint8_t fade_brightness[4];
} PixelConfig;
static PixelConfig settings;
#if Storage_Enable_define == 1
static PixelConfig defaults;
static StorageModule PixelStorage = {
.name = "Pixel Map",
.settings = &settings,
.defaults = &defaults,
.size = sizeof(PixelConfig),
.onLoad = Pixel_loadConfig,
.onSave = Pixel_saveConfig,
.display = Pixel_printConfig
};
#endif
// Macro Module command dictionary
CLIDict_Entry( aniAdd, "Add the given animation id to the stack" );
CLIDict_Entry( aniDel, "Remove the given stack index animation" );
CLIDict_Entry( aniStack, "Displays the animation stack contents" );
CLIDict_Entry( chanTest, "Channel test. No arg - next pixel. # - pixel, r - roll-through. a - all, s - stop" );
CLIDict_Entry( pixelList, "Prints out pixel:channel mappings." );
CLIDict_Entry( pixelSCTest, "Scancode pixel test. No arg - next pixel. # - pixel, r - roll-through. a - all, s - stop" );
CLIDict_Entry( pixelTest, "Pixel test. No arg - next pixel. # - pixel, r - roll-through. a - all, s - stop, f - full" );
CLIDict_Entry( pixelXYTest, "XY pixel test. No arg - next pixel. # - pixel, r - roll-through. a - all, s - stop" );
CLIDict_Entry( rectDisp, "Show the current output of the MCU pixel buffer." );
CLIDict_Def( pixelCLIDict, "Pixel Module Commands" ) = {
CLIDict_Item( aniAdd ),
CLIDict_Item( aniDel ),
CLIDict_Item( aniStack ),
CLIDict_Item( chanTest ),
CLIDict_Item( pixelList ),
CLIDict_Item( pixelSCTest ),
CLIDict_Item( pixelTest ),
CLIDict_Item( pixelXYTest ),
CLIDict_Item( rectDisp ),
{ 0, 0, 0 } // Null entry for dictionary end
};
// Gamma correction
extern const uint8_t gamma_table[];
static uint8_t gamma_enabled;
// Debug states
PixelTest Pixel_testMode;
volatile uint16_t Pixel_testPos = 0;
// Frame State
// Indicates to pixel and output modules current state of the buffer
FrameState Pixel_FrameState;
// Animation Stack
AnimationStack Pixel_AnimationStack;
// Animation Control
AnimationControl Pixel_animationControl;
// Memory Stor for Animation Elements
// Animation elements may be called multiple times, thus memory must be allocated per instance
AnimationStackElement Pixel_AnimationElement_Stor[Pixel_AnimationStackSize];
#if defined(_host_)
uint16_t Pixel_AnimationStack_HostSize = Pixel_AnimationStackSize;
uint8_t Pixel_Buffers_HostLen = Pixel_BuffersLen_KLL;
uint8_t Pixel_MaxChannelPerPixel_Host = Pixel_MaxChannelPerPixel;
uint16_t Pixel_Mapping_HostLen = 128; // TODO Define
uint8_t Pixel_AnimationStackElement_HostSize = sizeof( AnimationStackElement );
#endif
// Pixel Fade Profile Mapping
// Assigned per pixel (rather than channel)
// 0 - Disabled
// 1 - Profile 1 - Keys
// 2 - Profile 2 - Underlighting
// 3 - Profile 3 - Indicator LEDs
// 4 - Profile 4 - Current active layer (defaultmap is excluded)
static uint8_t Pixel_pixel_fade_profile[Pixel_TotalPixels_KLL];
// Pixel Fade Profile Parameters
// TODO (HaaTa): Use KLL to determine number of profiles (currently only 4)
static PixelFadeProfile Pixel_pixel_fade_profile_entries[4];
// Latency Measurement Resource
static uint8_t pixelLatencyResource;
// ----- Function Declarations -----
uint8_t Pixel_animationProcess( AnimationStackElement *elem );
uint8_t Pixel_addAnimation( AnimationStackElement *element, CapabilityState cstate );
uint8_t Pixel_determineLastTriggerScanCode( TriggerMacro *trigger );
void Pixel_pixelSet( PixelElement *elem, uint32_t value );
void Pixel_clearAnimations();
void Pixel_SecondaryProcessing_profile_init();
PixelBuf *Pixel_bufferMap( uint16_t channel );
AnimationStackElement *Pixel_lookupAnimation( uint16_t index, uint16_t prev );
// ----- Capabilities -----
//
void Pixel_GammaControl_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_GammaControl_capability(func)");
return;
default:
return;
}
uint8_t arg = *(uint8_t*)(&args[0]);
// Interconnect broadcasting
#if defined(ConnectEnabled_define)
// By default send to the *next* node, which will determine where to go next
extern uint8_t Connect_id; // connect_scan.c
uint8_t addr = Connect_id + 1;
// Send interconnect remote capability packet
// generatedKeymap.h
extern const Capability CapabilitiesList[];
// Broadcast layerStackExact remote capability (0xFF is the broadcast id)
Connect_send_RemoteCapability(
addr,
Pixel_GammaControl_capability_index,
state,
stateType,
CapabilitiesList[ Pixel_GammaControl_capability_index ].argCount,
args
);
#endif
// Decide how to handle function
switch ( arg )
{
case 0: // Disabled
gamma_enabled = 0;
break;
case 1: // Enabled
gamma_enabled = 1;
break;
default: // Toggle
gamma_enabled = !gamma_enabled;
break;
}
}
void Pixel_AnimationIndex_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
case CapabilityState_Last:
// Mainly used on press
// Except some configurations may also use release
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_AnimationIndex_capability(settingindex)");
return;
default:
return;
}
// Interconnect broadcasting
#if defined(ConnectEnabled_define)
// By default send to the *next* node, which will determine where to go next
extern uint8_t Connect_id; // connect_scan.c
uint8_t addr = Connect_id + 1;
// Send interconnect remote capability packet
// generatedKeymap.h
extern const Capability CapabilitiesList[];
// Broadcast layerStackExact remote capability (0xFF is the broadcast id)
Connect_send_RemoteCapability(
addr,
Pixel_AnimationIndex_capability_index,
state,
stateType,
CapabilitiesList[ Pixel_AnimationIndex_capability_index ].argCount,
args
);
#endif
// Lookup animation settings
uint16_t index = *(uint16_t*)(&args[0]);
// Check if a valid setting
if ( index >= Pixel_AnimationSettingsNum_KLL )
{
warn_print("Invalid AnimationSetting index: ");
printInt16( index );
print( NL );
return;
}
AnimationStackElement element = Pixel_AnimationSettings[ index ];
element.trigger = trigger;
Pixel_addAnimation( &element, cstate );
}
// XXX (HaaTa): It's not recommended to use this capability, use AnimationIndex instead
void Pixel_Animation_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_Animation_capability(index,loops,pfunc,framedelay,frameoption,replace)");
return;
default:
return;
}
AnimationStackElement element;
element.trigger = trigger;
element.pos = 0; // TODO (HaaTa) Start at specific frame
element.subpos = 0;
element.index = *(uint16_t*)(&args[0]);
element.loops = *(uint8_t*)(&args[2]);
element.pfunc = *(uint8_t*)(&args[3]);
element.framedelay = *(uint8_t*)(&args[4]);
element.frameoption = *(uint8_t*)(&args[5]);
element.replace = *(uint8_t*)(&args[6]);
Pixel_addAnimation( &element, cstate );
}
// XXX (HaaTa): TODO
void Pixel_Pixel_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_Pixel_capability(pixel,chan,value)");
return;
default:
return;
}
/*
PixelChange change = *(PixelChange*)(&args[0]);
uint16_t channel = *(uint16_t*)(&args[1]);
uint32_t value = *(uint32_t*)(&args[3]);
*/
// TODO (HaaTa) Apply the channel modification
}
void Pixel_AnimationControl_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_AnimationControl_capability(func)");
return;
default:
return;
}
// Interconnect broadcasting
#if defined(ConnectEnabled_define)
// By default send to the *next* node, which will determine where to go next
extern uint8_t Connect_id; // connect_scan.c
uint8_t addr = Connect_id + 1;
// Send interconnect remote capability packet
// generatedKeymap.h
extern const Capability CapabilitiesList[];
// Broadcast layerStackExact remote capability (0xFF is the broadcast id)
Connect_send_RemoteCapability(
addr,
Pixel_AnimationControl_capability_index,
state,
stateType,
CapabilitiesList[ Pixel_AnimationControl_capability_index ].argCount,
args
);
#endif
uint8_t arg = *(uint8_t*)(&args[0]);
// Decide how to handle function
switch ( arg )
{
case 0: // Pause/Resume
// Determine how to handle Pause/Resume
switch ( Pixel_animationControl )
{
case AnimationControl_Forward:
case AnimationControl_ForwardOne:
Pixel_animationControl = AnimationControl_Pause;
break;
case AnimationControl_Pause:
default:
Pixel_animationControl = AnimationControl_Forward;
break;
}
break;
case 1: // Forward one frame
Pixel_animationControl = AnimationControl_ForwardOne;
break;
case 2: // Forward
Pixel_animationControl = AnimationControl_Forward;
break;
case 3: // Stop (clears all animations)
Pixel_animationControl = AnimationControl_Stop;
break;
case 4: // Reset (restarts animations)
Pixel_animationControl = AnimationControl_Reset;
break;
case 5: // Pauses animations and clears display
Pixel_animationControl = AnimationControl_WipePause;
break;
case 6: // Pauses animation
Pixel_animationControl = AnimationControl_Pause;
break;
case 7: // Clears pixels (no pause and no stop)
Pixel_animationControl = AnimationControl_Clear;
break;
}
}
void Pixel_FadeSet_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_FadeSet_capability(profile,config,period)");
return;
default:
return;
}
// Interconnect broadcasting
#if defined(ConnectEnabled_define)
// By default send to the *next* node, which will determine where to go next
extern uint8_t Connect_id; // connect_scan.c
uint8_t addr = Connect_id + 1;
// Send interconnect remote capability packet
// generatedKeymap.h
extern const Capability CapabilitiesList[];
// Broadcast layerStackExact remote capability (0xFF is the broadcast id)
Connect_send_RemoteCapability(
addr,
Pixel_FadeSet_capability_index,
state,
stateType,
CapabilitiesList[ Pixel_FadeSet_capability_index ].argCount,
args
);
#endif
// Get arguments
uint8_t profile = *(uint8_t*)(&args[0]);
uint8_t config = *(uint8_t*)(&args[1]);
uint8_t period = *(uint8_t*)(&args[2]);
// Get period configuation
const PixelPeriodConfig *period_config = &Pixel_LED_FadePeriods[period];
// Set period configuration
Pixel_pixel_fade_profile_entries[profile].conf[config].start = period_config->start;
Pixel_pixel_fade_profile_entries[profile].conf[config].end = period_config->end;
// Reset the current period being processed
Pixel_pixel_fade_profile_entries[profile].pos = 0;
Pixel_pixel_fade_profile_entries[profile].period_conf = PixelPeriodIndex_Off_to_On;
}
void Pixel_FadeLayerHighlight_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
// Get argument
uint16_t layer = *(uint16_t*)(&args[0]);
switch ( cstate )
{
case CapabilityState_Initial:
// Refresh the fade profiles
Pixel_SecondaryProcessing_profile_init();
// Scan the layer for keys
break;
case CapabilityState_Last:
// Refresh the fade profiles
Pixel_SecondaryProcessing_profile_init();
// If any layers are still active, re-run using top layer
layer = Layer_topActive();
if ( layer > 0 )
{
break;
}
return;
case CapabilityState_Debug:
// Display capability name
print("Pixel_FadeLayerHighlight_capability(layer)");
return;
default:
return;
}
// Interconnect broadcasting
#if defined(ConnectEnabled_define)
// By default send to the *next* node, which will determine where to go next
extern uint8_t Connect_id; // connect_scan.c
uint8_t addr = Connect_id + 1;
// Send interconnect remote capability packet
// generatedKeymap.h
extern const Capability CapabilitiesList[];
// Broadcast layerStackExact remote capability (0xFF is the broadcast id)
Connect_send_RemoteCapability(
addr,
Pixel_FadeLayerHighlight_capability_index,
state,
stateType,
CapabilitiesList[ Pixel_FadeLayerHighlight_capability_index ].argCount,
args
);
#endif
// Ignore if an invalid layer
if ( layer >= LayerNum )
{
return;
}
// Lookup layer
const Layer *layer_map = &LayerIndex[layer];
#if KLL_LED_FadeActiveLayerInvert_define == 1
// Default layer
const Layer *default_map = &LayerIndex[0];
// Add keys not in layer
uint8_t key = 1; // Scan Codes start at 1
for ( ; key < layer_map->first; key++ )
{
// If we've exceeded the pixel lookup, ignore
if ( key > MaxPixelToScanCode_KLL )
{
return;
}
uint8_t index = key - default_map->first;
// If the first entry in trigger list is a 0, ignore (otherwise, key is in layer)
if ( !Trigger_DetermineScanCodeOnTrigger( default_map, index ) )
{
continue;
}
// Lookup pixel associated with scancode (remember -1 as all pixels and scancodes start at 1, not 0)
uint16_t pixel = Pixel_ScanCodeToPixel[key - 1];
// If pixel is 0, ignore
if ( pixel == 0 )
{
continue;
}
// Set pixel to group #4
Pixel_pixel_fade_profile[pixel - 1] = 4;
}
// Iterate over every key in layer, skipping active keys
for ( ; key <= layer_map->last; key++ )
{
// If we've exceeded the pixel lookup, ignore
if ( key > MaxPixelToScanCode_KLL )
{
return;
}
uint8_t index = key - layer_map->first;
// If the first entry in trigger list is a 0, set as this key is not in the layer
// Ignore otherwise
if ( Trigger_DetermineScanCodeOnTrigger( layer_map, index ) )
{
continue;
}
// If the first entry in trigger list is a 0, ignore (otherwise, key is in layer)
if ( !Trigger_DetermineScanCodeOnTrigger( default_map, index ) )
{
continue;
}
// Lookup pixel associated with scancode (remember -1 as all pixels and scancodes start at 1, not 0)
uint16_t pixel = Pixel_ScanCodeToPixel[key - 1];
// If pixel is 0, ignore
if ( pixel == 0 )
{
continue;
}
// Set pixel to group #4
Pixel_pixel_fade_profile[pixel - 1] = 4;
}
// Add keys not in layer
for ( ; key <= default_map->last; key++ )
{
// If we've exceeded the pixel lookup, ignore
if ( key > MaxPixelToScanCode_KLL )
{
return;
}
uint8_t index = key - default_map->first;
// If the first entry in trigger list is a 0, ignore (otherwise, key is in layer)
if ( !Trigger_DetermineScanCodeOnTrigger( default_map, index ) )
{
continue;
}
// Lookup pixel associated with scancode (remember -1 as all pixels and scancodes start at 1, not 0)
uint16_t pixel = Pixel_ScanCodeToPixel[key - 1];
// If pixel is 0, ignore
if ( pixel == 0 )
{
continue;
}
// Set pixel to group #4
Pixel_pixel_fade_profile[pixel - 1] = 4;
}
#else
// Lookup list of keys in layer
for ( uint8_t key = layer_map->first; key <= layer_map->last; key++ )
{
uint8_t index = key - layer_map->first;
// If the first entry in trigger list is a 0, ignore (otherwise, key is in layer)
if ( !Trigger_DetermineScanCodeOnTrigger( layer_map, index ) )
{
continue;
}
// Lookup pixel associated with scancode (remember -1 as all pixels and scancodes start at 1, not 0)
uint16_t pixel = Pixel_ScanCodeToPixel[key - 1];
// If pixel is 0, ignore
if ( pixel == 0 )
{
continue;
}
// Set pixel to group #4
Pixel_pixel_fade_profile[pixel - 1] = 4;
}
#endif
}
void Pixel_FadeControl_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only activate on press event
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_FadeControl_capability(test)");
return;
default:
return;
}
// Get arguments
uint8_t profile = args[0];
uint8_t command = args[1];
uint8_t arg = args[2];
// Make sure profile is valid
if ( profile >= sizeof(Pixel_pixel_fade_profile_entries) )
{
return;
}
// Process command
uint16_t tmp;
switch ( command )
{
case PixelFadeControl_Reset:
for ( uint8_t config = 0; config < 4; config++ )
{
Pixel_pixel_fade_profile_entries[profile].conf[config] = \
Pixel_LED_FadePeriods[Pixel_LED_FadePeriod_Defaults[profile][config]];
}
Pixel_pixel_fade_profile_entries[profile].pos = 0;
Pixel_pixel_fade_profile_entries[profile].period_conf = PixelPeriodIndex_Off_to_On;
Pixel_pixel_fade_profile_entries[profile].brightness = Pixel_LED_FadeBrightness[profile];
break;
case PixelFadeControl_Reset_All:
// Setup fade defaults
for ( uint8_t pr = 0; pr < 4; pr++ )
{
for ( uint8_t config = 0; config < 4; config++ )
{
Pixel_pixel_fade_profile_entries[pr].conf[config] = \
Pixel_LED_FadePeriods[Pixel_LED_FadePeriod_Defaults[pr][config]];
}
Pixel_pixel_fade_profile_entries[pr].pos = 0;
Pixel_pixel_fade_profile_entries[pr].period_conf = PixelPeriodIndex_Off_to_On;
Pixel_pixel_fade_profile_entries[profile].brightness = Pixel_LED_FadeBrightness[pr];
}
break;
case PixelFadeControl_Brightness_Set:
// Set brightness
Pixel_pixel_fade_profile_entries[profile].brightness = arg;
break;
case PixelFadeControl_Brightness_Increment:
// Increment with no rollover
tmp = Pixel_pixel_fade_profile_entries[profile].brightness;
if ( tmp + arg > 0xFF )
{
Pixel_pixel_fade_profile_entries[profile].brightness = 0xFF;
break;
}
Pixel_pixel_fade_profile_entries[profile].brightness += arg;
break;
case PixelFadeControl_Brightness_Decrement:
// Decrement with no rollover
tmp = Pixel_pixel_fade_profile_entries[profile].brightness;
if ( tmp - arg < 0x00 )
{
Pixel_pixel_fade_profile_entries[profile].brightness = 0x00;
break;
}
Pixel_pixel_fade_profile_entries[profile].brightness -= arg;
break;
case PixelFadeControl_Brightness_Default:
Pixel_pixel_fade_profile_entries[profile].brightness = Pixel_LED_FadeBrightness[profile];
break;
default:
return;
}
}
void Pixel_LEDTest_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only activate on press event
break;
case CapabilityState_Debug:
// Display capability name
print("Pixel_LEDTest_capability(test)");
return;
default:
return;
}
// Get arguments
PixelTest test = *(PixelTest*)(&args[0]);
uint16_t index = *(uint16_t*)(&args[1]);
// If index is not set to 0xFFFF, make sure to update the test position
if ( index != 0xFFFF )
{
Pixel_testPos = index;
}
// Set the test mode
Pixel_testMode = test;
}
// ----- Functions -----
// -- Debug Functions --
// Debug info for PixelElement
void Pixel_showPixelElement( PixelElement *elem )
{
print("W:");
printInt8( elem->width );
print(" C:");
printInt8( elem->channels );
print(" I:");
printInt16( elem->indices[0] );
for ( uint8_t c = 1; c < elem->channels; c++ )
{
print(",");
printInt16( elem->indices[c] );
}
}
// -- Utility Functions --
// TODO Support non-8bit channels
uint8_t Pixel_8bitInterpolation( uint8_t start, uint8_t end, uint8_t dist )
{
return (start * (256 - dist) + end * dist) >> 8;
}
void Pixel_pixelInterpolate( PixelElement *elem, uint8_t position, uint8_t intensity )
{
// Toggle each of the channels of the pixel
for ( uint8_t ch = 0; ch < elem->channels; ch++ )
{
uint16_t ch_pos = elem->indices[ch];
PixelBuf *pixbuf = Pixel_bufferMap( ch_pos );
PixelBuf16( pixbuf, ch_pos ) = Pixel_8bitInterpolation( 0, intensity, position * (ch + 1) );
}
}
// -- Animation Stack --
// Locates animation memory slot using default settings for the animation
// Initiates animation to process on the next cycle
// Returns 1 on success, 0 on failure to allocate
uint8_t Pixel_addDefaultAnimation( uint32_t index )
{
if ( index >= Pixel_AnimationSettingsNum_KLL )
{
warn_print("Invalid AnimationSetting index: ");
printInt32( index );
print( NL );
return 0;
}
return Pixel_addAnimation( (AnimationStackElement*)&Pixel_AnimationSettings[ index ], CapabilityState_None );
}
// Allocates animaton memory slot
// Initiates animation to process on the next cycle
// Returns 1 on success, 0 on failure to allocate
uint8_t Pixel_addAnimation( AnimationStackElement *element, CapabilityState cstate )
{
AnimationStackElement *found;
switch ( element->replace )
{
case AnimationReplaceType_Basic:
case AnimationReplaceType_All:
found = Pixel_lookupAnimation( element->index, 0 );
// If found, modify stack element
if ( found != NULL && ( found->trigger == element->trigger || element->replace == AnimationReplaceType_All ) )
{
found->pos = element->pos;
found->subpos = element->subpos;
found->loops = element->loops;
found->pfunc = element->pfunc;
found->ffunc = element->ffunc;
found->framedelay = element->framedelay;
found->frameoption = element->frameoption;
found->replace = element->replace;
found->state = element->state;
return 0;
}
break;
// Replace on press and release
// Press starts the animation
// Release stops the animation
case AnimationReplaceType_State:
found = Pixel_lookupAnimation( element->index, 0 );
switch ( cstate )
{
// Press
case CapabilityState_Initial:
// If found, modify stack element
if ( found && found->trigger == element->trigger )
{
found->pos = element->pos;
found->subpos = element->subpos;
found->loops = element->loops;
found->pfunc = element->pfunc;
found->ffunc = element->ffunc;
found->framedelay = element->framedelay;
found->frameoption = element->frameoption;
found->replace = element->replace;
found->state = element->state;
return 0;
}
break;
// Release
case CapabilityState_Last:
// Only need to do something if the animation was found (which is stop)
if ( found && found->trigger == element->trigger )
{
found->state = AnimationPlayState_Stop;
}
return 0;
default:
break;
}
break;
// Clear all current animations from stack before adding new animation
case AnimationReplaceType_Clear:
Pixel_clearAnimations();
break;
// Clear all current animations from stack before adding new animation
// Unless it's paused, and if it's paused do a replace if necessary
case AnimationReplaceType_ClearActive:
found = Pixel_lookupAnimation( element->index, 0 );
// If found, modify stack element
if ( found )
{
found->pos = element->pos;
found->subpos = element->subpos;
found->loops = element->loops;
found->pfunc = element->pfunc;
found->ffunc = element->ffunc;
found->framedelay = element->framedelay;
found->frameoption = element->frameoption;
found->replace = element->replace;
found->state = element->state;
return 0;
}
// Iterate through stack, stopping animations that are not paused
// and ignoring the found animation