]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/PartialMap/macro.c
Adding layer fall-through lookup
[kiibohd-controller.git] / Macro / PartialMap / macro.c
1 /* Copyright (C) 2014 by Jacob Alexander
2  *
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.
7  *
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.
12  *
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/>.
15  */
16
17 // ----- Includes -----
18
19 // Compiler Includes
20 #include <Lib/MacroLib.h>
21
22 // Project Includes
23 #include <cli.h>
24 #include <led.h>
25 #include <print.h>
26 #include <scan_loop.h>
27 #include <output_com.h>
28
29 // Keymaps
30 #include "usb_hid.h"
31 #include <defaultMap.h>
32 #include "generatedKeymap.h" // TODO Use actual generated version
33
34 // Local Includes
35 #include "macro.h"
36
37
38
39 // ----- Function Declarations -----
40
41 void cliFunc_capList   ( char* args );
42 void cliFunc_capSelect ( 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 );
52
53
54
55 // ----- Variables -----
56
57 // Macro Module command dictionary
58 char*       macroCLIDictName = "Macro Module Commands";
59 CLIDictItem macroCLIDict[] = {
60         { "capList",     "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
61         { "capSelect",   "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B", cliFunc_capSelect },
62         { "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 },
63         { "keyRelease",  "Release a key-press from the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
64         { "layerList",   "List available layers.", cliFunc_layerList },
65         { "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 },
66         { "macroDebug",  "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
67         { "macroList",   "List the defined trigger and result macros.", cliFunc_macroList },
68         { "macroProc",   "Pause/Resume macro processing.", cliFunc_macroProc },
69         { "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 },
70         { "macroStep",   "Do N macro processing steps. Defaults to 1.", cliFunc_macroStep },
71         { 0, 0, 0 } // Null entry for dictionary end
72 };
73
74
75 // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
76 uint8_t macroDebugMode = 0;
77
78 // Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
79 uint8_t macroPauseMode = 0;
80
81 // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
82 unsigned int macroStepCounter = 0;
83
84
85 // Key Trigger List Buffer
86 TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
87 uint8_t macroTriggerListBufferSize = 0;
88
89 // Pending Trigger Macro Index List
90 //  * Any trigger macros that need processing from a previous macro processing loop
91 // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
92 //       Possibly could be calculated by the KLL compiler
93 // XXX It may be possible to calculate the worst case using the KLL compiler
94 unsigned int macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
95 unsigned int macroTriggerMacroPendingListSize = 0;
96
97 // Layer Index Stack
98 //  * When modifying layer state and the state is non-0x0, the stack must be adjusted
99 unsigned int macroLayerIndexStack[ LayerNum ] = { 0 };
100 unsigned int macroLayerIndexStackSize = 0;
101
102 // Pending Result Macro Index List
103 //  * Any result macro that needs processing from a previous macro processing loop
104 unsigned int macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
105 unsigned int macroResultMacroPendingListSize = 0;
106
107
108
109 // ----- Functions -----
110
111 // Looks up the trigger list for the given scan code (from the active layer)
112 // NOTE: Calling function must handle the NULL pointer case
113 unsigned int *Macro_layerLookup( uint8_t scanCode )
114 {
115         // If no trigger macro is defined at the given layer, fallthrough to the next layer
116         for ( unsigned int layer = 0; layer < macroLayerIndexStackSize; layer++ )
117         {
118                 // Lookup layer
119                 unsigned int **map = LayerIndex[ macroLayerIndexStack[ layer ] ].triggerMap;
120
121                 // Determine if layer has key defined
122                 if ( map != 0 && *map[ scanCode ] != 0 )
123                         return map[ scanCode ];
124         }
125
126         // Do lookup on default layer
127         unsigned int **map = LayerIndex[0].triggerMap;
128
129         // Determine if layer has key defined
130         if ( map == 0 && *map[ scanCode ] == 0 )
131         {
132                 erro_msg("Scan Code has no defined Trigger Macro: ");
133                 printHex( scanCode );
134                 return 0;
135         }
136
137         // Return lookup result
138         return map[ scanCode ];
139 }
140
141
142 // Update the scancode key state
143 // States:
144 //   * 0x00 - Off
145 //   * 0x01 - Pressed
146 //   * 0x02 - Held
147 //   * 0x03 - Released
148 //   * 0x04 - Unpressed (this is currently ignored)
149 inline void Macro_keyState( uint8_t scanCode, uint8_t state )
150 {
151         // Only add to macro trigger list if one of three states
152         switch ( state )
153         {
154         case 0x01: // Pressed
155         case 0x02: // Held
156         case 0x03: // Released
157                 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
158                 macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
159                 macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x00; // Normal key
160                 macroTriggerListBufferSize++;
161                 break;
162         }
163 }
164
165
166 // Update the scancode analog state
167 // States:
168 //   * 0x00      - Off
169 //   * 0x01      - Released
170 //   * 0x02-0xFF - Analog value (low to high)
171 inline void Macro_analogState( uint8_t scanCode, uint8_t state )
172 {
173         // Only add to macro trigger list if non-off
174         if ( state != 0x00 )
175         {
176                 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
177                 macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
178                 macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x02; // Analog key
179                 macroTriggerListBufferSize++;
180         }
181 }
182
183
184 // Update led state
185 // States:
186 //   * 0x00 - Off
187 //   * 0x01 - On
188 inline void Macro_ledState( uint8_t ledCode, uint8_t state )
189 {
190         // Only add to macro trigger list if non-off
191         if ( state != 0x00 )
192         {
193                 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
194                 macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
195                 macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x01; // LED key
196                 macroTriggerListBufferSize++;
197         }
198 }
199
200
201 // Evaluate/Update the TriggerMacro
202 void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
203 {
204         // Which combo in the sequence is being evaluated
205         unsigned int comboPos = triggerMacro->pos;
206
207         // If combo length is more than 1, cancel trigger macro if an incorrect key is found
208         uint8_t comboLength = triggerMacro->guide[ comboPos ];
209
210         // Iterate over list of keys currently pressed
211         for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed += 2 )
212         {
213                 // Compare with keys in combo
214                 for ( unsigned int comboKey = 0; comboKey < comboLength; comboKey++ )
215                 {
216                         // Lookup key in combo
217                         uint8_t guideKey = triggerMacro->guide[ comboPos + comboKey + 2 ]; // TODO Only Press/Hold/Release atm
218
219                         // Sequence Case
220                         if ( comboLength == 1 )
221                         {
222                                 // If key matches and only 1 key pressed, increment the TriggerMacro combo position
223                                 if ( guideKey == macroTriggerListBuffer[ keyPressed ].scanCode && macroTriggerListBufferSize == 1 )
224                                 {
225                                         triggerMacro->pos += comboLength * 2 + 1;
226                                         // TODO check if TriggerMacro is finished, register ResultMacro
227                                         return;
228                                 }
229
230                                 // If key does not match or more than 1 key pressed, reset the TriggerMacro combo position
231                                 triggerMacro->pos = 0;
232                                 return;
233                         }
234                         // Combo Case
235                         else
236                         {
237                                 // TODO
238                         }
239                 }
240         }
241 }
242
243
244
245
246 /*
247 inline void Macro_bufferAdd( uint8_t byte )
248 {
249         // Make sure we haven't overflowed the key buffer
250         // Default function for adding keys to the KeyIndex_Buffer, does a DefaultMap_Lookup
251         if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
252         {
253                 uint8_t key = DefaultMap_Lookup[byte];
254                 for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
255                 {
256                         // Key already in the buffer
257                         if ( KeyIndex_Buffer[c] == key )
258                                 return;
259                 }
260
261                 // Add to the buffer
262                 KeyIndex_Buffer[KeyIndex_BufferUsed++] = key;
263         }
264 }
265
266 inline void Macro_bufferRemove( uint8_t byte )
267 {
268         uint8_t key = DefaultMap_Lookup[byte];
269
270         // Check for the released key, and shift the other keys lower on the buffer
271         for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
272         {
273                 // Key to release found
274                 if ( KeyIndex_Buffer[c] == key )
275                 {
276                         // Shift keys from c position
277                         for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
278                                 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
279
280                         // Decrement Buffer
281                         KeyIndex_BufferUsed--;
282
283                         return;
284                 }
285         }
286
287         // Error case (no key to release)
288         erro_msg("Could not find key to release: ");
289         printHex( key );
290 }
291 */
292
293 inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
294 {
295 }
296
297 inline void Macro_process()
298 {
299         // Only do one round of macro processing between Output Module timer sends
300         if ( USBKeys_Sent != 0 )
301                 return;
302
303         // If the pause flag is set, only process if the step counter is non-zero
304         if ( macroPauseMode && macroStepCounter == 0 )
305         {
306                 return;
307         }
308         // Proceed, decrementing the step counter
309         else
310         {
311                 macroStepCounter--;
312         }
313
314         // Loop through macro trigger buffer
315         for ( uint8_t index = 0; index < macroTriggerListBufferSize; index++ )
316         {
317                 // Get scanCode, first item of macroTriggerListBuffer pairs
318                 uint8_t scanCode = macroTriggerListBuffer[ index ].scanCode;
319
320                 // Lookup trigger list for this key
321                 unsigned int *triggerList = Macro_layerLookup( scanCode );
322
323                 // The first element is the length of the trigger list
324                 unsigned int triggerListSize = triggerList[0];
325
326                 // Loop through the trigger list
327                 for ( unsigned int trigger = 0; trigger < triggerListSize; trigger++ )
328                 {
329                         // Lookup TriggerMacro
330                         TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
331
332                         // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
333                         uint8_t state = macroTriggerListBuffer[ index ].state;
334
335                         // Evaluate Macro
336                         Macro_evalTriggerMacro( triggerMacro );
337                 }
338         }
339
340
341
342
343
344         /* TODO
345         // Loop through input buffer
346         for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
347         {
348                 //print(" KEYS: ");
349                 //printInt8( KeyIndex_BufferUsed );
350                 // Get the keycode from the buffer
351                 uint8_t key = KeyIndex_Buffer[index];
352
353                 // Set the modifier bit if this key is a modifier
354                 if ( (key & KEY_LCTRL) == KEY_LCTRL ) // AND with 0xE0
355                 {
356                         USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
357
358                         // Modifier processed, move on to the next key
359                         continue;
360                 }
361
362                 // Too many keys
363                 if ( USBKeys_Sent >= USBKeys_MaxSize )
364                 {
365                         warn_msg("USB Key limit reached");
366                         errorLED( 1 );
367                         break;
368                 }
369
370                 // Allow ignoring keys with 0's
371                 if ( key != 0 )
372                 {
373                         USBKeys_Array[USBKeys_Sent++] = key;
374                 }
375                 else
376                 {
377                         // Key was not mapped
378                         erro_msg( "Key not mapped... - " );
379                         printHex( key );
380                         errorLED( 1 );
381                 }
382         }
383         */
384
385         // Signal buffer that we've used it
386         Scan_finishedWithBuffer( KeyIndex_BufferUsed );
387
388         // If Macro debug mode is set, clear the USB Buffer
389         if ( macroDebugMode )
390         {
391                 USBKeys_Modifiers = 0;
392                 USBKeys_Sent = 0;
393         }
394 }
395
396 inline void Macro_setup()
397 {
398         // Register Macro CLI dictionary
399         CLI_registerDictionary( macroCLIDict, macroCLIDictName );
400
401         // Disable Macro debug mode
402         macroDebugMode = 0;
403
404         // Disable Macro pause flag
405         macroPauseMode = 0;
406
407         // Set Macro step counter to zero
408         macroStepCounter = 0;
409
410         // Make sure macro trigger buffer is empty
411         macroTriggerListBufferSize = 0;
412 }
413
414
415 // ----- CLI Command Functions -----
416
417 void cliFunc_capList( char* args )
418 {
419         print( NL );
420         info_msg("Capabilities List");
421
422         // Iterate through all of the capabilities and display them
423         for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
424         {
425                 print( NL "\t" );
426                 printHex( cap );
427                 print(" - ");
428
429                 // Display/Lookup Capability Name (utilize debug mode of capability)
430                 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
431                 capability( 0xFF, 0xFF, 0 );
432         }
433 }
434
435 void cliFunc_capSelect( char* args )
436 {
437         // Parse code from argument
438         char* curArgs;
439         char* arg1Ptr;
440         char* arg2Ptr = args;
441
442         // Total number of args to scan (must do a lookup if a keyboard capability is selected)
443         unsigned int totalArgs = 2; // Always at least two args
444         unsigned int cap = 0;
445
446         // Arguments used for keyboard capability function
447         unsigned int argSetCount = 0;
448         uint8_t *argSet = (uint8_t*)args;
449
450         // Process all args
451         for ( unsigned int c = 0; argSetCount < totalArgs; c++ )
452         {
453                 curArgs = arg2Ptr;
454                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
455
456                 // Stop processing args if no more are found
457                 // Extra arguments are ignored
458                 if ( *arg1Ptr == '\0' )
459                         break;
460
461                 // For the first argument, choose the capability
462                 if ( c == 0 ) switch ( arg1Ptr[0] )
463                 {
464                 // Keyboard Capability
465                 case 'K':
466                         // Determine capability index
467                         cap = decToInt( &arg1Ptr[1] );
468
469                         // Lookup the number of args
470                         totalArgs += CapabilitiesList[ cap ].argCount;
471                         continue;
472                 }
473
474                 // Because allocating memory isn't doable, and the argument count is arbitrary
475                 // The argument pointer is repurposed as the argument list (much smaller anyways)
476                 argSet[ argSetCount++ ] = (uint8_t)decToInt( arg1Ptr );
477
478                 // Once all the arguments are prepared, call the keyboard capability function
479                 if ( argSetCount == totalArgs )
480                 {
481                         // Indicate that the capability was called
482                         print( NL );
483                         info_msg("K");
484                         printInt8( cap );
485                         print(" - ");
486                         printHex( argSet[0] );
487                         print(" - ");
488                         printHex( argSet[1] );
489                         print(" - ");
490                         printHex( argSet[2] );
491                         print( "..." NL );
492
493                         void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
494                         capability( argSet[0], argSet[1], &argSet[2] );
495                 }
496         }
497 }
498
499 void cliFunc_keyPress( char* args )
500 {
501         // Parse codes from arguments
502         char* curArgs;
503         char* arg1Ptr;
504         char* arg2Ptr = args;
505
506         // Process all args
507         for ( ;; )
508         {
509                 curArgs = arg2Ptr;
510                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
511
512                 // Stop processing args if no more are found
513                 if ( *arg1Ptr == '\0' )
514                         break;
515
516                 // Ignore non-Scancode numbers
517                 switch ( arg1Ptr[0] )
518                 {
519                 // Scancode
520                 case 'S':
521                         Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
522                         break;
523                 }
524         }
525 }
526
527 void cliFunc_keyRelease( char* args )
528 {
529         // Parse codes from arguments
530         char* curArgs;
531         char* arg1Ptr;
532         char* arg2Ptr = args;
533
534         // Process all args
535         for ( ;; )
536         {
537                 curArgs = arg2Ptr;
538                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
539
540                 // Stop processing args if no more are found
541                 if ( *arg1Ptr == '\0' )
542                         break;
543
544                 // Ignore non-Scancode numbers
545                 switch ( arg1Ptr[0] )
546                 {
547                 // Scancode
548                 case 'S':
549                         Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
550                         break;
551                 }
552         }
553 }
554
555 void cliFunc_layerList( char* args )
556 {
557         print( NL );
558         info_msg("Layer List");
559
560         // Iterate through all of the layers and display them
561         for ( unsigned int layer = 0; layer < LayerNum; layer++ )
562         {
563                 print( NL "\t" );
564                 printHex( layer );
565                 print(" - ");
566
567                 // Display layer name
568                 dPrint( LayerIndex[ layer ].name );
569
570                 // Default map
571                 if ( layer == 0 )
572                         print(" \033[1m(default)\033[0m");
573
574                 // Layer State
575                 print( NL "\t\t Layer State: " );
576                 printHex( LayerIndex[ layer ].state );
577
578                 // Max Index
579                 print(" Max Index: ");
580                 printHex( LayerIndex[ layer ].max );
581         }
582 }
583
584 void cliFunc_layerState( char* args )
585 {
586         // Parse codes from arguments
587         char* curArgs;
588         char* arg1Ptr;
589         char* arg2Ptr = args;
590
591         uint8_t arg1 = 0;
592         uint8_t arg2 = 0;
593
594         // Process first two args
595         for ( uint8_t c = 0; c < 2; c++ )
596         {
597                 curArgs = arg2Ptr;
598                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
599
600                 // Stop processing args if no more are found
601                 if ( *arg1Ptr == '\0' )
602                         break;
603
604                 switch ( c )
605                 {
606                 // First argument (e.g. L1)
607                 case 0:
608                         if ( arg1Ptr[0] != 'L' )
609                                 return;
610
611                         arg1 = (uint8_t)decToInt( &arg1Ptr[1] );
612                         break;
613                 // Second argument (e.g. 4)
614                 case 1:
615                         arg2 = (uint8_t)decToInt( arg1Ptr );
616
617                         // Display operation (to indicate that it worked)
618                         print( NL );
619                         info_msg("Setting Layer L");
620                         printInt8( arg1 );
621                         print(" to - ");
622                         printHex( arg2 );
623
624                         // Set the layer state
625                         LayerIndex[ arg1 ].state = arg2;
626                         break;
627                 }
628         }
629 }
630
631 void cliFunc_macroDebug( char* args )
632 {
633         // Toggle macro debug mode
634         macroDebugMode = macroDebugMode ? 0 : 1;
635
636         print( NL );
637         info_msg("Macro Debug Mode: ");
638         printInt8( macroDebugMode );
639 }
640
641 void cliFunc_macroList( char* args )
642 {
643         // Show available trigger macro indices
644         print( NL );
645         info_msg("Trigger Macros Range: T0 -> T");
646         printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
647
648         // Show available result macro indices
649         print( NL );
650         info_msg("Result  Macros Range: R0 -> R");
651         printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
652
653         // Show Trigger to Result Macro Links
654         print( NL );
655         info_msg("Trigger : Result Macro Pairs");
656         for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
657         {
658                 print( NL );
659                 print("\tT");
660                 printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
661                 print(" : R");
662                 printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
663         }
664 }
665
666 void cliFunc_macroProc( char* args )
667 {
668         // Toggle macro pause mode
669         macroPauseMode = macroPauseMode ? 0 : 1;
670
671         print( NL );
672         info_msg("Macro Processing Mode: ");
673         printInt8( macroPauseMode );
674 }
675
676 void macroDebugShowTrigger( unsigned int index )
677 {
678         // Only proceed if the macro exists
679         if ( index >= TriggerMacroNum )
680                 return;
681
682         // Trigger Macro Show
683         TriggerMacro *macro = &TriggerMacroList[ index ];
684
685         print( NL );
686         info_msg("Trigger Macro Index: ");
687         printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
688         print( NL );
689
690         // Read the comboLength for combo in the sequence (sequence of combos)
691         unsigned int pos = 0;
692         uint8_t comboLength = macro->guide[ pos ];
693
694         // Iterate through and interpret the guide
695         while ( comboLength != 0 )
696         {
697                 // Initial position of the combo
698                 unsigned int comboPos = ++pos;
699
700                 // Iterate through the combo
701                 while ( pos < comboLength * TriggerGuideSize + comboPos )
702                 {
703                         // Assign TriggerGuide element (key type, state and scancode)
704                         TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
705
706                         // Display guide information about trigger key
707                         printHex( guide->scanCode );
708                         print("|");
709                         printHex( guide->type );
710                         print("|");
711                         printHex( guide->state );
712
713                         // Increment position
714                         pos += TriggerGuideSize;
715
716                         // Only show combo separator if there are combos left in the sequence element
717                         if ( pos < comboLength * TriggerGuideSize + comboPos )
718                                 print("+");
719                 }
720
721                 // Read the next comboLength
722                 comboLength = macro->guide[ pos ];
723
724                 // Only show sequence separator if there is another combo to process
725                 if ( comboLength != 0 )
726                         print(";");
727         }
728
729         // Display current position
730         print( NL "Position: " );
731         printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
732
733         // Display result macro index
734         print( NL "Result Macro Index: " );
735         printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
736 }
737
738 void macroDebugShowResult( unsigned int index )
739 {
740         // Only proceed if the macro exists
741         if ( index >= ResultMacroNum )
742                 return;
743
744         // Trigger Macro Show
745         ResultMacro *macro = &ResultMacroList[ index ];
746
747         print( NL );
748         info_msg("Result Macro Index: ");
749         printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
750         print( NL );
751
752         // Read the comboLength for combo in the sequence (sequence of combos)
753         unsigned int pos = 0;
754         uint8_t comboLength = macro->guide[ pos++ ];
755
756         // Iterate through and interpret the guide
757         while ( comboLength != 0 )
758         {
759                 // Function Counter, used to keep track of the combos processed
760                 unsigned int funcCount = 0;
761
762                 // Iterate through the combo
763                 while ( funcCount < comboLength )
764                 {
765                         // Assign TriggerGuide element (key type, state and scancode)
766                         ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
767
768                         // Display Function Index
769                         printHex( guide->index );
770                         print("|");
771
772                         // Display Function Ptr Address
773                         printHex( (unsigned int)CapabilitiesList[ guide->index ].func );
774                         print("|");
775
776                         // Display/Lookup Capability Name (utilize debug mode of capability)
777                         void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
778                         capability( 0xFF, 0xFF, 0 );
779
780                         // Display Argument(s)
781                         print("(");
782                         for ( unsigned int arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
783                         {
784                                 // Arguments are only 8 bit values
785                                 printHex( (&guide->args)[ arg ] );
786
787                                 // Only show arg separator if there are args left
788                                 if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
789                                         print(",");
790                         }
791                         print(")");
792
793                         // Increment position
794                         pos += ResultGuideSize( guide );
795
796                         // Increment function count
797                         funcCount++;
798
799                         // Only show combo separator if there are combos left in the sequence element
800                         if ( funcCount < comboLength )
801                                 print("+");
802                 }
803
804                 // Read the next comboLength
805                 comboLength = macro->guide[ pos++ ];
806
807                 // Only show sequence separator if there is another combo to process
808                 if ( comboLength != 0 )
809                         print(";");
810         }
811
812         // Display current position
813         print( NL "Position: " );
814         printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
815
816         // Display final trigger state/type
817         print( NL "Final Trigger State (State/Type): " );
818         printHex( macro->state );
819         print("/");
820         printHex( macro->stateType );
821 }
822
823 void cliFunc_macroShow( char* args )
824 {
825         // Parse codes from arguments
826         char* curArgs;
827         char* arg1Ptr;
828         char* arg2Ptr = args;
829
830         // Process all args
831         for ( ;; )
832         {
833                 curArgs = arg2Ptr;
834                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
835
836                 // Stop processing args if no more are found
837                 if ( *arg1Ptr == '\0' )
838                         break;
839
840                 // Ignore invalid codes
841                 switch ( arg1Ptr[0] )
842                 {
843                 // Indexed Trigger Macro
844                 case 'T':
845                         macroDebugShowTrigger( decToInt( &arg1Ptr[1] ) );
846                         break;
847                 // Indexed Result Macro
848                 case 'R':
849                         macroDebugShowResult( decToInt( &arg1Ptr[1] ) );
850                         break;
851                 }
852         }
853 }
854
855 void cliFunc_macroStep( char* args )
856 {
857         // Parse number from argument
858         //  NOTE: Only first argument is used
859         char* arg1Ptr;
860         char* arg2Ptr;
861         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
862
863         // Set the macro step counter, negative int's are cast to uint
864         macroStepCounter = (unsigned int)decToInt( arg1Ptr );
865 }
866