-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathmacro.c
1911 lines (1612 loc) · 45.1 KB
/
macro.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) 2014-2020 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 <cli.h>
#include <latency.h>
#include <led.h>
#include <print.h>
#include <scan_loop.h>
// Keymaps
#include <usb_hid.h> // Generated using kll (and hid-io/layouts) at compile time, in build directory
#include <generatedKeymap.h> // Generated using kll at compile time, in build directory
// Connect Includes
#if defined(ConnectEnabled_define)
#include <connect_scan.h>
#endif
// PixelMap Includes
#if defined(Pixel_MapEnabled_define)
#include <pixel.h>
#endif
// Local Includes
#include "layer.h"
#include "trigger.h"
#include "result.h"
#include "macro.h"
// ----- Function Declarations -----
void cliFunc_capDebug ( char* args );
void cliFunc_capList ( char* args );
void cliFunc_capSelect ( char* args );
void cliFunc_keyHold ( char* args );
void cliFunc_keyPress ( char* args );
void cliFunc_keyRelease( char* args );
void cliFunc_layerDebug( char* args );
void cliFunc_layerList ( char* args );
void cliFunc_layerState( char* args );
void cliFunc_macroDebug( char* args );
void cliFunc_macroList ( char* args );
void cliFunc_macroProc ( char* args );
void cliFunc_macroShow ( char* args );
void cliFunc_macroStep ( char* args );
void cliFunc_posList ( char* args );
void cliFunc_voteDebug ( char* args );
void Macro_showScheduleType( ScheduleState state );
void Macro_showTriggerType( TriggerType type );
// ----- Variables -----
// Macro Module command dictionary
CLIDict_Entry( capDebug, "Prints capability debug message before calling, with trigger conditions." );
CLIDict_Entry( capList, "Prints an indexed list of all non USB keycode capabilities." );
CLIDict_Entry( capSelect, "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B" );
CLIDict_Entry( keyHold, "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
CLIDict_Entry( keyPress, "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
CLIDict_Entry( keyRelease, "Send key-release event to macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
CLIDict_Entry( layerDebug, "Layer debug mode. Shows layer stack and any changes." );
CLIDict_Entry( layerList, "List available layers." );
CLIDict_Entry( layerState, "Modify specified indexed layer state <layer> <state byte>." NL "\t\t\033[35mL2\033[0m Indexed Layer 0x02" NL "\t\t0 Off, 1 Shift, 2 Latch, 4 Lock States" );
CLIDict_Entry( macroDebug, "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes." );
CLIDict_Entry( macroList, "List the defined trigger and result macros." );
CLIDict_Entry( macroProc, "Pause/Resume macro processing." );
CLIDict_Entry( macroShow, "Show the macro corresponding to the given index." NL "\t\t\033[35mT16\033[0m Indexed Trigger Macro 0x10, \033[35mR12\033[0m Indexed Result Macro 0x0C" );
CLIDict_Entry( macroStep, "Do N macro processing steps. Defaults to 1." );
CLIDict_Entry( posList, "List physical key positions by ScanCode." );
CLIDict_Entry( voteDebug, "Show results of TriggerEvent voting." );
CLIDict_Def( macroCLIDict, "Macro Module Commands" ) = {
CLIDict_Item( capDebug ),
CLIDict_Item( capList ),
CLIDict_Item( capSelect ),
CLIDict_Item( keyHold ),
CLIDict_Item( keyPress ),
CLIDict_Item( keyRelease ),
CLIDict_Item( layerDebug ),
CLIDict_Item( layerList ),
CLIDict_Item( layerState ),
CLIDict_Item( macroDebug ),
CLIDict_Item( macroList ),
CLIDict_Item( macroProc ),
CLIDict_Item( macroShow ),
CLIDict_Item( macroStep ),
CLIDict_Item( posList ),
CLIDict_Item( voteDebug ),
{ 0, 0, 0 } // Null entry for dictionary end
};
// Capability debug flag - If set, shows the name of the capability when before it is called
extern uint8_t capDebugMode;
// Layer debug flag - If set, displays any changes to layers and the full layer stack on change
extern uint8_t layerDebugMode;
// Macro debug flag - If set, clears the USB Buffers after signalling processing completion
// 1 - Disable USB output, show debug
// 2 - Enabled USB output, show debug
// 3 - Disable USB output
uint8_t macroDebugMode;
// Vote debug flag - If set show the result of each
uint8_t voteDebugMode;
// Trigger pending debug flag - If set show pending triggers before evaluating
uint8_t triggerPendingDebugMode;
// Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
uint8_t macroPauseMode;
// Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
uint16_t macroStepCounter;
// Macro rotation store - Each store is indexed, and is initialized to 0
static uint8_t Macro_rotation_store[256]; // TODO (HaaTa): Use KLL to determine max usage (or dynamically size)
// Latency resource
static uint8_t macroLatencyResource;
// Incoming Trigger Event Buffer
TriggerEvent macroTriggerEventBuffer[ MaxScanCode_KLL + 1 ];
var_uint_t macroTriggerEventBufferSize;
extern ResultsPending macroResultMacroPendingList;
extern index_uint_t macroTriggerMacroPendingList[];
extern index_uint_t macroTriggerMacroPendingListSize;
// Interconnect ScanCode Cache
#if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
// TODO This can be shrunk by the size of the max node 0 ScanCode
TriggerEvent macroInterconnectCache[ MaxScanCode_KLL + 1 ];
uint8_t macroInterconnectCacheSize = 0;
#endif
// Dynamically Sized Type Widths
#if defined(_host_)
const uint8_t StateWordSize = StateWordSize_define;
const uint8_t IndexWordSize = IndexWordSize_define;
const uint8_t ScheduleStateSize = ScheduleStateSize_define;
#endif
// ----- Capabilities -----
// Rotation capability
// Maintains state and fires of a rotation event trigger
void Macro_rotate_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 on press
break;
case CapabilityState_Debug:
// Display capability name
print("Macro_rotate()");
return;
default:
return;
}
// Get storage index from arguments
uint8_t index = args[0];
// Get increment from arguments
int8_t increment = (int8_t)args[1];
// Trigger rotation
Macro_rotationState( index, increment );
}
// No-op capability (None)
void Macro_none_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Debug:
// Display capability name
print("Macro_none()");
return;
default:
break;
}
}
// Test Thread-safe Capability
// Capability used to test a thread-safe result
void Macro_testThreadSafe_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Debug:
// Display capability name
print("Macro_testThreadSafe()");
return;
default:
break;
}
// Show trigger information
print("ThreadSafe: ");
Macro_showTriggerType( (TriggerType)stateType );
print(" ");
Macro_showScheduleType( (ScheduleState)state );
print(" - ");
printHex32( (intptr_t)trigger );
print(NL);
}
// Test Thread-unsafe Capability
// Capability used to test a thread-unsafe result
void Macro_testThreadUnsafe_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Debug:
// Display capability name
print("Macro_testThreadUnsafe()");
return;
default:
break;
}
// Show trigger information
print("ThreadUnsafe: ");
Macro_showTriggerType( (TriggerType)stateType );
print(" ");
Macro_showScheduleType( (ScheduleState)state );
print(" - ");
printHex32( (intptr_t)trigger );
print(NL);
}
// ----- Debug Functions -----
// Shows a ScheduleType
void Macro_showScheduleType( ScheduleState state )
{
// State types
switch ( state & 0x0F )
{
case ScheduleType_P:
//case ScheduleType_A:
print("\033[1;33mP\033[0m");
break;
case ScheduleType_H:
//case ScheduleType_On:
print("\033[1;32mH\033[0m");
break;
case ScheduleType_R:
//case ScheduleType_D:
print("\033[1;35mR\033[0m");
break;
case ScheduleType_O:
//case ScheduleType_Off:
print("\033[1mO\033[0m");
break;
case ScheduleType_UP:
print("UP");
break;
case ScheduleType_UR:
print("UR");
break;
case ScheduleType_Done:
print("Done");
break;
case ScheduleType_Repeat:
print("Repeat");
break;
case ScheduleType_Debug:
print("Debug");
break;
default:
print("\033[1;31mINVALID\033[0m");
printInt8( state & 0x0F );
break;
}
// Check for Shift/Latch/Lock type
switch ( state & 0xF0 )
{
case ScheduleType_Shift:
print("Sh");
break;
case ScheduleType_Latch:
print("La");
break;
case ScheduleType_Lock:
print("Lo");
break;
default:
break;
}
}
// Shows a Schedule
void Macro_showSchedule( Schedule *param, uint8_t analog )
{
// Analog
if ( analog )
{
printInt8( param->analog );
}
// Everything else
else
{
Macro_showScheduleType( param->state );
}
// Time
print(":");
printInt32( param->time.ms );
print(".");
printInt32( param->time.ticks );
}
// Shows a TriggerType
void Macro_showTriggerType( TriggerType type )
{
// Type
switch ( type )
{
// Switches
case TriggerType_Switch1:
case TriggerType_Switch2:
case TriggerType_Switch3:
case TriggerType_Switch4:
print("Sw");
break;
// LEDs
case TriggerType_LED1:
print("LED");
break;
// Analog
case TriggerType_Analog1:
case TriggerType_Analog2:
case TriggerType_Analog3:
case TriggerType_Analog4:
print("An");
break;
// Layer
case TriggerType_Layer1:
case TriggerType_Layer2:
case TriggerType_Layer3:
case TriggerType_Layer4:
print("Layer");
break;
// Animation
case TriggerType_Animation1:
case TriggerType_Animation2:
case TriggerType_Animation3:
case TriggerType_Animation4:
print("Animation");
break;
// Sleep
case TriggerType_Sleep1:
print("Sleep");
break;
// Resume
case TriggerType_Resume1:
print("Resume");
break;
// Inactive
case TriggerType_Inactive1:
print("Inactive");
break;
// Active
case TriggerType_Active1:
print("Active");
break;
// Rotation
case TriggerType_Rotation1:
print("Rotation");
break;
// Dial
case TriggerType_Dial1:
print("Dial");
break;
// Invalid
default:
print("INVALID");
break;
// Debug
case TriggerType_Debug:
print("Debug");
break;
}
}
// Shows a TriggerEvent
void Macro_showTriggerEvent( TriggerEvent *event )
{
// Decode type
Macro_showTriggerType( event->type );
print(" ");
// Show state
switch ( event->type )
{
case TriggerType_Analog1:
case TriggerType_Analog2:
case TriggerType_Analog3:
case TriggerType_Analog4:
case TriggerType_Rotation1:
case TriggerType_Dial1:
printInt8( event->state );
break;
default:
Macro_showScheduleType( event->state );
break;
}
print(" ");
// Show index number
printInt8( event->type );
print(":");
printInt8( event->index );
}
// Shows a TriggerGuide
void Macro_showTriggerGuide( TriggerGuide *guide )
{
}
// ----- Functions -----
#if defined(_host_)
// Callback for host-side kll
extern int Output_callback( char* command, char* args );
const uint32_t LayerNum_host = LayerNum;
#endif
// Add an interconnect ScanCode
// These are handled differently (less information is sent, hold/off states must be assumed)
// Returns 1 if added, 0 if the ScanCode is already in the buffer
// Returns 2 if there's an error
#if defined(ConnectEnabled_define) || defined(PressReleaseCache_define)
uint8_t Macro_pressReleaseAdd( void *trigger_ptr )
{
TriggerEvent *trigger = (TriggerEvent*)trigger_ptr;
// Error checking
uint8_t error = 0;
switch ( trigger->type )
{
case TriggerType_Switch1:
case TriggerType_Switch2:
case TriggerType_Switch3:
case TriggerType_Switch4:
case TriggerType_LED1:
switch ( trigger->state )
{
case ScheduleType_P:
case ScheduleType_H:
case ScheduleType_R:
case ScheduleType_O:
break;
default:
erro_print("Invalid key state - ");
error = 1;
break;
}
break;
case TriggerType_Analog1:
case TriggerType_Analog2:
case TriggerType_Analog3:
case TriggerType_Analog4:
case TriggerType_Rotation1:
case TriggerType_Dial1:
break;
case TriggerType_Animation1:
case TriggerType_Animation2:
case TriggerType_Animation3:
case TriggerType_Animation4:
case TriggerType_Sleep1:
case TriggerType_Resume1:
case TriggerType_Active1:
case TriggerType_Inactive1:
break;
// Invalid TriggerGuide type for Interconnect
default:
erro_print("Invalid type - ");
error = 1;
break;
}
// Check if ScanCode is out of range
if ( trigger->index > MaxScanCode_KLL )
{
warn_print("ScanCode is out of range/not defined - ");
error = 1;
}
// Display TriggerGuide
if ( error )
{
printHex( trigger->type );
print(" ");
printHex( trigger->state );
print(" ");
printHex( trigger->index );
print( NL );
return 2;
}
// Add trigger to the Interconnect Cache
// During each processing loop, a scancode may be re-added depending on it's state
for ( var_uint_t c = 0; c < macroInterconnectCacheSize; c++ )
{
// Check if the same ScanCode
if ( macroInterconnectCache[ c ].index == trigger->index )
{
// Update the state
macroInterconnectCache[ c ].state = trigger->state;
return 0;
}
}
// If not in the list, add it
macroInterconnectCache[ macroInterconnectCacheSize++ ] = *trigger;
return 1;
}
#endif
// Update the scancode key state
// States:
// * 0x00 - Off
// * 0x01 - Pressed
// * 0x02 - Held
// * 0x03 - Released
// * 0x04 - Unpressed (this is currently ignored)
void Macro_keyState( uint16_t scanCode, uint8_t state )
{
#if defined(ConnectEnabled_define)
// Only compile in if a Connect node module is available
if ( !Connect_master )
{
// ScanCodes are only added if there was a state change (on/off)
switch ( state )
{
case ScheduleType_O: // Off
case ScheduleType_H: // Held
return;
}
}
#endif
// Lookup done based on size of scanCode
uint8_t index = 0;
TriggerType type = TriggerType_Switch1;
// Only add to macro trigger list if one of three states
switch ( state )
{
case ScheduleType_P: // Pressed
case ScheduleType_H: // Held
case ScheduleType_R: // Released
// Check if ScanCode is out of range
if ( scanCode > MaxScanCode_KLL )
{
warn_print("ScanCode is out of range/not defined: ");
printInt16( scanCode );
print( NL );
return;
}
// Determine which type
if ( scanCode < 256 )
{
index = scanCode;
}
else if ( scanCode < 512 )
{
index = scanCode - 256;
type = TriggerType_Switch2;
}
else if ( scanCode < 768 )
{
index = scanCode - 512;
type = TriggerType_Switch3;
}
else if ( scanCode < 1024 )
{
index = scanCode - 768;
type = TriggerType_Switch4;
}
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type;
macroTriggerEventBufferSize++;
break;
}
}
// Update the scancode analog state
// States:
// * 0x00 - Off
// * 0x01 - Released
// * 0x02-0xFF - Analog value (low to high)
void Macro_analogState( uint16_t scanCode, uint8_t state )
{
// Only add to macro trigger list if non-off
if ( state == 0x00 )
return;
// Lookup done based on size of scanCode
uint8_t index = 0;
TriggerType type = TriggerType_Analog1;
// Determine which type
if ( scanCode < 256 )
{
index = scanCode;
}
else if ( scanCode < 512 )
{
index = scanCode - 256;
type = TriggerType_Analog2;
}
else if ( scanCode < 768 )
{
index = scanCode - 512;
type = TriggerType_Analog3;
}
else if ( scanCode < 1024 )
{
index = scanCode - 768;
type = TriggerType_Analog4;
}
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type;
macroTriggerEventBufferSize++;
}
// Update led state
// States:
// * 0x00 - Off
// * 0x01 - Activate
// * 0x02 - On
// * 0x03 - Deactivate
void Macro_ledState( uint16_t ledCode, uint8_t state )
{
// Lookup done based on size of scanCode
uint8_t index = ledCode;
TriggerType type = TriggerType_LED1;
// Only add to macro trigger list if one of three states
switch ( state )
{
case ScheduleType_A: // Activate
case ScheduleType_On: // On
case ScheduleType_D: // Deactivate
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type;
macroTriggerEventBufferSize++;
break;
}
}
// Update animation state
// States:
// * 0x00 - Off
// * 0x06 - Done
// * 0x07 - Repeat
void Macro_animationState( uint16_t animationIndex, uint8_t state )
{
// Lookup done based on size of layerIndex
uint8_t index = 0;
TriggerType type = TriggerType_Animation1;
// Only add to macro trigger list if one of three states
switch ( state )
{
case ScheduleType_Done: // Activate
case ScheduleType_Repeat: // On
// Check if animation index is out of range
if ( animationIndex > AnimationNum_KLL )
{
warn_print("AnimationIndex is out of range/not defined: ");
printInt16( animationIndex );
print( NL );
return;
}
// Determine which type
if ( animationIndex < 256 )
{
index = animationIndex;
}
else if ( animationIndex < 512 )
{
index = animationIndex - 256;
type = TriggerType_Animation2;
}
else if ( animationIndex < 768 )
{
index = animationIndex - 512;
type = TriggerType_Animation3;
}
else if ( animationIndex < 1024 )
{
index = animationIndex - 768;
type = TriggerType_Animation4;
}
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type;
macroTriggerEventBufferSize++;
break;
}
}
// Update layer state
// States:
// * 0x00 - Off
// * 0x01 - Activate
// * 0x02 - On
// * 0x03 - Deactivate
void Macro_layerState( uint16_t layerIndex, uint8_t state )
{
// Lookup done based on size of layerIndex
uint8_t index = 0;
TriggerType type = TriggerType_Layer1;
// Only add to macro trigger list if one of three states
// Mask around Shift/Latch/Lock state
switch ( state & ScheduleType_D )
{
case ScheduleType_A: // Activate
case ScheduleType_On: // On
case ScheduleType_D: // Deactivate
// Check if layer is out of range
if ( layerIndex > LayerNum_KLL )
{
warn_print("LayerIndex is out of range/not defined: ");
printInt16( layerIndex );
print( NL );
return;
}
// Determine which type
if ( layerIndex < 256 )
{
index = layerIndex;
}
else if ( layerIndex < 512 )
{
index = layerIndex - 256;
type = TriggerType_Layer2;
}
else if ( layerIndex < 768 )
{
index = layerIndex - 512;
type = TriggerType_Layer3;
}
else if ( layerIndex < 1024 )
{
index = layerIndex - 768;
type = TriggerType_Layer4;
}
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type;
macroTriggerEventBufferSize++;
break;
}
}
// Update Time State trigger
// Only valid with Sleep/Resume and Inactive/Active triggers
// States:
// * 0x00 - Off
// * 0x01 - Activate
// * 0x02 - On
// * 0x03 - Deactivate
void Macro_timeState( uint8_t type, uint16_t cur_time, uint8_t state )
{
// Make sure this is a valid trigger type
switch ( type )
{
case TriggerType_Sleep1:
case TriggerType_Resume1:
case TriggerType_Inactive1:
case TriggerType_Active1:
break;
// Ignore if not the correct type
default:
warn_print("Invalid time state trigger update: ");
printHex( type );
print(NL);
return;
}
// cur_time is controlled by the caller
// When this function called the trigger is active
if ( cur_time > 0xFF )
{
warn_print("Only 255 time instances are accepted for a time state trigger: ");
printInt16( cur_time );
print(NL);
return;
}
uint8_t index = cur_time;
// Only add to macro trigger list if one of three states
switch ( state )
{
case ScheduleType_A: // Activate
case ScheduleType_On: // On
case ScheduleType_D: // Deactivate
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type;
macroTriggerEventBufferSize++;
break;
}
}
// Rotation state update
// Queues up a rotation trigger event
// States:
// * 0x00 - Off
// * 0x01 - Activate
// * 0x02 - On
// * 0x03 - Deactivate
void Macro_rotationState( uint8_t index, int8_t increment )
{
// For now, always Rotation1
uint8_t type = TriggerType_Rotation1;
// If index is invalid, ignore
if ( index > RotationNum )
{
return;
}
// State is used as the increment position
int16_t position = Macro_rotation_store[index] + increment;
// If first starting, the first rotation is 0
if ( Macro_rotation_store[index] == 255 )
{
position = 0;
}
// Wrap-around
// May have to wrap-around multiple times
while ( position > Rotation_MaxParameter[index] )
{
position -= Rotation_MaxParameter[index] + 1;
}
// Reverse Wrap-around
if ( position < 0 )
{
// May have to wrap-around multiple times
while ( position * -1 > Rotation_MaxParameter[index] )
{
position += Rotation_MaxParameter[index] - 1;
}
// Do wrap-around
position += Rotation_MaxParameter[index] - 1;
}
Macro_rotation_store[index] = position;
// Queue event
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = position;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type;
macroTriggerEventBufferSize++;
}
// Dial state update
// Queues up a dial trigger event
// States:
// * 0x00 - Off
// * 0x01 - Increase
// * 0x02 - Decrease
void Macro_dialState( uint8_t index, uint8_t state )
{
uint8_t type = TriggerType_Dial1;
// Queue event
switch ( state )
{
case ScheduleType_Inc: // Increment
case ScheduleType_Dec: // Decrement
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state;
macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type;
macroTriggerEventBufferSize++;
break;
}
}
// [In]Activity detected, do TickStore update and signal generation
// Returns 1 if a signal is sent
uint8_t Macro_tick_update( TickStore *store, uint8_t type )
{
uint32_t ticks = Time_tick_update( store );
uint8_t signal_sent = 0;
// Check for a fresh store (separate signal)
if ( store->fresh_store )
{
Time_tick_reset( store );
// Unset fresh store
store->fresh_store = 0;