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_keyPress ( char* args );
43 void cliFunc_keyRelease( char* args );
44 void cliFunc_layerList ( char* args );
45 void cliFunc_layerState( char* args );
46 void cliFunc_macroDebug( char* args );
47 void cliFunc_macroList ( char* args );
48 void cliFunc_macroProc ( char* args );
49 void cliFunc_macroShow ( char* args );
50 void cliFunc_macroStep ( char* args );
56 // Bit positions are important, passes (correct key) always trump incorrect key votes
57 typedef enum TriggerMacroVote {
58 TriggerMacroVote_Release = 0x8, // Correct key
59 TriggerMacroVote_PassRelease = 0xC, // Correct key (both pass and release)
60 TriggerMacroVote_Pass = 0x4, // Correct key
61 TriggerMacroVote_DoNothing = 0x2, // Incorrect key
62 TriggerMacroVote_Fail = 0x1, // Incorrect key
63 TriggerMacroVote_Invalid = 0x0, // Invalid state
66 typedef enum TriggerMacroEval {
67 TriggerMacroEval_DoNothing,
68 TriggerMacroEval_DoResult,
69 TriggerMacroEval_DoResultAndRemove,
70 TriggerMacroEval_Remove,
73 typedef enum ResultMacroEval {
74 ResultMacroEval_DoNothing,
75 ResultMacroEval_Remove,
80 // ----- Variables -----
82 // Macro Module command dictionary
83 char* macroCLIDictName = "Macro Module Commands";
84 CLIDictItem macroCLIDict[] = {
85 { "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
86 { "capSelect", "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B", cliFunc_capSelect },
87 { "keyPress", "Send key-presses to the macro module. Held until released. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
88 { "keyRelease", "Release a key-press from the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
89 { "layerList", "List available layers.", cliFunc_layerList },
90 { "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 },
91 { "macroDebug", "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
92 { "macroList", "List the defined trigger and result macros.", cliFunc_macroList },
93 { "macroProc", "Pause/Resume macro processing.", cliFunc_macroProc },
94 { "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 },
95 { "macroStep", "Do N macro processing steps. Defaults to 1.", cliFunc_macroStep },
96 { 0, 0, 0 } // Null entry for dictionary end
100 // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
101 uint8_t macroDebugMode = 0;
103 // Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
104 uint8_t macroPauseMode = 0;
106 // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
107 unsigned int macroStepCounter = 0;
110 // Key Trigger List Buffer
111 TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
112 uint8_t macroTriggerListBufferSize = 0;
114 // Pending Trigger Macro Index List
115 // * Any trigger macros that need processing from a previous macro processing loop
116 // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
117 // Possibly could be calculated by the KLL compiler
118 // XXX It may be possible to calculate the worst case using the KLL compiler
119 unsigned int macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
120 unsigned int macroTriggerMacroPendingListSize = 0;
123 // * When modifying layer state and the state is non-0x0, the stack must be adjusted
124 unsigned int macroLayerIndexStack[ LayerNum ] = { 0 };
125 unsigned int macroLayerIndexStackSize = 0;
127 // Pending Result Macro Index List
128 // * Any result macro that needs processing from a previous macro processing loop
129 unsigned int macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
130 unsigned int macroResultMacroPendingListSize = 0;
134 // ----- Capabilities -----
136 // Modifies the specified Layer control byte
137 // Argument #1: Layer Index -> unsigned int
138 // Argument #2: Toggle byte -> uint8_t
139 void Macro_layerStateToggle_capability( uint8_t state, uint8_t stateType, uint8_t *args )
141 // Display capability name
142 if ( stateType == 0xFF && state == 0xFF )
144 print("Macro_layerState(layerIndex,toggleByte)");
148 // Get layer index from arguments
149 unsigned int layer = (unsigned int)(&args[0]);
151 // Get layer toggle byte
152 uint8_t toggleByte = args[ sizeof(unsigned int) ];
154 // Is layer in the LayerIndexStack?
155 uint8_t inLayerIndexStack = 0;
156 unsigned int stackItem = 0;
157 while ( stackItem < macroLayerIndexStackSize )
159 // Flag if layer is already in the LayerIndexStack
160 if ( macroLayerIndexStack[ stackItem ] == layer )
162 inLayerIndexStack = 1;
166 // Increment to next item
170 // Toggle Layer State Byte
171 if ( LayerIndex[ layer ].state & toggleByte )
174 LayerIndex[ layer ].state &= ~toggleByte;
179 LayerIndex[ layer ].state |= toggleByte;
182 // If the layer was not in the LayerIndexStack add it
183 if ( !inLayerIndexStack )
185 macroLayerIndexStack[ macroLayerIndexStackSize++ ] = layer;
188 // If the layer is in the LayerIndexStack and the state is 0x00, remove
189 if ( LayerIndex[ layer ].state == 0x00 && inLayerIndexStack )
191 // Remove the layer from the LayerIndexStack
192 // Using the already positioned stackItem variable from the loop above
193 while ( stackItem < macroLayerIndexStackSize )
195 macroLayerIndexStack[ stackItem ] = macroLayerIndexStack[ stackItem + 1 ];
199 // Reduce LayerIndexStack size
200 macroLayerIndexStackSize--;
206 // ----- Functions -----
208 // Looks up the trigger list for the given scan code (from the active layer)
209 // NOTE: Calling function must handle the NULL pointer case
210 unsigned int *Macro_layerLookup( uint8_t scanCode )
212 // If no trigger macro is defined at the given layer, fallthrough to the next layer
213 for ( unsigned int layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ )
216 Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
218 // Check if latch has been pressed for this layer
219 // XXX Regardless of whether a key is found, the latch is removed on first lookup
220 uint8_t latch = layer->state & 0x02;
223 layer->state &= ~0x02;
226 // Only use layer, if state is valid
227 // XOR each of the state bits
228 // If only two are enabled, do not use this state
229 if ( (layer->state & 0x01) ^ (latch>>1) ^ ((layer->state & 0x04)>>2) )
232 unsigned int **map = layer->triggerMap;
234 // Determine if layer has key defined
235 if ( map != 0 && *map[ scanCode ] != 0 )
236 return map[ scanCode ];
240 // Do lookup on default layer
241 unsigned int **map = LayerIndex[0].triggerMap;
243 // Determine if layer has key defined
244 if ( map == 0 && *map[ scanCode ] == 0 )
246 erro_msg("Scan Code has no defined Trigger Macro: ");
247 printHex( scanCode );
251 // Return lookup result
252 return map[ scanCode ];
256 // Update the scancode key state
262 // * 0x04 - Unpressed (this is currently ignored)
263 inline void Macro_keyState( uint8_t scanCode, uint8_t state )
265 // Only add to macro trigger list if one of three states
268 case 0x01: // Pressed
270 case 0x03: // Released
271 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
272 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
273 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x00; // Normal key
274 macroTriggerListBufferSize++;
280 // Update the scancode analog state
284 // * 0x02-0xFF - Analog value (low to high)
285 inline void Macro_analogState( uint8_t scanCode, uint8_t state )
287 // Only add to macro trigger list if non-off
290 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
291 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
292 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
293 macroTriggerListBufferSize++;
302 inline void Macro_ledState( uint8_t ledCode, uint8_t state )
304 // Only add to macro trigger list if non-off
307 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
308 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
309 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
310 macroTriggerListBufferSize++;
315 // Append result macro to pending list, checking for duplicates
316 // Do nothing if duplicate
317 inline void Macro_appendResultMacroToPendingList( unsigned int resultMacroIndex )
319 // Iterate through result macro pending list, making sure this macro hasn't been added yet
320 for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
322 // If duplicate found, do nothing
323 if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
327 // No duplicates found, add to pending list
328 macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
332 // Determine if long ResultMacro (more than 1 seqence element)
333 inline uint8_t Macro_isLongResultMacro( ResultMacro *macro )
335 // Check the second sequence combo length
336 // If non-zero return 1 (long sequence)
337 // 0 otherwise (short sequence)
338 return macro->guide[ macro->guide[0] * ResultGuideSize( (ResultGuide*)macro->guide ) ] > 0 ? 1 : 0;
342 // Votes on the given key vs. guide
343 inline TriggerMacroVote Macro_evalTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
345 // Depending on key type
346 switch ( guide->type )
350 // Depending on the state of the buffered key, make voting decision
352 if ( guide->scanCode != key->scanCode )
354 switch ( key->state )
356 // Wrong key, pressed, fail
358 return TriggerMacroVote_Fail;
360 // Wrong key, held or released, do not pass (no effect)
363 return TriggerMacroVote_DoNothing;
370 switch ( key->state )
372 // Correct key, pressed, possible passing
374 return TriggerMacroVote_Pass;
376 // Correct key, held, possible passing or release
378 return TriggerMacroVote_PassRelease;
380 // Correct key, released, possible release
382 return TriggerMacroVote_Release;
390 erro_print("LED State Type - Not implemented...");
395 erro_print("Analog State Type - Not implemented...");
398 // Invalid State Type
400 erro_print("Invalid State Type. This is a bug.");
404 // XXX Shouldn't reach here
405 return TriggerMacroVote_Invalid;
409 // Evaluate/Update TriggerMacro
410 inline TriggerMacroEval Macro_evalTriggerMacro( unsigned int triggerMacroIndex )
412 // Lookup TriggerMacro
413 TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
415 // Check if macro has finished and should be incremented sequence elements
416 if ( macro->state == TriggerMacro_Release )
418 macro->state = TriggerMacro_Waiting;
419 macro->pos = macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize;
422 // Current Macro position
423 unsigned int pos = macro->pos;
425 // Length of the combo being processed
426 uint8_t comboLength = macro->guide[ pos ];
428 // If no combo items are left, remove the TriggerMacro from the pending list
429 if ( comboLength == 0 )
431 return TriggerMacroEval_Remove;
434 // Iterate through the key buffer, comparing to each key in the combo
435 // If any of the pressed keys do not match, fail the macro
437 // The macro is waiting for input when in the TriggerMacro_Waiting state
438 // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
439 // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
440 // On scan after position increment, change to TriggerMacro_Waiting state
441 // TODO Add support for system LED states (NumLock, CapsLock, etc.)
442 // TODO Add support for analog key states
443 // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
444 // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
445 TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
446 for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
448 // Lookup key information
449 TriggerGuide *keyInfo = ¯oTriggerListBuffer[ key ];
451 // Iterate through the items in the combo, voting the on the key state
452 TriggerMacroVote vote = TriggerMacroVote_Invalid;
453 for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
455 // Assign TriggerGuide element (key type, state and scancode)
456 TriggerGuide *guide = (TriggerGuide*)(¯o->guide[ comboItem ]);
458 // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
459 // Also mask all of the non-passing votes
460 vote |= Macro_evalTriggerMacroVote( keyInfo, guide );
461 if ( vote >= TriggerMacroVote_Pass )
463 vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
468 // After voting, append to overall vote
472 // Decide new state of macro after voting
473 // Fail macro, remove from pending list
474 if ( overallVote & TriggerMacroVote_Fail )
476 return TriggerMacroEval_Remove;
478 // Do nothing, incorrect key is being held or released
479 else if ( overallVote & TriggerMacroVote_DoNothing )
481 // Just doing nothing :)
483 // If passing and in Waiting state, set macro state to Press
484 else if ( overallVote & TriggerMacroVote_Pass && macro->state == TriggerMacro_Waiting )
486 macro->state = TriggerMacro_Press;
488 // If in press state, and this is the final combo, send request for ResultMacro
489 // Check to see if the result macro only has a single element
490 // If this result macro has more than 1 key, only send once
491 // TODO Add option to have macro repeat rate
492 if ( macro->guide[ pos + comboLength ] == 0 )
494 // Long Macro, only send once (more than 1 sequence item)
495 // Short Macro (only 1 sequence item)
496 return Macro_isLongResultMacro( &ResultMacroList[ macro->result ] )
497 ? TriggerMacroEval_DoResult
498 : TriggerMacroEval_DoResultAndRemove;
502 // If ready for transition and in Press state, set to Waiting and increment combo position
503 // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
504 else if ( overallVote & TriggerMacroVote_Release && macro->state == TriggerMacro_Press )
506 macro->state = TriggerMacro_Release;
509 return TriggerMacroEval_DoNothing;
513 // Evaluate/Update ResultMacro
514 inline ResultMacroEval Macro_evalResultMacro( unsigned int resultMacroIndex )
516 // Lookup ResultMacro
517 ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
519 // Current Macro position
520 unsigned int pos = macro->pos;
522 // Length of combo being processed
523 uint8_t comboLength = macro->guide[ pos ];
525 // If no combo items are left, remove the ResultMacro from the pending list
526 if ( comboLength == 0 )
528 return ResultMacroEval_Remove;
531 // Function Counter, used to keep track of the combo items processed
532 unsigned int funcCount = 0;
534 // Combo Item Position within the guide
535 unsigned int comboItem = pos + 1;
537 // Iterate through the Result Combo
538 while ( funcCount < comboLength )
540 // Assign TriggerGuide element (key type, state and scancode)
541 ResultGuide *guide = (ResultGuide*)(¯o->guide[ pos ]);
543 // Do lookup on capability function
544 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
547 capability( macro->state, macro->stateType, &guide->args );
549 // Increment counters
551 comboItem += ResultGuideSize( (ResultGuide*)(¯o->guide[ comboItem ]) );
554 // Move to next item in the sequence
555 macro->pos = comboItem;
557 // If the ResultMacro is finished, it will be removed on the next iteration
558 return ResultMacroEval_DoNothing;
562 // Update pending trigger list
563 void Macro_updateTriggerMacroPendingList()
565 // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
566 for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
568 // Lookup Trigger List
569 unsigned int *triggerList = Macro_layerLookup( macroTriggerListBuffer[ key ].scanCode );
571 // Number of Triggers in list
572 unsigned int triggerListSize = triggerList[0];
574 // Iterate over triggerList to see if any TriggerMacros need to be added
575 // First item is the number of items in the TriggerList
576 for ( unsigned int macro = 1; macro < triggerListSize + 1; macro++ )
578 // Lookup trigger macro index
579 unsigned int triggerMacroIndex = triggerList[ macro ];
581 // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
582 // triggerList needs to be added
583 unsigned int pending = 0;
584 for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
586 // Stop scanning if the trigger macro index is found in the pending list
587 if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
591 // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
592 // Add it to the list
593 if ( pending == macroTriggerMacroPendingListSize )
595 macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
602 // Macro Procesing Loop
603 // Called once per USB buffer send
604 inline void Macro_process()
606 // Only do one round of macro processing between Output Module timer sends
607 if ( USBKeys_Sent != 0 )
610 // If the pause flag is set, only process if the step counter is non-zero
611 if ( macroPauseMode )
613 if ( macroStepCounter == 0 )
616 // Proceed, decrementing the step counter
620 // Update pending trigger list, before processing TriggerMacros
621 Macro_updateTriggerMacroPendingList();
623 // Tail pointer for macroTriggerMacroPendingList
624 // Macros must be explicitly re-added
625 unsigned int macroTriggerMacroPendingListTail = 0;
627 // Iterate through the pending TriggerMacros, processing each of them
628 for ( unsigned int macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
630 switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
632 // Trigger Result Macro (purposely falling through)
633 case TriggerMacroEval_DoResult:
634 // Append ResultMacro to PendingList
635 Macro_appendResultMacroToPendingList( TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ].result );
637 // Otherwise, just re-add
639 macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
642 // Trigger Result Macro and Remove (purposely falling through)
643 case TriggerMacroEval_DoResultAndRemove:
644 // Append ResultMacro to PendingList
645 Macro_appendResultMacroToPendingList( TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ].result );
647 // Remove Macro from Pending List, nothing to do, removing by default
648 case TriggerMacroEval_Remove:
653 // Update the macroTriggerMacroPendingListSize with the tail pointer
654 macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
657 // Tail pointer for macroResultMacroPendingList
658 // Macros must be explicitly re-added
659 unsigned int macroResultMacroPendingListTail = 0;
661 // Iterate through the pending ResultMacros, processing each of them
662 for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
664 switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
666 // Re-add macros to pending list
667 case ResultMacroEval_DoNothing:
669 macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
672 // Remove Macro from Pending List, nothing to do, removing by default
673 case ResultMacroEval_Remove:
678 // Update the macroResultMacroPendingListSize with the tail pointer
679 macroResultMacroPendingListSize = macroResultMacroPendingListTail;
681 // Signal buffer that we've used it
682 Scan_finishedWithMacro( macroTriggerListBufferSize );
684 // Reset TriggerList buffer
685 macroTriggerListBufferSize = 0;
687 // If Macro debug mode is set, clear the USB Buffer
688 if ( macroDebugMode )
690 USBKeys_Modifiers = 0;
696 inline void Macro_setup()
698 // Register Macro CLI dictionary
699 CLI_registerDictionary( macroCLIDict, macroCLIDictName );
701 // Disable Macro debug mode
704 // Disable Macro pause flag
707 // Set Macro step counter to zero
708 macroStepCounter = 0;
710 // Make sure macro trigger buffer is empty
711 macroTriggerListBufferSize = 0;
713 // Initialize TriggerMacro states
714 for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
716 TriggerMacroList[ macro ].result = 0;
717 TriggerMacroList[ macro ].pos = 0;
718 TriggerMacroList[ macro ].state = TriggerMacro_Waiting;
721 // Initialize ResultMacro states
722 for ( unsigned int macro = 0; macro < ResultMacroNum; macro++ )
724 ResultMacroList[ macro ].pos = 0;
725 ResultMacroList[ macro ].state = 0;
726 ResultMacroList[ macro ].stateType = 0;
731 // ----- CLI Command Functions -----
733 void cliFunc_capList( char* args )
736 info_msg("Capabilities List");
738 // Iterate through all of the capabilities and display them
739 for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
745 // Display/Lookup Capability Name (utilize debug mode of capability)
746 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
747 capability( 0xFF, 0xFF, 0 );
751 void cliFunc_capSelect( char* args )
753 // Parse code from argument
756 char* arg2Ptr = args;
758 // Total number of args to scan (must do a lookup if a keyboard capability is selected)
759 unsigned int totalArgs = 2; // Always at least two args
760 unsigned int cap = 0;
762 // Arguments used for keyboard capability function
763 unsigned int argSetCount = 0;
764 uint8_t *argSet = (uint8_t*)args;
767 for ( unsigned int c = 0; argSetCount < totalArgs; c++ )
770 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
772 // Stop processing args if no more are found
773 // Extra arguments are ignored
774 if ( *arg1Ptr == '\0' )
777 // For the first argument, choose the capability
778 if ( c == 0 ) switch ( arg1Ptr[0] )
780 // Keyboard Capability
782 // Determine capability index
783 cap = decToInt( &arg1Ptr[1] );
785 // Lookup the number of args
786 totalArgs += CapabilitiesList[ cap ].argCount;
790 // Because allocating memory isn't doable, and the argument count is arbitrary
791 // The argument pointer is repurposed as the argument list (much smaller anyways)
792 argSet[ argSetCount++ ] = (uint8_t)decToInt( arg1Ptr );
794 // Once all the arguments are prepared, call the keyboard capability function
795 if ( argSetCount == totalArgs )
797 // Indicate that the capability was called
802 printHex( argSet[0] );
804 printHex( argSet[1] );
806 printHex( argSet[2] );
809 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
810 capability( argSet[0], argSet[1], &argSet[2] );
815 void cliFunc_keyPress( char* args )
817 // Parse codes from arguments
820 char* arg2Ptr = args;
826 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
828 // Stop processing args if no more are found
829 if ( *arg1Ptr == '\0' )
832 // Ignore non-Scancode numbers
833 switch ( arg1Ptr[0] )
837 Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
843 void cliFunc_keyRelease( char* args )
845 // Parse codes from arguments
848 char* arg2Ptr = args;
854 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
856 // Stop processing args if no more are found
857 if ( *arg1Ptr == '\0' )
860 // Ignore non-Scancode numbers
861 switch ( arg1Ptr[0] )
865 Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
871 void cliFunc_layerList( char* args )
874 info_msg("Layer List");
876 // Iterate through all of the layers and display them
877 for ( unsigned int layer = 0; layer < LayerNum; layer++ )
883 // Display layer name
884 dPrint( LayerIndex[ layer ].name );
888 print(" \033[1m(default)\033[0m");
891 print( NL "\t\t Layer State: " );
892 printHex( LayerIndex[ layer ].state );
895 print(" Max Index: ");
896 printHex( LayerIndex[ layer ].max );
900 void cliFunc_layerState( char* args )
902 // Parse codes from arguments
905 char* arg2Ptr = args;
910 // Process first two args
911 for ( uint8_t c = 0; c < 2; c++ )
914 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
916 // Stop processing args if no more are found
917 if ( *arg1Ptr == '\0' )
922 // First argument (e.g. L1)
924 if ( arg1Ptr[0] != 'L' )
927 arg1 = (uint8_t)decToInt( &arg1Ptr[1] );
929 // Second argument (e.g. 4)
931 arg2 = (uint8_t)decToInt( arg1Ptr );
933 // Display operation (to indicate that it worked)
935 info_msg("Setting Layer L");
940 // Set the layer state
941 LayerIndex[ arg1 ].state = arg2;
947 void cliFunc_macroDebug( char* args )
949 // Toggle macro debug mode
950 macroDebugMode = macroDebugMode ? 0 : 1;
953 info_msg("Macro Debug Mode: ");
954 printInt8( macroDebugMode );
957 void cliFunc_macroList( char* args )
959 // Show available trigger macro indices
961 info_msg("Trigger Macros Range: T0 -> T");
962 printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
964 // Show available result macro indices
966 info_msg("Result Macros Range: R0 -> R");
967 printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
969 // Show Trigger to Result Macro Links
971 info_msg("Trigger : Result Macro Pairs");
972 for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
976 printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
978 printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
982 void cliFunc_macroProc( char* args )
984 // Toggle macro pause mode
985 macroPauseMode = macroPauseMode ? 0 : 1;
988 info_msg("Macro Processing Mode: ");
989 printInt8( macroPauseMode );
992 void macroDebugShowTrigger( unsigned int index )
994 // Only proceed if the macro exists
995 if ( index >= TriggerMacroNum )
998 // Trigger Macro Show
999 TriggerMacro *macro = &TriggerMacroList[ index ];
1002 info_msg("Trigger Macro Index: ");
1003 printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
1006 // Read the comboLength for combo in the sequence (sequence of combos)
1007 unsigned int pos = 0;
1008 uint8_t comboLength = macro->guide[ pos ];
1010 // Iterate through and interpret the guide
1011 while ( comboLength != 0 )
1013 // Initial position of the combo
1014 unsigned int comboPos = ++pos;
1016 // Iterate through the combo
1017 while ( pos < comboLength * TriggerGuideSize + comboPos )
1019 // Assign TriggerGuide element (key type, state and scancode)
1020 TriggerGuide *guide = (TriggerGuide*)(¯o->guide[ pos ]);
1022 // Display guide information about trigger key
1023 printHex( guide->scanCode );
1025 printHex( guide->type );
1027 printHex( guide->state );
1029 // Increment position
1030 pos += TriggerGuideSize;
1032 // Only show combo separator if there are combos left in the sequence element
1033 if ( pos < comboLength * TriggerGuideSize + comboPos )
1037 // Read the next comboLength
1038 comboLength = macro->guide[ pos ];
1040 // Only show sequence separator if there is another combo to process
1041 if ( comboLength != 0 )
1045 // Display current position
1046 print( NL "Position: " );
1047 printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
1049 // Display result macro index
1050 print( NL "Result Macro Index: " );
1051 printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
1054 void macroDebugShowResult( unsigned int index )
1056 // Only proceed if the macro exists
1057 if ( index >= ResultMacroNum )
1060 // Trigger Macro Show
1061 ResultMacro *macro = &ResultMacroList[ index ];
1064 info_msg("Result Macro Index: ");
1065 printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
1068 // Read the comboLength for combo in the sequence (sequence of combos)
1069 unsigned int pos = 0;
1070 uint8_t comboLength = macro->guide[ pos++ ];
1072 // Iterate through and interpret the guide
1073 while ( comboLength != 0 )
1075 // Function Counter, used to keep track of the combos processed
1076 unsigned int funcCount = 0;
1078 // Iterate through the combo
1079 while ( funcCount < comboLength )
1081 // Assign TriggerGuide element (key type, state and scancode)
1082 ResultGuide *guide = (ResultGuide*)(¯o->guide[ pos ]);
1084 // Display Function Index
1085 printHex( guide->index );
1088 // Display Function Ptr Address
1089 printHex( (unsigned int)CapabilitiesList[ guide->index ].func );
1092 // Display/Lookup Capability Name (utilize debug mode of capability)
1093 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
1094 capability( 0xFF, 0xFF, 0 );
1096 // Display Argument(s)
1098 for ( unsigned int arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
1100 // Arguments are only 8 bit values
1101 printHex( (&guide->args)[ arg ] );
1103 // Only show arg separator if there are args left
1104 if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
1109 // Increment position
1110 pos += ResultGuideSize( guide );
1112 // Increment function count
1115 // Only show combo separator if there are combos left in the sequence element
1116 if ( funcCount < comboLength )
1120 // Read the next comboLength
1121 comboLength = macro->guide[ pos++ ];
1123 // Only show sequence separator if there is another combo to process
1124 if ( comboLength != 0 )
1128 // Display current position
1129 print( NL "Position: " );
1130 printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
1132 // Display final trigger state/type
1133 print( NL "Final Trigger State (State/Type): " );
1134 printHex( macro->state );
1136 printHex( macro->stateType );
1139 void cliFunc_macroShow( char* args )
1141 // Parse codes from arguments
1144 char* arg2Ptr = args;
1150 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1152 // Stop processing args if no more are found
1153 if ( *arg1Ptr == '\0' )
1156 // Ignore invalid codes
1157 switch ( arg1Ptr[0] )
1159 // Indexed Trigger Macro
1161 macroDebugShowTrigger( decToInt( &arg1Ptr[1] ) );
1163 // Indexed Result Macro
1165 macroDebugShowResult( decToInt( &arg1Ptr[1] ) );
1171 void cliFunc_macroStep( char* args )
1173 // Parse number from argument
1174 // NOTE: Only first argument is used
1177 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
1179 // Set the macro step counter, negative int's are cast to uint
1180 macroStepCounter = (unsigned int)decToInt( arg1Ptr );