1 /* Copyright (C) 2014 by Jacob Alexander
3 * This file is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This file is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this file. If not, see <http://www.gnu.org/licenses/>.
17 // ----- Includes -----
20 #include <Lib/MacroLib.h>
26 #include <scan_loop.h>
30 #include <defaultMap.h>
31 #include "generatedKeymap.h" // TODO Use actual generated version
38 // ----- Function Declarations -----
40 void cliFunc_capList ( char* args );
41 void cliFunc_capSelect ( char* args );
42 void cliFunc_keyHold ( char* args );
43 void cliFunc_keyPress ( char* args );
44 void cliFunc_keyRelease( char* args );
45 void cliFunc_layerList ( char* args );
46 void cliFunc_layerState( char* args );
47 void cliFunc_macroDebug( char* args );
48 void cliFunc_macroList ( char* args );
49 void cliFunc_macroProc ( char* args );
50 void cliFunc_macroShow ( char* args );
51 void cliFunc_macroStep ( char* args );
57 // Bit positions are important, passes (correct key) always trump incorrect key votes
58 typedef enum TriggerMacroVote {
59 TriggerMacroVote_Release = 0x8, // Correct key
60 TriggerMacroVote_PassRelease = 0xC, // Correct key (both pass and release)
61 TriggerMacroVote_Pass = 0x4, // Correct key
62 TriggerMacroVote_DoNothing = 0x2, // Incorrect key
63 TriggerMacroVote_Fail = 0x1, // Incorrect key
64 TriggerMacroVote_Invalid = 0x0, // Invalid state
67 typedef enum TriggerMacroEval {
68 TriggerMacroEval_DoNothing,
69 TriggerMacroEval_DoResult,
70 TriggerMacroEval_DoResultAndRemove,
71 TriggerMacroEval_Remove,
74 typedef enum ResultMacroEval {
75 ResultMacroEval_DoNothing,
76 ResultMacroEval_Remove,
81 // ----- Variables -----
83 // Macro Module command dictionary
84 const char macroCLIDictName[] = "Macro Module Commands";
85 const CLIDictItem macroCLIDict[] = {
86 { "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
87 { "capSelect", "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B", cliFunc_capSelect },
88 { "keyHold", "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyHold },
89 { "keyPress", "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
90 { "keyRelease", "Send key-release event to macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
91 { "layerList", "List available layers.", cliFunc_layerList },
92 { "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", cliFunc_layerState },
93 { "macroDebug", "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
94 { "macroList", "List the defined trigger and result macros.", cliFunc_macroList },
95 { "macroProc", "Pause/Resume macro processing.", cliFunc_macroProc },
96 { "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", cliFunc_macroShow },
97 { "macroStep", "Do N macro processing steps. Defaults to 1.", cliFunc_macroStep },
98 { 0, 0, 0 } // Null entry for dictionary end
102 // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
103 uint8_t macroDebugMode = 0;
105 // Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
106 uint8_t macroPauseMode = 0;
108 // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
109 unsigned int macroStepCounter = 0;
112 // Key Trigger List Buffer
113 TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
114 uint8_t macroTriggerListBufferSize = 0;
116 // Pending Trigger Macro Index List
117 // * Any trigger macros that need processing from a previous macro processing loop
118 // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
119 // Possibly could be calculated by the KLL compiler
120 // XXX It may be possible to calculate the worst case using the KLL compiler
121 unsigned int macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
122 unsigned int macroTriggerMacroPendingListSize = 0;
125 // * When modifying layer state and the state is non-0x0, the stack must be adjusted
126 unsigned int macroLayerIndexStack[ LayerNum ] = { 0 };
127 unsigned int macroLayerIndexStackSize = 0;
129 // Pending Result Macro Index List
130 // * Any result macro that needs processing from a previous macro processing loop
131 unsigned int macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
132 unsigned int macroResultMacroPendingListSize = 0;
136 // ----- Capabilities -----
138 // Modifies the specified Layer control byte
139 // Argument #1: Layer Index -> unsigned int
140 // Argument #2: Toggle byte -> uint8_t
141 void Macro_layerStateToggle_capability( uint8_t state, uint8_t stateType, uint8_t *args )
143 // Display capability name
144 if ( stateType == 0xFF && state == 0xFF )
146 print("Macro_layerState(layerIndex,toggleByte)");
150 // Get layer index from arguments
151 // Cast pointer to uint8_t to unsigned int then access that memory location
152 unsigned int layer = *(unsigned int*)(&args[0]);
154 // Get layer toggle byte
155 uint8_t toggleByte = args[ sizeof(unsigned int) ];
157 // Is layer in the LayerIndexStack?
158 uint8_t inLayerIndexStack = 0;
159 unsigned int stackItem = 0;
160 while ( stackItem < macroLayerIndexStackSize )
162 // Flag if layer is already in the LayerIndexStack
163 if ( macroLayerIndexStack[ stackItem ] == layer )
165 inLayerIndexStack = 1;
169 // Increment to next item
173 // Toggle Layer State Byte
174 if ( LayerIndex[ layer ].state & toggleByte )
177 LayerIndex[ layer ].state &= ~toggleByte;
182 LayerIndex[ layer ].state |= toggleByte;
185 // If the layer was not in the LayerIndexStack add it
186 if ( !inLayerIndexStack )
188 macroLayerIndexStack[ macroLayerIndexStackSize++ ] = layer;
191 // If the layer is in the LayerIndexStack and the state is 0x00, remove
192 if ( LayerIndex[ layer ].state == 0x00 && inLayerIndexStack )
194 // Remove the layer from the LayerIndexStack
195 // Using the already positioned stackItem variable from the loop above
196 while ( stackItem < macroLayerIndexStackSize )
198 macroLayerIndexStack[ stackItem ] = macroLayerIndexStack[ stackItem + 1 ];
202 // Reduce LayerIndexStack size
203 macroLayerIndexStackSize--;
209 // ----- Functions -----
211 // Looks up the trigger list for the given scan code (from the active layer)
212 // NOTE: Calling function must handle the NULL pointer case
213 unsigned int *Macro_layerLookup( uint8_t scanCode )
215 // If no trigger macro is defined at the given layer, fallthrough to the next layer
216 for ( unsigned int layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ )
219 Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
221 // Check if latch has been pressed for this layer
222 // XXX Regardless of whether a key is found, the latch is removed on first lookup
223 uint8_t latch = layer->state & 0x02;
226 layer->state &= ~0x02;
229 // Only use layer, if state is valid
230 // XOR each of the state bits
231 // If only two are enabled, do not use this state
232 if ( (layer->state & 0x01) ^ (latch>>1) ^ ((layer->state & 0x04)>>2) )
235 unsigned int **map = (unsigned int**)layer->triggerMap;
237 // Determine if layer has key defined
238 if ( map != 0 && *map[ scanCode ] != 0 )
239 return map[ scanCode ];
243 // Do lookup on default layer
244 unsigned int **map = (unsigned int**)LayerIndex[0].triggerMap;
246 // Determine if layer has key defined
247 if ( map == 0 && *map[ scanCode ] == 0 )
249 erro_msg("Scan Code has no defined Trigger Macro: ");
250 printHex( scanCode );
254 // Return lookup result
255 return map[ scanCode ];
259 // Update the scancode key state
265 // * 0x04 - Unpressed (this is currently ignored)
266 inline void Macro_keyState( uint8_t scanCode, uint8_t state )
268 // Only add to macro trigger list if one of three states
271 case 0x01: // Pressed
273 case 0x03: // Released
274 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
275 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
276 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
277 macroTriggerListBufferSize++;
283 // Update the scancode analog state
287 // * 0x02-0xFF - Analog value (low to high)
288 inline void Macro_analogState( uint8_t scanCode, uint8_t state )
290 // Only add to macro trigger list if non-off
293 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
294 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
295 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
296 macroTriggerListBufferSize++;
305 inline void Macro_ledState( uint8_t ledCode, uint8_t state )
307 // Only add to macro trigger list if non-off
310 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
311 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
312 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
313 macroTriggerListBufferSize++;
318 // Append result macro to pending list, checking for duplicates
319 // Do nothing if duplicate
320 inline void Macro_appendResultMacroToPendingList( unsigned int resultMacroIndex )
322 // Iterate through result macro pending list, making sure this macro hasn't been added yet
323 for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
325 // If duplicate found, do nothing
326 if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
330 // No duplicates found, add to pending list
331 macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
335 // Determine if long ResultMacro (more than 1 seqence element)
336 inline uint8_t Macro_isLongResultMacro( ResultMacro *macro )
338 // Check the second sequence combo length
339 // If non-zero return 1 (long sequence)
340 // 0 otherwise (short sequence)
341 return macro->guide[ macro->guide[0] * ResultGuideSize( (ResultGuide*)macro->guide ) ] > 0 ? 1 : 0;
345 // Votes on the given key vs. guide
346 inline TriggerMacroVote Macro_evalTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
348 // Depending on key type
349 switch ( guide->type )
353 // Depending on the state of the buffered key, make voting decision
355 if ( guide->scanCode != key->scanCode )
357 switch ( key->state )
359 // Wrong key, pressed, fail
361 return TriggerMacroVote_Fail;
363 // Wrong key, held or released, do not pass (no effect)
366 return TriggerMacroVote_DoNothing;
373 switch ( key->state )
375 // Correct key, pressed, possible passing
377 return TriggerMacroVote_Pass;
379 // Correct key, held, possible passing or release
381 return TriggerMacroVote_PassRelease;
383 // Correct key, released, possible release
385 return TriggerMacroVote_Release;
393 erro_print("LED State Type - Not implemented...");
398 erro_print("Analog State Type - Not implemented...");
401 // Invalid State Type
403 erro_print("Invalid State Type. This is a bug.");
407 // XXX Shouldn't reach here
408 return TriggerMacroVote_Invalid;
412 // Evaluate/Update TriggerMacro
413 inline TriggerMacroEval Macro_evalTriggerMacro( unsigned int triggerMacroIndex )
415 // Lookup TriggerMacro
416 TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
418 // Check if macro has finished and should be incremented sequence elements
419 if ( macro->state == TriggerMacro_Release )
421 macro->state = TriggerMacro_Waiting;
422 macro->pos = macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize;
425 // Current Macro position
426 unsigned int pos = macro->pos;
428 // Length of the combo being processed
429 uint8_t comboLength = macro->guide[ pos ];
431 // If no combo items are left, remove the TriggerMacro from the pending list
432 if ( comboLength == 0 )
434 return TriggerMacroEval_Remove;
437 // Iterate through the key buffer, comparing to each key in the combo
438 // If any of the pressed keys do not match, fail the macro
440 // The macro is waiting for input when in the TriggerMacro_Waiting state
441 // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
442 // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
443 // On scan after position increment, change to TriggerMacro_Waiting state
444 // TODO Add support for system LED states (NumLock, CapsLock, etc.)
445 // TODO Add support for analog key states
446 // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
447 // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
448 TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
449 for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
451 // Lookup key information
452 TriggerGuide *keyInfo = ¯oTriggerListBuffer[ key ];
454 // Iterate through the items in the combo, voting the on the key state
455 TriggerMacroVote vote = TriggerMacroVote_Invalid;
456 for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
458 // Assign TriggerGuide element (key type, state and scancode)
459 TriggerGuide *guide = (TriggerGuide*)(¯o->guide[ comboItem ]);
461 // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
462 // Also mask all of the non-passing votes
463 vote |= Macro_evalTriggerMacroVote( keyInfo, guide );
464 if ( vote >= TriggerMacroVote_Pass )
466 vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
471 // After voting, append to overall vote
475 // Decide new state of macro after voting
476 // Fail macro, remove from pending list
477 if ( overallVote & TriggerMacroVote_Fail )
479 return TriggerMacroEval_Remove;
481 // Do nothing, incorrect key is being held or released
482 else if ( overallVote & TriggerMacroVote_DoNothing )
484 // Just doing nothing :)
486 // If passing and in Waiting state, set macro state to Press
487 else if ( overallVote & TriggerMacroVote_Pass && macro->state == TriggerMacro_Waiting )
489 macro->state = TriggerMacro_Press;
491 // If in press state, and this is the final combo, send request for ResultMacro
492 // Check to see if the result macro only has a single element
493 // If this result macro has more than 1 key, only send once
494 // TODO Add option to have macro repeat rate
495 if ( macro->guide[ pos + comboLength ] == 0 )
497 // Long Macro, only send once (more than 1 sequence item)
498 // Short Macro (only 1 sequence item)
499 return Macro_isLongResultMacro( &ResultMacroList[ macro->result ] )
500 ? TriggerMacroEval_DoResult
501 : TriggerMacroEval_DoResultAndRemove;
505 // If ready for transition and in Press state, set to Waiting and increment combo position
506 // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
507 else if ( overallVote & TriggerMacroVote_Release && macro->state == TriggerMacro_Press )
509 macro->state = TriggerMacro_Release;
512 return TriggerMacroEval_DoNothing;
516 // Evaluate/Update ResultMacro
517 inline ResultMacroEval Macro_evalResultMacro( unsigned int resultMacroIndex )
519 // Lookup ResultMacro
520 ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
522 // Current Macro position
523 unsigned int pos = macro->pos;
525 // Length of combo being processed
526 uint8_t comboLength = macro->guide[ pos ];
528 // If no combo items are left, remove the ResultMacro from the pending list
529 if ( comboLength == 0 )
531 return ResultMacroEval_Remove;
534 // Function Counter, used to keep track of the combo items processed
535 unsigned int funcCount = 0;
537 // Combo Item Position within the guide
538 unsigned int comboItem = pos + 1;
540 // Iterate through the Result Combo
541 while ( funcCount < comboLength )
543 // Assign TriggerGuide element (key type, state and scancode)
544 ResultGuide *guide = (ResultGuide*)(¯o->guide[ pos ]);
546 // Do lookup on capability function
547 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
550 capability( macro->state, macro->stateType, &guide->args );
552 // Increment counters
554 comboItem += ResultGuideSize( (ResultGuide*)(¯o->guide[ comboItem ]) );
557 // Move to next item in the sequence
558 macro->pos = comboItem;
560 // If the ResultMacro is finished, it will be removed on the next iteration
561 return ResultMacroEval_DoNothing;
565 // Update pending trigger list
566 void Macro_updateTriggerMacroPendingList()
568 // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
569 for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
571 // Lookup Trigger List
572 unsigned int *triggerList = Macro_layerLookup( macroTriggerListBuffer[ key ].scanCode );
574 // Number of Triggers in list
575 unsigned int triggerListSize = triggerList[0];
577 // Iterate over triggerList to see if any TriggerMacros need to be added
578 // First item is the number of items in the TriggerList
579 for ( unsigned int macro = 1; macro < triggerListSize + 1; macro++ )
581 // Lookup trigger macro index
582 unsigned int triggerMacroIndex = triggerList[ macro ];
584 // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
585 // triggerList needs to be added
586 unsigned int pending = 0;
587 for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
589 // Stop scanning if the trigger macro index is found in the pending list
590 if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
594 // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
595 // Add it to the list
596 if ( pending == macroTriggerMacroPendingListSize )
598 macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
605 // Macro Procesing Loop
606 // Called once per USB buffer send
607 inline void Macro_process()
609 // Only do one round of macro processing between Output Module timer sends
610 if ( USBKeys_Sent != 0 )
613 // If the pause flag is set, only process if the step counter is non-zero
614 if ( macroPauseMode )
616 if ( macroStepCounter == 0 )
619 // Proceed, decrementing the step counter
621 dbug_print("Macro Step");
624 // Update pending trigger list, before processing TriggerMacros
625 Macro_updateTriggerMacroPendingList();
627 // Tail pointer for macroTriggerMacroPendingList
628 // Macros must be explicitly re-added
629 unsigned int macroTriggerMacroPendingListTail = 0;
631 // Iterate through the pending TriggerMacros, processing each of them
632 for ( unsigned int macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
634 switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
636 // Trigger Result Macro (purposely falling through)
637 case TriggerMacroEval_DoResult:
638 // Append ResultMacro to PendingList
639 Macro_appendResultMacroToPendingList( TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ].result );
641 // Otherwise, just re-add
643 macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
646 // Trigger Result Macro and Remove (purposely falling through)
647 case TriggerMacroEval_DoResultAndRemove:
648 // Append ResultMacro to PendingList
649 Macro_appendResultMacroToPendingList( TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ].result );
651 // Remove Macro from Pending List, nothing to do, removing by default
652 case TriggerMacroEval_Remove:
657 // Update the macroTriggerMacroPendingListSize with the tail pointer
658 macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
661 // Tail pointer for macroResultMacroPendingList
662 // Macros must be explicitly re-added
663 unsigned int macroResultMacroPendingListTail = 0;
665 // Iterate through the pending ResultMacros, processing each of them
666 for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
668 switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
670 // Re-add macros to pending list
671 case ResultMacroEval_DoNothing:
673 macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
676 // Remove Macro from Pending List, nothing to do, removing by default
677 case ResultMacroEval_Remove:
682 // Update the macroResultMacroPendingListSize with the tail pointer
683 macroResultMacroPendingListSize = macroResultMacroPendingListTail;
685 // Signal buffer that we've used it
686 Scan_finishedWithMacro( macroTriggerListBufferSize );
688 // Reset TriggerList buffer
689 macroTriggerListBufferSize = 0;
691 // If Macro debug mode is set, clear the USB Buffer
692 if ( macroDebugMode )
694 USBKeys_Modifiers = 0;
700 inline void Macro_setup()
702 // Register Macro CLI dictionary
703 CLI_registerDictionary( macroCLIDict, macroCLIDictName );
705 // Disable Macro debug mode
708 // Disable Macro pause flag
711 // Set Macro step counter to zero
712 macroStepCounter = 0;
714 // Make sure macro trigger buffer is empty
715 macroTriggerListBufferSize = 0;
717 // Initialize TriggerMacro states
718 for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
720 TriggerMacroList[ macro ].pos = 0;
721 TriggerMacroList[ macro ].state = TriggerMacro_Waiting;
724 // Initialize ResultMacro states
725 for ( unsigned int macro = 0; macro < ResultMacroNum; macro++ )
727 ResultMacroList[ macro ].pos = 0;
728 ResultMacroList[ macro ].state = 0;
729 ResultMacroList[ macro ].stateType = 0;
734 // ----- CLI Command Functions -----
736 void cliFunc_capList( char* args )
739 info_msg("Capabilities List");
740 printHex( CapabilitiesNum );
742 // Iterate through all of the capabilities and display them
743 for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
749 // Display/Lookup Capability Name (utilize debug mode of capability)
750 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
751 capability( 0xFF, 0xFF, 0 );
755 void cliFunc_capSelect( char* args )
757 // Parse code from argument
760 char* arg2Ptr = args;
762 // Total number of args to scan (must do a lookup if a keyboard capability is selected)
763 unsigned int totalArgs = 2; // Always at least two args
764 unsigned int cap = 0;
766 // Arguments used for keyboard capability function
767 unsigned int argSetCount = 0;
768 uint8_t *argSet = (uint8_t*)args;
771 for ( unsigned int c = 0; argSetCount < totalArgs; c++ )
774 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
776 // Stop processing args if no more are found
777 // Extra arguments are ignored
778 if ( *arg1Ptr == '\0' )
781 // For the first argument, choose the capability
782 if ( c == 0 ) switch ( arg1Ptr[0] )
784 // Keyboard Capability
786 // Determine capability index
787 cap = numToInt( &arg1Ptr[1] );
789 // Lookup the number of args
790 totalArgs += CapabilitiesList[ cap ].argCount;
794 // Because allocating memory isn't doable, and the argument count is arbitrary
795 // The argument pointer is repurposed as the argument list (much smaller anyways)
796 argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
798 // Once all the arguments are prepared, call the keyboard capability function
799 if ( argSetCount == totalArgs )
801 // Indicate that the capability was called
806 printHex( argSet[0] );
808 printHex( argSet[1] );
810 printHex( argSet[2] );
813 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
814 capability( argSet[0], argSet[1], &argSet[2] );
819 void cliFunc_keyHold( char* args )
821 // Parse codes from arguments
824 char* arg2Ptr = args;
830 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
832 // Stop processing args if no more are found
833 if ( *arg1Ptr == '\0' )
836 // Ignore non-Scancode numbers
837 switch ( arg1Ptr[0] )
841 Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
847 void cliFunc_keyPress( char* args )
849 // Parse codes from arguments
852 char* arg2Ptr = args;
858 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
860 // Stop processing args if no more are found
861 if ( *arg1Ptr == '\0' )
864 // Ignore non-Scancode numbers
865 switch ( arg1Ptr[0] )
869 Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
875 void cliFunc_keyRelease( char* args )
877 // Parse codes from arguments
880 char* arg2Ptr = args;
886 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
888 // Stop processing args if no more are found
889 if ( *arg1Ptr == '\0' )
892 // Ignore non-Scancode numbers
893 switch ( arg1Ptr[0] )
897 Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
903 void cliFunc_layerList( char* args )
906 info_msg("Layer List");
908 // Iterate through all of the layers and display them
909 for ( unsigned int layer = 0; layer < LayerNum; layer++ )
915 // Display layer name
916 dPrint( (char*)LayerIndex[ layer ].name );
920 print(" \033[1m(default)\033[0m");
923 print( NL "\t\t Layer State: " );
924 printHex( LayerIndex[ layer ].state );
927 print(" Max Index: ");
928 printHex( LayerIndex[ layer ].max );
932 void cliFunc_layerState( char* args )
934 // Parse codes from arguments
937 char* arg2Ptr = args;
942 // Process first two args
943 for ( uint8_t c = 0; c < 2; c++ )
946 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
948 // Stop processing args if no more are found
949 if ( *arg1Ptr == '\0' )
954 // First argument (e.g. L1)
956 if ( arg1Ptr[0] != 'L' )
959 arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
961 // Second argument (e.g. 4)
963 arg2 = (uint8_t)numToInt( arg1Ptr );
965 // Display operation (to indicate that it worked)
967 info_msg("Setting Layer L");
972 // Set the layer state
973 LayerIndex[ arg1 ].state = arg2;
979 void cliFunc_macroDebug( char* args )
981 // Toggle macro debug mode
982 macroDebugMode = macroDebugMode ? 0 : 1;
985 info_msg("Macro Debug Mode: ");
986 printInt8( macroDebugMode );
989 void cliFunc_macroList( char* args )
991 // Show pending key events
993 info_msg("Pending Key Events: ");
994 printInt16( (uint16_t)macroTriggerListBufferSize );
996 for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
998 printHex( macroTriggerListBuffer[ key ].scanCode );
1002 // Show pending trigger macros
1004 info_msg("Pending Trigger Macros: ");
1005 printInt16( (uint16_t)macroTriggerMacroPendingListSize );
1007 for ( unsigned int macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
1009 printHex( macroTriggerMacroPendingList[ macro ] );
1013 // Show pending result macros
1015 info_msg("Pending Result Macros: ");
1016 printInt16( (uint16_t)macroResultMacroPendingListSize );
1018 for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
1020 printHex( macroResultMacroPendingList[ macro ] );
1024 // Show available trigger macro indices
1026 info_msg("Trigger Macros Range: T0 -> T");
1027 printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
1029 // Show available result macro indices
1031 info_msg("Result Macros Range: R0 -> R");
1032 printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
1034 // Show Trigger to Result Macro Links
1036 info_msg("Trigger : Result Macro Pairs");
1037 for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
1041 printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
1043 printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
1047 void cliFunc_macroProc( char* args )
1049 // Toggle macro pause mode
1050 macroPauseMode = macroPauseMode ? 0 : 1;
1053 info_msg("Macro Processing Mode: ");
1054 printInt8( macroPauseMode );
1057 void macroDebugShowTrigger( unsigned int index )
1059 // Only proceed if the macro exists
1060 if ( index >= TriggerMacroNum )
1063 // Trigger Macro Show
1064 TriggerMacro *macro = &TriggerMacroList[ index ];
1067 info_msg("Trigger Macro Index: ");
1068 printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
1071 // Read the comboLength for combo in the sequence (sequence of combos)
1072 unsigned int pos = 0;
1073 uint8_t comboLength = macro->guide[ pos ];
1075 // Iterate through and interpret the guide
1076 while ( comboLength != 0 )
1078 // Initial position of the combo
1079 unsigned int comboPos = ++pos;
1081 // Iterate through the combo
1082 while ( pos < comboLength * TriggerGuideSize + comboPos )
1084 // Assign TriggerGuide element (key type, state and scancode)
1085 TriggerGuide *guide = (TriggerGuide*)(¯o->guide[ pos ]);
1087 // Display guide information about trigger key
1088 printHex( guide->scanCode );
1090 printHex( guide->type );
1092 printHex( guide->state );
1094 // Increment position
1095 pos += TriggerGuideSize;
1097 // Only show combo separator if there are combos left in the sequence element
1098 if ( pos < comboLength * TriggerGuideSize + comboPos )
1102 // Read the next comboLength
1103 comboLength = macro->guide[ pos ];
1105 // Only show sequence separator if there is another combo to process
1106 if ( comboLength != 0 )
1110 // Display current position
1111 print( NL "Position: " );
1112 printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
1114 // Display result macro index
1115 print( NL "Result Macro Index: " );
1116 printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
1119 void macroDebugShowResult( unsigned int index )
1121 // Only proceed if the macro exists
1122 if ( index >= ResultMacroNum )
1125 // Trigger Macro Show
1126 ResultMacro *macro = &ResultMacroList[ index ];
1129 info_msg("Result Macro Index: ");
1130 printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
1133 // Read the comboLength for combo in the sequence (sequence of combos)
1134 unsigned int pos = 0;
1135 uint8_t comboLength = macro->guide[ pos++ ];
1137 // Iterate through and interpret the guide
1138 while ( comboLength != 0 )
1140 // Function Counter, used to keep track of the combos processed
1141 unsigned int funcCount = 0;
1143 // Iterate through the combo
1144 while ( funcCount < comboLength )
1146 // Assign TriggerGuide element (key type, state and scancode)
1147 ResultGuide *guide = (ResultGuide*)(¯o->guide[ pos ]);
1149 // Display Function Index
1150 printHex( guide->index );
1153 // Display Function Ptr Address
1154 printHex( (unsigned int)CapabilitiesList[ guide->index ].func );
1157 // Display/Lookup Capability Name (utilize debug mode of capability)
1158 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
1159 capability( 0xFF, 0xFF, 0 );
1161 // Display Argument(s)
1163 for ( unsigned int arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
1165 // Arguments are only 8 bit values
1166 printHex( (&guide->args)[ arg ] );
1168 // Only show arg separator if there are args left
1169 if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
1174 // Increment position
1175 pos += ResultGuideSize( guide );
1177 // Increment function count
1180 // Only show combo separator if there are combos left in the sequence element
1181 if ( funcCount < comboLength )
1185 // Read the next comboLength
1186 comboLength = macro->guide[ pos++ ];
1188 // Only show sequence separator if there is another combo to process
1189 if ( comboLength != 0 )
1193 // Display current position
1194 print( NL "Position: " );
1195 printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
1197 // Display final trigger state/type
1198 print( NL "Final Trigger State (State/Type): " );
1199 printHex( macro->state );
1201 printHex( macro->stateType );
1204 void cliFunc_macroShow( char* args )
1206 // Parse codes from arguments
1209 char* arg2Ptr = args;
1215 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1217 // Stop processing args if no more are found
1218 if ( *arg1Ptr == '\0' )
1221 // Ignore invalid codes
1222 switch ( arg1Ptr[0] )
1224 // Indexed Trigger Macro
1226 macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
1228 // Indexed Result Macro
1230 macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
1236 void cliFunc_macroStep( char* args )
1238 // Parse number from argument
1239 // NOTE: Only first argument is used
1242 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
1244 // Default to 1, if no argument given
1245 unsigned int count = (unsigned int)numToInt( arg1Ptr );
1250 // Set the macro step counter, negative int's are cast to uint
1251 macroStepCounter = count;