-
Notifications
You must be signed in to change notification settings - Fork 271
/
trigger.c
999 lines (850 loc) · 27.2 KB
/
trigger.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
/* 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 <led.h>
#include <print.h>
// Local Includes
#include "trigger.h"
#include "layer.h"
#include "kll.h"
// ----- Enums -----
// Bit positions are important, passes (correct key) always trump incorrect key votes
typedef enum TriggerMacroVote {
TriggerMacroVote_Release = 0x10, // Correct key
TriggerMacroVote_PassRelease = 0x18, // Correct key (both pass and release)
TriggerMacroVote_Pass = 0x8, // Correct key
TriggerMacroVote_DoNothingRelease = 0x4, // Incorrect key
TriggerMacroVote_DoNothing = 0x2, // Incorrect key
TriggerMacroVote_Fail = 0x1, // Incorrect key
TriggerMacroVote_Invalid = 0x0, // Invalid state
} TriggerMacroVote;
typedef enum TriggerMacroEval {
TriggerMacroEval_DoNothing,
TriggerMacroEval_DoResult,
TriggerMacroEval_DoResultAndRemove,
TriggerMacroEval_Remove,
} TriggerMacroEval;
// ----- Generated KLL Variables -----
extern const Capability CapabilitiesList[];
extern const TriggerMacro TriggerMacroList[];
extern TriggerMacroRecord TriggerMacroRecordList[];
extern const ResultMacro ResultMacroList[];
// ----- Variables -----
// Incoming Trigger Event Buffer
extern TriggerEvent macroTriggerEventBuffer[];
extern var_uint_t macroTriggerEventBufferSize;
extern var_uint_t macroTriggerEventLayerCache[];
// Debug Variables
extern uint8_t triggerPendingDebugMode;
extern uint8_t voteDebugMode;
// Pending Trigger Macro Index List
// * Any trigger macros that need processing from a previous macro processing loop
// TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
// Possibly could be calculated by the KLL compiler
// XXX It may be possible to calculate the worst case using the KLL compiler
#if TriggerMacroNum == 0
#undef TriggerMacroNum
#define TriggerMacroNum 1
#endif
index_uint_t macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
index_uint_t macroTriggerMacroPendingListSize = 0;
// ----- Protected Macro Functions -----
extern void Result_appendResultMacroToPendingList( const TriggerMacro *triggerMacro );
// ----- Functions -----
// -- Debug --
// Show TriggerMacroVote
void Trigger_showTriggerMacroVote( TriggerMacroVote vote, uint8_t long_trigger_macro )
{
const char *result = "";
// Long Macro
if ( long_trigger_macro )
{
print("l");
}
// Short Macro
else
{
print("s");
}
// Static voting
switch ( vote )
{
case TriggerMacroVote_Invalid:
result = "V:I";
break;
case TriggerMacroVote_Release:
result = "V:R";
break;
case TriggerMacroVote_Pass:
result = "V:P";
break;
case TriggerMacroVote_PassRelease:
result = "V:PR";
break;
default:
print("V:");
if ( vote & TriggerMacroVote_Fail )
{
print("F");
}
if ( vote & TriggerMacroVote_DoNothingRelease )
{
print("NR");
}
else if ( vote & TriggerMacroVote_DoNothing )
{
print("N");
}
return;
}
print( result );
}
// -- General --
// Determine if long ResultMacro (more than 1 sequence element)
uint8_t Trigger_isLongResultMacro( const ResultMacro *macro )
{
// Check the second sequence combo length
// If non-zero return non-zero (long sequence)
// 0 otherwise (short sequence)
var_uint_t position = 1;
for ( var_uint_t result = 0; result < macro->guide[0]; result++ )
position += ResultGuideSize( (ResultGuide*)¯o->guide[ position ] );
return macro->guide[ position ];
}
// Determine if long TriggerMacro (more than 1 sequence element)
uint8_t Trigger_isLongTriggerMacro( const TriggerMacro *macro )
{
// Check the second sequence combo length
// If non-zero return non-zero (long sequence)
// 0 otherwise (short sequence)
return macro->guide[ macro->guide[0] * TriggerGuideSize + 1 ];
}
// Handle short trigger PHRO/AODO state transitions
TriggerMacroVote Trigger_evalShortTriggerMacroVote_PHRO( ScheduleState state )
{
switch ( state & 0x0F )
{
// Correct key, pressed, possible passing
case ScheduleType_P:
return TriggerMacroVote_Pass;
// Correct key, held, possible passing or release
case ScheduleType_H:
return TriggerMacroVote_PassRelease;
// Correct key, released, possible release
case ScheduleType_R:
return TriggerMacroVote_Release;
// Invalid state, fail
default:
return TriggerMacroVote_Fail;
}
}
// Handle short trigger DRO state transitions
TriggerMacroVote Trigger_evalShortTriggerMacroVote_DRO( ScheduleState state )
{
switch ( state )
{
// Correct event, possible passing
case ScheduleType_Done:
case ScheduleType_Repeat:
return TriggerMacroVote_Pass;
// Invalid state, fail
default:
return TriggerMacroVote_Fail;
}
}
// Votes on the given key vs. guide, short macros
TriggerMacroVote Trigger_evalShortTriggerMacroVote( TriggerEvent *event, TriggerGuide *guide, TriggerMacroVote *cur_vote )
{
// Lookup full index
var_uint_t guide_index = KLL_TriggerIndex_loopkup( guide->type, guide->scanCode );
var_uint_t event_index = KLL_TriggerIndex_loopkup( event->type, event->index );
// Return value
TriggerMacroVote vote = TriggerMacroVote_Invalid;
// Depending on key type
switch ( guide->type )
{
// Normal State Type
case TriggerType_Switch1:
case TriggerType_Switch2:
case TriggerType_Switch3:
case TriggerType_Switch4:
// LED State Type
case TriggerType_LED1:
// Layer State Type
case TriggerType_Layer1:
case TriggerType_Layer2:
case TriggerType_Layer3:
case TriggerType_Layer4:
// Activity State Types
case TriggerType_Sleep1:
case TriggerType_Resume1:
case TriggerType_Inactive1:
case TriggerType_Active1:
// For short TriggerMacros completely ignore incorrect keys
// Only monitor 0x70 bits if set in the guide, otherwise ensure they are 0x00
// Used for Layer state information
if (
guide_index == event_index &&
guide->type == event->type &&
(
(guide->state & 0x70) == (event->state & 0x70) ||
(guide->state & 0x70) == 0x00
)
)
{
// If this trigger is generic, we can just vote based on the incoming state
if ( guide->state & ScheduleType_Gen )
{
vote = Trigger_evalShortTriggerMacroVote_PHRO( event->state );
break;
}
// TODO (HaaTa) Implement state scheduling
erro_printNL("State Scheduling not implemented yet...");
}
vote = TriggerMacroVote_DoNothing;
break;
/*
// LED State Type
case TriggerType_LED1:
// XXX (HaaTa) This is an initial version of State Scheduling
// For any state match that is not ScheduleType_A, set to ScheduleType_A
// as this will indicate a pulse to the capability.
if (
guide_index == event_index &&
guide->type == event->type
)
{
// When state scheduling is specified
// TODO (HaaTa); We should probably move to another state type for "auto" schedule types
if ( guide->state == event->state && guide->state != ScheduleType_A )
{
return Trigger_evalShortTriggerMacroVote_PHRO( ScheduleType_A );
}
//return Trigger_evalShortTriggerMacroVote_PHRO( event->state );
}
return TriggerMacroVote_DoNothing;
*/
// Analog State Type
case TriggerType_Analog1:
case TriggerType_Analog2:
case TriggerType_Analog3:
case TriggerType_Analog4:
erro_printNL("Analog State Type - Not implemented...");
break;
// Animation State Type
case TriggerType_Animation1:
case TriggerType_Animation2:
case TriggerType_Animation3:
case TriggerType_Animation4:
// For short TriggerMacros completely ignore incorrect triggers
if (
guide_index == event_index &&
guide->type == event->type &&
guide->state == event->state
)
{
vote = Trigger_evalShortTriggerMacroVote_DRO( event->state );
break;
}
vote = TriggerMacroVote_DoNothing;
break;
// Rotation State Type
case TriggerType_Rotation1:
// Rotation triggers use state as the index, rather than encoding a type of action
// There is only "activated" state for rotations, which is only sent once
// This makes rotations not so useful for long macros
// (though it may be possible to implement it if there is demand)
if (
guide_index == event_index &&
guide->type == event->type &&
guide->state == event->state // <== This is the rotation position
)
{
// Only ever "Pressed", other states are not used with rotations
vote = Trigger_evalShortTriggerMacroVote_PHRO( ScheduleType_P );
break;
}
vote = TriggerMacroVote_DoNothing;
break;
// Dial State Type
case TriggerType_Dial1:
// Dial triggers encode the direction of the event
// There is only "activated" state for dials, which is only sent once
// This makes dials not so useful for long macros
// (though it may be possible to implement it if there is demand)
if (
guide_index == event_index &&
guide->type == event->type &&
guide->state == event->state
)
{
// Only ever "Pressed", other states are not used with rotations
vote = Trigger_evalShortTriggerMacroVote_PHRO( ScheduleType_P );
break;
}
vote = TriggerMacroVote_DoNothing;
break;
// Invalid State Type
default:
erro_printNL("Invalid State Type. This is a bug.");
break;
}
// If this is a combo macro, make a preference for TriggerMacroVote_Pass instead of TriggerMacroVote_PassRelease
if ( *cur_vote != TriggerMacroVote_Invalid && event_index == guide_index )
{
// Make sure the votes are different and one of them are Pass
if ( *cur_vote != vote
&& ( *cur_vote == TriggerMacroVote_Pass || vote == TriggerMacroVote_Pass )
&& ( *cur_vote == TriggerMacroVote_PassRelease || vote == TriggerMacroVote_PassRelease )
)
{
*cur_vote = TriggerMacroVote_Pass;
vote = TriggerMacroVote_Pass;
}
}
return vote;
}
// Handle long trigger PHRO/AODO state transitions
TriggerMacroVote Trigger_evalLongTriggerMacroVote_PHRO( ScheduleState state, uint8_t correct )
{
// Correct scancode match
if ( correct )
{
switch ( state )
{
// Correct key, pressed, possible passing
case ScheduleType_P:
return TriggerMacroVote_Pass;
// Correct key, held, possible passing or release
case ScheduleType_H:
return TriggerMacroVote_PassRelease;
// Correct key, released, possible release
case ScheduleType_R:
return TriggerMacroVote_Release;
// Invalid state, fail
default:
return TriggerMacroVote_Fail;
}
}
// Incorrect scancode match
else
{
switch ( state )
{
// Wrong key, pressed, fail
case ScheduleType_P:
return TriggerMacroVote_Fail;
// Wrong key, held, do not pass (no effect)
case ScheduleType_H:
return TriggerMacroVote_DoNothing;
// Wrong key released, fail out if pos == 0
case ScheduleType_R:
return TriggerMacroVote_DoNothing | TriggerMacroVote_DoNothingRelease;
// Invalid state, fail
default:
return TriggerMacroVote_Fail;
}
}
}
// Handle long trigger DRO state transitions
TriggerMacroVote Trigger_evalLongTriggerMacroVote_DRO( ScheduleState state, uint8_t correct )
{
// Correct match
if ( correct )
{
switch ( state )
{
// Correct event, possible passing
case ScheduleType_Done:
case ScheduleType_Repeat:
return TriggerMacroVote_Pass;
// Invalid state, fail
default:
return TriggerMacroVote_Fail;
}
}
// Incorrect match
else
{
return TriggerMacroVote_Fail;
}
}
// Votes on the given key vs. guide, long macros
// A long macro is defined as a guide with more than 1 combo
TriggerMacroVote Trigger_evalLongTriggerMacroVote( TriggerEvent *event, TriggerGuide *guide, TriggerMacroVote *cur_vote )
{
// Lookup full index
var_uint_t guide_index = KLL_TriggerIndex_loopkup( guide->type, guide->scanCode );
var_uint_t event_index = KLL_TriggerIndex_loopkup( event->type, event->index );
// Depending on key type
switch ( guide->type )
{
// Normal State Type
case TriggerType_Switch1:
case TriggerType_Switch2:
case TriggerType_Switch3:
case TriggerType_Switch4:
// LED State Type
case TriggerType_LED1:
// Layer State Type
case TriggerType_Layer1:
case TriggerType_Layer2:
case TriggerType_Layer3:
case TriggerType_Layer4:
// Activity State Types
case TriggerType_Sleep1:
case TriggerType_Resume1:
case TriggerType_Inactive1:
case TriggerType_Active1:
// Depending on the state of the buffered key, make voting decision
// Only monitor 0x70 bits if set in the guide, otherwise ensure they are 0x00
// Used for Layer state information
// Correct key
if (
guide_index == event_index &&
guide->type == event->type &&
(
(guide->state & 0x70) == (event->state & 0x70) ||
(guide->state & 0x70) == 0x00
)
)
{
return Trigger_evalLongTriggerMacroVote_PHRO( event->state, 1 );
}
// Incorrect key
else
{
return Trigger_evalLongTriggerMacroVote_PHRO( event->state, 0 );
}
break;
// Analog State Type
case TriggerType_Analog1:
case TriggerType_Analog2:
case TriggerType_Analog3:
case TriggerType_Analog4:
erro_printNL("Analog State Type - Not implemented...");
break;
// Animation State Type
case TriggerType_Animation1:
case TriggerType_Animation2:
case TriggerType_Animation3:
case TriggerType_Animation4:
// Depending on the state of the buffered key, make voting decision
// Correct trigger
if (
guide_index == event_index &&
guide->type == event->type &&
guide->state == event->state
)
{
return Trigger_evalLongTriggerMacroVote_DRO( event->state, 1 );
}
// Incorrect trigger
else
{
return Trigger_evalLongTriggerMacroVote_DRO( event->state, 0 );
}
break;
// Rotation State Type
case TriggerType_Rotation1:
// Rotation triggers use state as the index, rather than encoding a type of action
// There is only "activated" state for rotations, which is only sent once
// This makes rotations not so useful for long macros
// (though it may be possible to implement it if there is demand)
// TODO
erro_printNL("Rotation State Type (Long Macros) - Not implemented...");
break;
// Dial State Type
case TriggerType_Dial1:
// Dial triggers are single shot events
// The Off event is not sent (though it does exist)
// So all events must be single shot
// This make dials not so useful when constructing long macros
// (though it may be possible to implement it if there is demand)
// TODO
erro_printNL("Dial State Type (Long Macros) - Not implemented...");
break;
// Invalid State Type
default:
erro_printNL("Invalid State Type. This is a bug.");
break;
}
// XXX Shouldn't reach here
return TriggerMacroVote_Invalid;
}
// Iterate over combo, voting on the key state
TriggerMacroVote Trigger_overallVote(
const TriggerMacro *macro,
TriggerMacroRecord *record,
uint8_t long_trigger_macro,
var_uint_t pos
)
{
// Length of the combo being processed
uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
// The macro is waiting for input when in the TriggerMacro_Waiting state
// Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
// Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
// On scan after position increment, change to TriggerMacro_Waiting state
// TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
{
// Assign TriggerGuide element (key type, state and scancode)
TriggerGuide *guide = (TriggerGuide*)(¯o->guide[ comboItem ]);
TriggerMacroVote vote = TriggerMacroVote_Invalid;
// Iterate through the key buffer, comparing to each key in the combo
for ( var_uint_t key = 0; key < macroTriggerEventBufferSize; key++ )
{
// Lookup key information
TriggerEvent *triggerInfo = ¯oTriggerEventBuffer[ key ];
// Vote on triggers
vote |= long_trigger_macro
? Trigger_evalLongTriggerMacroVote( triggerInfo, guide, &overallVote )
: Trigger_evalShortTriggerMacroVote( triggerInfo, guide, &overallVote );
}
// Mask out incorrect votes, if anything indicates a pass
if ( vote >= TriggerMacroVote_Pass )
{
vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
}
// If no pass vote was found after scanning all of the keys
// Fail the combo, if this is a short macro (long macros already will have a fail vote)
if ( !long_trigger_macro && vote < TriggerMacroVote_Pass )
{
vote |= TriggerMacroVote_Fail;
}
// After voting, append to overall vote
overallVote |= vote;
}
return overallVote;
}
// Evaluate/Update TriggerMacro
TriggerMacroEval Trigger_evalTriggerMacro( var_uint_t triggerMacroIndex )
{
// Lookup TriggerMacro
const TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
TriggerMacroRecord *record = &TriggerMacroRecordList[ triggerMacroIndex ];
// Check if this is a long Trigger Macro
uint8_t long_trigger_macro = Trigger_isLongTriggerMacro( macro );
// Long Macro
if ( long_trigger_macro )
{
// Check if macro has finished and should be incremented sequence elements
if ( record->state != TriggerMacro_Waiting )
{
record->prevPos = record->pos;
record->pos = record->pos + macro->guide[ record->pos ] * TriggerGuideSize + 1;
}
// Current Macro position
var_uint_t pos = record->pos;
// Length of the combo being processed
uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
// Iterate through the items in the combo, voting the on the key state
// If any of the pressed keys do not match, fail the macro
if ( comboLength != 0 )
{
overallVote |= Trigger_overallVote( macro, record, long_trigger_macro, pos );
}
// If this is a sequence, and have processed at least one vote already
// then we need to keep track of releases
if ( pos != 0 )
{
overallVote |= Trigger_overallVote( macro, record, long_trigger_macro, record->prevPos );
}
// If no pass vote was found after scanning the entire combo
// And this is the first position in the combo, just remove it (nothing important happened)
if ( overallVote & TriggerMacroVote_DoNothingRelease && pos == 0 )
{
overallVote |= TriggerMacroVote_Fail;
}
// Vote Debug
switch ( voteDebugMode )
{
case 1:
Trigger_showTriggerMacroVote( overallVote, long_trigger_macro );
print(" TriggerMacroList[");
printInt16( triggerMacroIndex );
print("]");
break;
}
// Decide new state of macro after voting
// Fail macro, remove from pending list
if ( overallVote & TriggerMacroVote_Fail )
{
return TriggerMacroEval_Remove;
}
// Do nothing, incorrect key is being held or released
else if ( overallVote & TriggerMacroVote_DoNothing )
{
record->state = TriggerMacro_Waiting;
// Just doing nothing :)
}
// If ready for release state but we get both release and the next press at the same time
// This is unlikely, but possible
else if ( ( overallVote & TriggerMacroVote_PassRelease ) == TriggerMacroVote_PassRelease )
{
record->state = TriggerMacro_PressRelease;
// If this is the last combo in the sequence, trigger result
if ( macro->guide[ pos + comboLength + 1 ] == 0 )
{
return TriggerMacroEval_DoResultAndRemove;
}
}
// If ready for transition and in Press state, increment combo position
else if ( overallVote & TriggerMacroVote_Release && record->state & TriggerMacro_Press )
{
record->state = TriggerMacro_Release;
// If this is the last combo in the sequence, trigger result
// Or, the final release of a sequence
if ( comboLength == 0 || macro->guide[ pos + comboLength + 1 ] == 0 )
{
return TriggerMacroEval_DoResultAndRemove;
}
}
// If passing and in Waiting state, set macro state to Press
else if ( overallVote & TriggerMacroVote_Pass
&& ( record->state == TriggerMacro_Waiting || record->state & TriggerMacro_Press ) )
{
record->state = TriggerMacro_Press;
// If this is the last combo in the sequence, trigger result
if ( macro->guide[ pos + comboLength + 1 ] == 0 )
{
return TriggerMacroEval_DoResultAndRemove;
}
}
}
// Short Macro
else
{
// Current Macro position
var_uint_t pos = record->pos;
// Iterate through the items in the combo, voting the on the key state
// If any of the pressed keys do not match, fail the macro
TriggerMacroVote overallVote = Trigger_overallVote( macro, record, long_trigger_macro, pos );
// Vote Debug
switch ( voteDebugMode )
{
case 1:
Trigger_showTriggerMacroVote( overallVote, long_trigger_macro );
print(" TriggerMacroList[");
printInt16( triggerMacroIndex );
print("]");
break;
}
// Decide new state of macro after voting
// Fail macro, remove from pending list
if ( overallVote & TriggerMacroVote_Fail )
{
return TriggerMacroEval_Remove;
}
// If passing and in Waiting state, set macro state to Press
// And trigger result
else if ( overallVote & TriggerMacroVote_Pass
&& ( record->state == TriggerMacro_Waiting || record->state == TriggerMacro_Press )
)
{
record->state = TriggerMacro_Press;
// Long result macro (more than 1 combo)
if ( Trigger_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
{
// Only ever trigger result once, on press
if ( overallVote == TriggerMacroVote_Pass )
{
return TriggerMacroEval_DoResultAndRemove;
}
}
// Short result macro
else
{
// Trigger result continuously
return TriggerMacroEval_DoResult;
}
}
// Otherwise, just remove the macro on key release
else if ( overallVote & TriggerMacroVote_Release )
{
// Long result macro (more than 1 combo) are ignored (only on press)
if ( !Trigger_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
{
record->state = TriggerMacro_Release;
return TriggerMacroEval_DoResultAndRemove;
}
}
// This is a short macro, just remove it
// The state can be rebuilt on the next iteration
return TriggerMacroEval_Remove;
}
return TriggerMacroEval_DoNothing;
}
// Update pending trigger list
void Trigger_updateTriggerMacroPendingList()
{
// Iterate over the macroTriggerEventBuffer to add any new Trigger Macros to the pending list
for ( var_uint_t key = 0; key < macroTriggerEventBufferSize; key++ )
{
TriggerEvent *event = ¯oTriggerEventBuffer[ key ];
// If this is a release case, indicate to layer lookup for possible latch expiry
uint8_t latch_expire = event->state == ScheduleType_R;
// Lookup Trigger List
nat_ptr_t *triggerList = Layer_layerLookup( event, latch_expire );
// If there was an error during lookup, skip
if ( triggerList == 0 )
continue;
// Number of Triggers in list
nat_ptr_t triggerListSize = triggerList[0];
// Iterate over triggerList to see if any TriggerMacros need to be added
// First item is the number of items in the TriggerList
for ( var_uint_t macro = 1; macro < triggerListSize + 1; macro++ )
{
// Lookup trigger macro index
var_uint_t triggerMacroIndex = triggerList[ macro ];
// Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
// triggerList needs to be added
var_uint_t pending = 0;
for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
{
// Stop scanning if the trigger macro index is found in the pending list
if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
break;
}
// If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
// Add it to the list
if ( pending == macroTriggerMacroPendingListSize )
{
macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
// Reset macro position
TriggerMacroRecordList[ triggerMacroIndex ].pos = 0;
TriggerMacroRecordList[ triggerMacroIndex ].prevPos = 0;
TriggerMacroRecordList[ triggerMacroIndex ].state = TriggerMacro_Waiting;
}
}
}
}
// Determines whether or not a scancode is used on a trigger
// index -> index within trigger list
uint8_t Trigger_DetermineScanCodeOnTrigger( const Layer *layer, uint8_t index )
{
// Check all triggers
for ( uint8_t trigger = 1; trigger <= layer->triggerMap[index][0]; trigger++ )
{
// Trigger element
nat_ptr_t elem = layer->triggerMap[index][trigger];
// Lookup trigger type
const uint8_t *pos = TriggerMacroList[elem].guide;
// If there are no elements, ignore
if ( pos[0] == 0 )
{
continue;
}
// Only look at first type, no need to go further
// XXX (HaaTa) This may cause bugs, but it's not as likely
// It's also much faster to only check the first type
switch ( pos[1] )
{
case TriggerType_Switch1:
case TriggerType_Switch2:
case TriggerType_Switch3:
case TriggerType_Switch4:
case TriggerType_Analog1:
case TriggerType_Analog2:
case TriggerType_Analog3:
case TriggerType_Analog4:
return 1;
default:
break;
}
}
// No triggers
return 0;
}
void Trigger_setup()
{
// Initialize TriggerMacro states
for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
{
TriggerMacroRecordList[ macro ].pos = 0;
TriggerMacroRecordList[ macro ].prevPos = 0;
TriggerMacroRecordList[ macro ].state = TriggerMacro_Waiting;
}
}
void Trigger_process()
{
// Update pending trigger list, before processing TriggerMacros
Trigger_updateTriggerMacroPendingList();
// Tail pointer for macroTriggerMacroPendingList
// Macros must be explicitly re-added
var_uint_t macroTriggerMacroPendingListTail = 0;
// Display trigger information before processing
if ( triggerPendingDebugMode )
{
print("\033[1;30mTPe\033[0m");
for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
{
print(" ");
printInt8( macroTriggerMacroPendingList[ macro ] );
}
print(NL);
}
// Iterate through the pending TriggerMacros, processing each of them
for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
{
index_uint_t cur_macro = macroTriggerMacroPendingList[ macro ];
switch ( Trigger_evalTriggerMacro( cur_macro ) )
{
// Trigger Result Macro (purposely falling through)
case TriggerMacroEval_DoResult:
if ( voteDebugMode )
{
print(" DR");
}
// Append ResultMacro to PendingList
Result_appendResultMacroToPendingList(
&TriggerMacroList[ cur_macro ]
);
default:
if ( voteDebugMode )
{
print(" _" NL);
}
macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = cur_macro;
break;
// Trigger Result Macro and Remove (purposely falling through)
case TriggerMacroEval_DoResultAndRemove:
if ( voteDebugMode )
{
print(" DRaR");
}
// Append ResultMacro to PendingList
Result_appendResultMacroToPendingList(
&TriggerMacroList[ cur_macro ]
);
// Remove Macro from Pending List, nothing to do, removing by default
case TriggerMacroEval_Remove:
if ( voteDebugMode )
{
print(" R" NL);
}
break;
}
}
// Update the macroTriggerMacroPendingListSize with the tail pointer
macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
}