]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/PartialMap/macro.c
Updating PartialLayer code to support varying counter widths.
[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
28 // Keymaps
29 #include "usb_hid.h"
30 //#include <defaultMap.h>
31 #include "templateKeymap.h" // TODO Use actual generated version
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_keyHold   ( char* args );
44 void cliFunc_keyPress  ( char* args );
45 void cliFunc_keyRelease( char* args );
46 void cliFunc_layerList ( char* args );
47 void cliFunc_layerState( char* args );
48 void cliFunc_macroDebug( char* args );
49 void cliFunc_macroList ( char* args );
50 void cliFunc_macroProc ( char* args );
51 void cliFunc_macroShow ( char* args );
52 void cliFunc_macroStep ( char* args );
53
54
55
56 // ----- Enums -----
57
58 // Bit positions are important, passes (correct key) always trump incorrect key votes
59 typedef enum TriggerMacroVote {
60         TriggerMacroVote_Release          = 0x10, // Correct key
61         TriggerMacroVote_PassRelease      = 0x18, // Correct key (both pass and release)
62         TriggerMacroVote_Pass             = 0x8,  // Correct key
63         TriggerMacroVote_DoNothingRelease = 0x4,  // Incorrect key
64         TriggerMacroVote_DoNothing        = 0x2,  // Incorrect key
65         TriggerMacroVote_Fail             = 0x1,  // Incorrect key
66         TriggerMacroVote_Invalid          = 0x0,  // Invalid state
67 } TriggerMacroVote;
68
69 typedef enum TriggerMacroEval {
70         TriggerMacroEval_DoNothing,
71         TriggerMacroEval_DoResult,
72         TriggerMacroEval_DoResultAndRemove,
73         TriggerMacroEval_Remove,
74 } TriggerMacroEval;
75
76 typedef enum ResultMacroEval {
77         ResultMacroEval_DoNothing,
78         ResultMacroEval_Remove,
79 } ResultMacroEval;
80
81
82
83 // ----- Variables -----
84
85 // Macro Module command dictionary
86 const char macroCLIDictName[] = "Macro Module Commands";
87 const CLIDictItem macroCLIDict[] = {
88         { "capList",     "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
89         { "capSelect",   "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B", cliFunc_capSelect },
90         { "keyHold",     "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyHold },
91         { "keyPress",    "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
92         { "keyRelease",  "Send key-release event to macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
93         { "layerList",   "List available layers.", cliFunc_layerList },
94         { "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 },
95         { "macroDebug",  "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
96         { "macroList",   "List the defined trigger and result macros.", cliFunc_macroList },
97         { "macroProc",   "Pause/Resume macro processing.", cliFunc_macroProc },
98         { "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 },
99         { "macroStep",   "Do N macro processing steps. Defaults to 1.", cliFunc_macroStep },
100         { 0, 0, 0 } // Null entry for dictionary end
101 };
102
103
104 // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
105 uint8_t macroDebugMode = 0;
106
107 // Macro pause flag - If set, the macro module pauses processing, unless unset, or the step counter is non-zero
108 uint8_t macroPauseMode = 0;
109
110 // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
111 uint16_t macroStepCounter = 0;
112
113
114 // Key Trigger List Buffer
115 TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
116 uint8_t macroTriggerListBufferSize = 0;
117
118 // Pending Trigger Macro Index List
119 //  * Any trigger macros that need processing from a previous macro processing loop
120 // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
121 //       Possibly could be calculated by the KLL compiler
122 // XXX It may be possible to calculate the worst case using the KLL compiler
123 uint16_t macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
124 uint16_t macroTriggerMacroPendingListSize = 0;
125
126 // Layer Index Stack
127 //  * When modifying layer state and the state is non-0x0, the stack must be adjusted
128 uint16_t macroLayerIndexStack[ LayerNum + 1 ] = { 0 };
129 uint16_t macroLayerIndexStackSize = 0;
130
131 // Pending Result Macro Index List
132 //  * Any result macro that needs processing from a previous macro processing loop
133 uint16_t macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
134 uint16_t macroResultMacroPendingListSize = 0;
135
136
137
138 // ----- Capabilities -----
139
140 // Sets the given layer with the specified layerState
141 void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t layerState )
142 {
143         // Is layer in the LayerIndexStack?
144         uint8_t inLayerIndexStack = 0;
145         uint16_t stackItem = 0;
146         while ( stackItem < macroLayerIndexStackSize )
147         {
148                 // Flag if layer is already in the LayerIndexStack
149                 if ( macroLayerIndexStack[ stackItem ] == layer )
150                 {
151                         inLayerIndexStack = 1;
152                         break;
153                 }
154
155                 // Increment to next item
156                 stackItem++;
157         }
158
159         // Toggle Layer State Byte
160         if ( LayerIndex[ layer ].state & layerState )
161         {
162                 // Unset
163                 LayerIndex[ layer ].state &= ~layerState;
164         }
165         else
166         {
167                 // Set
168                 LayerIndex[ layer ].state |= layerState;
169         }
170
171         // If the layer was not in the LayerIndexStack add it
172         if ( !inLayerIndexStack )
173         {
174                 macroLayerIndexStack[ macroLayerIndexStackSize++ ] = layer;
175         }
176
177         // If the layer is in the LayerIndexStack and the state is 0x00, remove
178         if ( LayerIndex[ layer ].state == 0x00 && inLayerIndexStack )
179         {
180                 // Remove the layer from the LayerIndexStack
181                 // Using the already positioned stackItem variable from the loop above
182                 while ( stackItem < macroLayerIndexStackSize )
183                 {
184                         macroLayerIndexStack[ stackItem ] = macroLayerIndexStack[ stackItem + 1 ];
185                         stackItem++;
186                 }
187
188                 // Reduce LayerIndexStack size
189                 macroLayerIndexStackSize--;
190         }
191 }
192
193 // Modifies the specified Layer control byte
194 // Argument #1: Layer Index -> uint16_t
195 // Argument #2: Layer State -> uint8_t
196 void Macro_layerState_capability( uint8_t state, uint8_t stateType, uint8_t *args )
197 {
198         // Display capability name
199         if ( stateType == 0xFF && state == 0xFF )
200         {
201                 print("Macro_layerState(layerIndex,layerState)");
202                 return;
203         }
204
205         // Only use capability on press or release
206         // TODO Analog
207         // XXX This may cause issues, might be better to implement state table here to decide -HaaTa
208         if ( stateType == 0x00 && state == 0x02 ) // Hold condition
209                 return;
210
211         // Get layer index from arguments
212         // Cast pointer to uint8_t to uint16_t then access that memory location
213         uint16_t layer = *(uint16_t*)(&args[0]);
214
215         // Get layer toggle byte
216         uint8_t layerState = args[ sizeof(uint16_t) ];
217
218         Macro_layerState( state, stateType, layer, layerState );
219 }
220
221
222 // Latches given layer
223 // Argument #1: Layer Index -> uint16_t
224 void Macro_layerLatch_capability( uint8_t state, uint8_t stateType, uint8_t *args )
225 {
226         // Display capability name
227         if ( stateType == 0xFF && state == 0xFF )
228         {
229                 print("Macro_layerLatch(layerIndex)");
230                 return;
231         }
232
233         // Only use capability on press
234         // TODO Analog
235         // XXX To make sense, this code be on press or release. Or it could even be a sticky shift (why? dunno) -HaaTa
236         if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
237                 return;
238
239         // Get layer index from arguments
240         // Cast pointer to uint8_t to uint16_t then access that memory location
241         uint16_t layer = *(uint16_t*)(&args[0]);
242
243         Macro_layerState( state, stateType, layer, 0x02 );
244 }
245
246
247 // Locks given layer
248 // Argument #1: Layer Index -> uint16_t
249 void Macro_layerLock_capability( uint8_t state, uint8_t stateType, uint8_t *args )
250 {
251         // Display capability name
252         if ( stateType == 0xFF && state == 0xFF )
253         {
254                 print("Macro_layerLock(layerIndex)");
255                 return;
256         }
257
258         // Only use capability on press
259         // TODO Analog
260         // XXX Could also be on release, but that's sorta dumb -HaaTa
261         if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
262                 return;
263
264         // Get layer index from arguments
265         // Cast pointer to uint8_t to uint16_t then access that memory location
266         uint16_t layer = *(uint16_t*)(&args[0]);
267
268         Macro_layerState( state, stateType, layer, 0x04 );
269 }
270
271
272 // Shifts given layer
273 // Argument #1: Layer Index -> uint16_t
274 void Macro_layerShift_capability( uint8_t state, uint8_t stateType, uint8_t *args )
275 {
276         // Display capability name
277         if ( stateType == 0xFF && state == 0xFF )
278         {
279                 print("Macro_layerShift(layerIndex)");
280                 return;
281         }
282
283         // Only use capability on press or release
284         // TODO Analog
285         if ( stateType == 0x00 && ( state == 0x00 || state == 0x02 ) ) // Only pass press or release conditions
286                 return;
287
288         // Get layer index from arguments
289         // Cast pointer to uint8_t to uint16_t then access that memory location
290         uint16_t layer = *(uint16_t*)(&args[0]);
291
292         Macro_layerState( state, stateType, layer, 0x01 );
293 }
294
295
296
297 // ----- Functions -----
298
299 // Looks up the trigger list for the given scan code (from the active layer)
300 // NOTE: Calling function must handle the NULL pointer case
301 nat_ptr_t *Macro_layerLookup( uint8_t scanCode )
302 {
303         // If no trigger macro is defined at the given layer, fallthrough to the next layer
304         for ( uint16_t layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ )
305         {
306                 // Lookup Layer
307                 Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
308
309                 // Check if latch has been pressed for this layer
310                 // XXX Regardless of whether a key is found, the latch is removed on first lookup
311                 uint8_t latch = layer->state & 0x02;
312                 if ( latch )
313                 {
314                         layer->state &= ~0x02;
315                 }
316
317                 // Only use layer, if state is valid
318                 // XOR each of the state bits
319                 // If only two are enabled, do not use this state
320                 if ( (layer->state & 0x01) ^ (latch>>1) ^ ((layer->state & 0x04)>>2) )
321                 {
322                         // Lookup layer
323                         nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap;
324
325                         // Determine if layer has key defined
326                         if ( map != 0 && *map[ scanCode ] != 0 )
327                                 return map[ scanCode ];
328                 }
329         }
330
331         // Do lookup on default layer
332         nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
333
334         // Determine if layer has key defined
335         if ( map == 0 && *map[ scanCode ] == 0 )
336         {
337                 erro_msg("Scan Code has no defined Trigger Macro: ");
338                 printHex( scanCode );
339                 return 0;
340         }
341
342         // Return lookup result
343         return map[ scanCode ];
344 }
345
346
347 // Update the scancode key state
348 // States:
349 //   * 0x00 - Off
350 //   * 0x01 - Pressed
351 //   * 0x02 - Held
352 //   * 0x03 - Released
353 //   * 0x04 - Unpressed (this is currently ignored)
354 inline void Macro_keyState( uint8_t scanCode, uint8_t state )
355 {
356         // Only add to macro trigger list if one of three states
357         switch ( state )
358         {
359         case 0x01: // Pressed
360         case 0x02: // Held
361         case 0x03: // Released
362                 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
363                 macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
364                 macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x00; // Normal key
365                 macroTriggerListBufferSize++;
366                 break;
367         }
368 }
369
370
371 // Update the scancode analog state
372 // States:
373 //   * 0x00      - Off
374 //   * 0x01      - Released
375 //   * 0x02-0xFF - Analog value (low to high)
376 inline void Macro_analogState( uint8_t scanCode, uint8_t state )
377 {
378         // Only add to macro trigger list if non-off
379         if ( state != 0x00 )
380         {
381                 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
382                 macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
383                 macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x02; // Analog key
384                 macroTriggerListBufferSize++;
385         }
386 }
387
388
389 // Update led state
390 // States:
391 //   * 0x00 - Off
392 //   * 0x01 - On
393 inline void Macro_ledState( uint8_t ledCode, uint8_t state )
394 {
395         // Only add to macro trigger list if non-off
396         if ( state != 0x00 )
397         {
398                 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
399                 macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
400                 macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x01; // LED key
401                 macroTriggerListBufferSize++;
402         }
403 }
404
405
406 // Append result macro to pending list, checking for duplicates
407 // Do nothing if duplicate
408 inline void Macro_appendResultMacroToPendingList( TriggerMacro *triggerMacro )
409 {
410         // Lookup result macro index
411         var_uint_t resultMacroIndex = triggerMacro->result;
412
413         // Iterate through result macro pending list, making sure this macro hasn't been added yet
414         for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
415         {
416                 // If duplicate found, do nothing
417                 if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
418                         return;
419         }
420
421         // No duplicates found, add to pending list
422         macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
423
424         // Lookup scanCode of the last key in the last combo
425         var_uint_t pos = 0;
426         for ( uint8_t comboLength = triggerMacro->guide[0]; comboLength > 0; )
427         {
428                 pos += TriggerGuideSize * comboLength + 1;
429                 comboLength = triggerMacro->guide[ pos ];
430         }
431
432         uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode;
433
434         // Lookup scanCode in buffer list for the current state and stateType
435         for ( uint8_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ )
436         {
437                 if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode )
438                 {
439                         ResultMacroList[ resultMacroIndex ].state     = macroTriggerListBuffer[ keyIndex ].state;
440                         ResultMacroList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type;
441                 }
442         }
443
444         // Reset the macro position
445         ResultMacroList[ resultMacroIndex ].pos = 0;
446 }
447
448
449 // Determine if long ResultMacro (more than 1 seqence element)
450 inline uint8_t Macro_isLongResultMacro( ResultMacro *macro )
451 {
452         // Check the second sequence combo length
453         // If non-zero return non-zero (long sequence)
454         // 0 otherwise (short sequence)
455         var_uint_t position = 1;
456         for ( var_uint_t result = 0; result < macro->guide[0]; result++ )
457                 position += ResultGuideSize( (ResultGuide*)&macro->guide[ position ] );
458         return macro->guide[ position ];
459 }
460
461
462 // Determine if long TriggerMacro (more than 1 sequence element)
463 inline uint8_t Macro_isLongTriggerMacro( TriggerMacro *macro )
464 {
465         // Check the second sequence combo length
466         // If non-zero return non-zero (long sequence)
467         // 0 otherwise (short sequence)
468         return macro->guide[ macro->guide[0] * TriggerGuideSize + 1 ];
469 }
470
471
472 // Votes on the given key vs. guide, short macros
473 inline TriggerMacroVote Macro_evalShortTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
474 {
475         // Depending on key type
476         switch ( guide->type )
477         {
478         // Normal State Type
479         case 0x00:
480                 // For short TriggerMacros completely ignore incorrect keys
481                 if ( guide->scanCode == key->scanCode )
482                 {
483                         switch ( key->state )
484                         {
485                         // Correct key, pressed, possible passing
486                         case 0x01:
487                                 return TriggerMacroVote_Pass;
488
489                         // Correct key, held, possible passing or release
490                         case 0x02:
491                                 return TriggerMacroVote_PassRelease;
492
493                         // Correct key, released, possible release
494                         case 0x03:
495                                 return TriggerMacroVote_Release;
496                         }
497                 }
498
499                 return TriggerMacroVote_DoNothing;
500
501         // LED State Type
502         case 0x01:
503                 erro_print("LED State Type - Not implemented...");
504                 break;
505
506         // Analog State Type
507         case 0x02:
508                 erro_print("Analog State Type - Not implemented...");
509                 break;
510
511         // Invalid State Type
512         default:
513                 erro_print("Invalid State Type. This is a bug.");
514                 break;
515         }
516
517         // XXX Shouldn't reach here
518         return TriggerMacroVote_Invalid;
519 }
520
521
522 // Votes on the given key vs. guide, long macros
523 // A long macro is defined as a guide with more than 1 combo
524 inline TriggerMacroVote Macro_evalLongTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
525 {
526         // Depending on key type
527         switch ( guide->type )
528         {
529         // Normal State Type
530         case 0x00:
531                 // Depending on the state of the buffered key, make voting decision
532                 // Incorrect key
533                 if ( guide->scanCode != key->scanCode )
534                 {
535                         switch ( key->state )
536                         {
537                         // Wrong key, pressed, fail
538                         case 0x01:
539                                 return TriggerMacroVote_Fail;
540
541                         // Wrong key, held, do not pass (no effect)
542                         case 0x02:
543                                 return TriggerMacroVote_DoNothing;
544
545                         // Wrong key released, fail out if pos == 0
546                         case 0x03:
547                                 return TriggerMacroVote_DoNothing | TriggerMacroVote_DoNothingRelease;
548                         }
549                 }
550
551                 // Correct key
552                 else
553                 {
554                         switch ( key->state )
555                         {
556                         // Correct key, pressed, possible passing
557                         case 0x01:
558                                 return TriggerMacroVote_Pass;
559
560                         // Correct key, held, possible passing or release
561                         case 0x02:
562                                 return TriggerMacroVote_PassRelease;
563
564                         // Correct key, released, possible release
565                         case 0x03:
566                                 return TriggerMacroVote_Release;
567                         }
568                 }
569
570                 break;
571
572         // LED State Type
573         case 0x01:
574                 erro_print("LED State Type - Not implemented...");
575                 break;
576
577         // Analog State Type
578         case 0x02:
579                 erro_print("Analog State Type - Not implemented...");
580                 break;
581
582         // Invalid State Type
583         default:
584                 erro_print("Invalid State Type. This is a bug.");
585                 break;
586         }
587
588         // XXX Shouldn't reach here
589         return TriggerMacroVote_Invalid;
590 }
591
592
593 // Evaluate/Update TriggerMacro
594 inline TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex )
595 {
596         // Lookup TriggerMacro
597         TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
598
599         // Check if macro has finished and should be incremented sequence elements
600         if ( macro->state == TriggerMacro_Release )
601         {
602                 macro->state = TriggerMacro_Waiting;
603                 macro->pos = macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1;
604         }
605
606         // Current Macro position
607         var_uint_t pos = macro->pos;
608
609         // Length of the combo being processed
610         uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
611
612         // If no combo items are left, remove the TriggerMacro from the pending list
613         if ( comboLength == 0 )
614         {
615                 return TriggerMacroEval_Remove;
616         }
617
618         // Check if this is a long Trigger Macro
619         uint8_t longMacro = Macro_isLongTriggerMacro( macro );
620
621         // Iterate through the items in the combo, voting the on the key state
622         // If any of the pressed keys do not match, fail the macro
623         //
624         // The macro is waiting for input when in the TriggerMacro_Waiting state
625         // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
626         // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
627         // On scan after position increment, change to TriggerMacro_Waiting state
628         // TODO Add support for system LED states (NumLock, CapsLock, etc.)
629         // TODO Add support for analog key states
630         // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
631         // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
632         TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
633         for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
634         {
635                 // Assign TriggerGuide element (key type, state and scancode)
636                 TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ comboItem ]);
637
638                 TriggerMacroVote vote = TriggerMacroVote_Invalid;
639                 // Iterate through the key buffer, comparing to each key in the combo
640                 for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
641                 {
642                         // Lookup key information
643                         TriggerGuide *keyInfo = &macroTriggerListBuffer[ key ];
644
645                         // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
646                         // Also mask all of the non-passing votes
647                         vote |= longMacro
648                                 ? Macro_evalLongTriggerMacroVote( keyInfo, guide )
649                                 : Macro_evalShortTriggerMacroVote( keyInfo, guide );
650                         if ( vote >= TriggerMacroVote_Pass )
651                         {
652                                 vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
653                                 break;
654                         }
655                 }
656
657                 // If no pass vote was found after scanning all of the keys
658                 // Fail the combo, if this is a short macro (long macros already will have a fail vote)
659                 if ( !longMacro && vote < TriggerMacroVote_Pass )
660                         vote |= TriggerMacroVote_Fail;
661
662                 // After voting, append to overall vote
663                 overallVote |= vote;
664         }
665
666         // If no pass vote was found after scanning the entire combo
667         // And this is the first position in the combo, just remove it (nothing important happened)
668         if ( longMacro && overallVote & TriggerMacroVote_DoNothingRelease && pos == 0 )
669                 overallVote |= TriggerMacroVote_Fail;
670
671         // Decide new state of macro after voting
672         // Fail macro, remove from pending list
673         if ( overallVote & TriggerMacroVote_Fail )
674         {
675                 return TriggerMacroEval_Remove;
676         }
677         // Do nothing, incorrect key is being held or released
678         else if ( overallVote & TriggerMacroVote_DoNothing && longMacro )
679         {
680                 // Just doing nothing :)
681         }
682         // If passing and in Waiting state, set macro state to Press
683         else if ( overallVote & TriggerMacroVote_Pass
684              && ( macro->state == TriggerMacro_Waiting || macro->state == TriggerMacro_Press ) )
685         {
686                 macro->state = TriggerMacro_Press;
687
688                 // If in press state, and this is the final combo, send request for ResultMacro
689                 // Check to see if the result macro only has a single element
690                 // If this result macro has more than 1 key, only send once
691                 // TODO Add option to have long macro repeat rate
692                 if ( macro->guide[ pos + comboLength + 1 ] == 0 )
693                 {
694                         // Long result macro (more than 1 combo)
695                         if ( Macro_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
696                         {
697                                 // Only ever trigger result once, on press
698                                 if ( overallVote == TriggerMacroVote_Pass )
699                                 {
700                                         return TriggerMacroEval_DoResultAndRemove;
701                                 }
702                         }
703                         // Short result macro
704                         else
705                         {
706                                 // Only trigger result once, on press, if long trigger (more than 1 combo)
707                                 if ( Macro_isLongTriggerMacro( macro ) )
708                                 {
709                                         return TriggerMacroEval_DoResultAndRemove;
710                                 }
711                                 // Otherwise, trigger result continuously
712                                 else
713                                 {
714                                         return TriggerMacroEval_DoResult;
715                                 }
716                         }
717                 }
718         }
719         // If ready for transition and in Press state, set to Waiting and increment combo position
720         // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
721         else if ( overallVote & TriggerMacroVote_Release && macro->state == TriggerMacro_Press )
722         {
723                 macro->state = TriggerMacro_Release;
724
725                 // If this is the last combo in the sequence, remove from the pending list
726                 if ( macro->guide[ macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1 ] == 0 )
727                         return TriggerMacroEval_Remove;
728         }
729         // Otherwise, just remove the macro on key release
730         // One more result has to be called to indicate to the ResultMacro that the key transitioned to the release state
731         else if ( overallVote & TriggerMacroVote_Release )
732         {
733                 return TriggerMacroEval_DoResultAndRemove;
734         }
735
736         // If this is a short macro, just remove it
737         // The state can be rebuilt on the next iteration
738         if ( !longMacro )
739                 return TriggerMacroEval_Remove;
740
741         return TriggerMacroEval_DoNothing;
742 }
743
744
745 // Evaluate/Update ResultMacro
746 inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex )
747 {
748         // Lookup ResultMacro
749         ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
750
751         // Current Macro position
752         var_uint_t pos = macro->pos;
753
754         // Length of combo being processed
755         uint8_t comboLength = macro->guide[ pos ];
756
757         // Function Counter, used to keep track of the combo items processed
758         var_uint_t funcCount = 0;
759
760         // Combo Item Position within the guide
761         var_uint_t comboItem = pos + 1;
762
763         // Iterate through the Result Combo
764         while ( funcCount < comboLength )
765         {
766                 // Assign TriggerGuide element (key type, state and scancode)
767                 ResultGuide *guide = (ResultGuide*)(&macro->guide[ comboItem ]);
768
769                 // Do lookup on capability function
770                 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
771
772                 // Call capability
773                 capability( macro->state, macro->stateType, &guide->args );
774
775                 // Increment counters
776                 funcCount++;
777                 comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
778         }
779
780         // Move to next item in the sequence
781         macro->pos = comboItem;
782
783         // If the ResultMacro is finished, remove
784         if ( macro->guide[ comboItem ] == 0 )
785         {
786                 return ResultMacroEval_Remove;
787         }
788
789         // Otherwise leave the macro in the list
790         return ResultMacroEval_DoNothing;
791 }
792
793
794 // Update pending trigger list
795 inline void Macro_updateTriggerMacroPendingList()
796 {
797         // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
798         for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
799         {
800                 // TODO LED States
801                 // TODO Analog Switches
802                 // Only add TriggerMacro to pending list if key was pressed (not held, released or off)
803                 if ( macroTriggerListBuffer[ key ].state == 0x00 && macroTriggerListBuffer[ key ].state != 0x01 )
804                         continue;
805
806                 // Lookup Trigger List
807                 nat_ptr_t *triggerList = Macro_layerLookup( macroTriggerListBuffer[ key ].scanCode );
808
809                 // Number of Triggers in list
810                 nat_ptr_t triggerListSize = triggerList[0];
811
812                 // Iterate over triggerList to see if any TriggerMacros need to be added
813                 // First item is the number of items in the TriggerList
814                 for ( var_uint_t macro = 1; macro < triggerListSize + 1; macro++ )
815                 {
816                         // Lookup trigger macro index
817                         var_uint_t triggerMacroIndex = triggerList[ macro ];
818
819                         // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
820                         //  triggerList needs to be added
821                         var_uint_t pending = 0;
822                         for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
823                         {
824                                 // Stop scanning if the trigger macro index is found in the pending list
825                                 if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
826                                         break;
827                         }
828
829                         // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
830                         // Add it to the list
831                         if ( pending == macroTriggerMacroPendingListSize )
832                         {
833                                 macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
834
835                                 // Reset macro position
836                                 TriggerMacroList[ triggerMacroIndex ].pos   = 0;
837                                 TriggerMacroList[ triggerMacroIndex ].state = TriggerMacro_Waiting;
838                         }
839                 }
840         }
841 }
842
843
844 // Macro Procesing Loop
845 // Called once per USB buffer send
846 inline void Macro_process()
847 {
848         // Only do one round of macro processing between Output Module timer sends
849         if ( USBKeys_Sent != 0 )
850                 return;
851
852         // If the pause flag is set, only process if the step counter is non-zero
853         if ( macroPauseMode )
854         {
855                 if ( macroStepCounter == 0 )
856                         return;
857
858                 // Proceed, decrementing the step counter
859                 macroStepCounter--;
860                 dbug_print("Macro Step");
861         }
862
863         // Update pending trigger list, before processing TriggerMacros
864         Macro_updateTriggerMacroPendingList();
865
866         // Tail pointer for macroTriggerMacroPendingList
867         // Macros must be explicitly re-added
868         var_uint_t macroTriggerMacroPendingListTail = 0;
869
870         // Iterate through the pending TriggerMacros, processing each of them
871         for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
872         {
873                 switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
874                 {
875                 // Trigger Result Macro (purposely falling through)
876                 case TriggerMacroEval_DoResult:
877                         // Append ResultMacro to PendingList
878                         Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
879
880                 default:
881                         macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
882                         break;
883
884                 // Trigger Result Macro and Remove (purposely falling through)
885                 case TriggerMacroEval_DoResultAndRemove:
886                         // Append ResultMacro to PendingList
887                         Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
888
889                 // Remove Macro from Pending List, nothing to do, removing by default
890                 case TriggerMacroEval_Remove:
891                         break;
892                 }
893         }
894
895         // Update the macroTriggerMacroPendingListSize with the tail pointer
896         macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
897
898
899         // Tail pointer for macroResultMacroPendingList
900         // Macros must be explicitly re-added
901         var_uint_t macroResultMacroPendingListTail = 0;
902
903         // Iterate through the pending ResultMacros, processing each of them
904         for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
905         {
906                 switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
907                 {
908                 // Re-add macros to pending list
909                 case ResultMacroEval_DoNothing:
910                 default:
911                         macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
912                         break;
913
914                 // Remove Macro from Pending List, nothing to do, removing by default
915                 case ResultMacroEval_Remove:
916                         break;
917                 }
918         }
919
920         // Update the macroResultMacroPendingListSize with the tail pointer
921         macroResultMacroPendingListSize = macroResultMacroPendingListTail;
922
923         // Signal buffer that we've used it
924         Scan_finishedWithMacro( macroTriggerListBufferSize );
925
926         // Reset TriggerList buffer
927         macroTriggerListBufferSize = 0;
928
929         // If Macro debug mode is set, clear the USB Buffer
930         if ( macroDebugMode )
931         {
932                 USBKeys_Modifiers = 0;
933                 USBKeys_Sent = 0;
934         }
935 }
936
937
938 inline void Macro_setup()
939 {
940         // Register Macro CLI dictionary
941         CLI_registerDictionary( macroCLIDict, macroCLIDictName );
942
943         // Disable Macro debug mode
944         macroDebugMode = 0;
945
946         // Disable Macro pause flag
947         macroPauseMode = 0;
948
949         // Set Macro step counter to zero
950         macroStepCounter = 0;
951
952         // Make sure macro trigger buffer is empty
953         macroTriggerListBufferSize = 0;
954
955         // Initialize TriggerMacro states
956         for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
957         {
958                 TriggerMacroList[ macro ].pos   = 0;
959                 TriggerMacroList[ macro ].state = TriggerMacro_Waiting;
960         }
961
962         // Initialize ResultMacro states
963         for ( var_uint_t macro = 0; macro < ResultMacroNum; macro++ )
964         {
965                 ResultMacroList[ macro ].pos       = 0;
966                 ResultMacroList[ macro ].state     = 0;
967                 ResultMacroList[ macro ].stateType = 0;
968         }
969 }
970
971
972 // ----- CLI Command Functions -----
973
974 void cliFunc_capList( char* args )
975 {
976         print( NL );
977         info_msg("Capabilities List");
978         printHex( CapabilitiesNum );
979
980         // Iterate through all of the capabilities and display them
981         for ( var_uint_t cap = 0; cap < CapabilitiesNum; cap++ )
982         {
983                 print( NL "\t" );
984                 printHex( cap );
985                 print(" - ");
986
987                 // Display/Lookup Capability Name (utilize debug mode of capability)
988                 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
989                 capability( 0xFF, 0xFF, 0 );
990         }
991 }
992
993 void cliFunc_capSelect( char* args )
994 {
995         // Parse code from argument
996         char* curArgs;
997         char* arg1Ptr;
998         char* arg2Ptr = args;
999
1000         // Total number of args to scan (must do a lookup if a keyboard capability is selected)
1001         var_uint_t totalArgs = 2; // Always at least two args
1002         var_uint_t cap = 0;
1003
1004         // Arguments used for keyboard capability function
1005         var_uint_t argSetCount = 0;
1006         uint8_t *argSet = (uint8_t*)args;
1007
1008         // Process all args
1009         for ( var_uint_t c = 0; argSetCount < totalArgs; c++ )
1010         {
1011                 curArgs = arg2Ptr;
1012                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1013
1014                 // Stop processing args if no more are found
1015                 // Extra arguments are ignored
1016                 if ( *arg1Ptr == '\0' )
1017                         break;
1018
1019                 // For the first argument, choose the capability
1020                 if ( c == 0 ) switch ( arg1Ptr[0] )
1021                 {
1022                 // Keyboard Capability
1023                 case 'K':
1024                         // Determine capability index
1025                         cap = numToInt( &arg1Ptr[1] );
1026
1027                         // Lookup the number of args
1028                         totalArgs += CapabilitiesList[ cap ].argCount;
1029                         continue;
1030                 }
1031
1032                 // Because allocating memory isn't doable, and the argument count is arbitrary
1033                 // The argument pointer is repurposed as the argument list (much smaller anyways)
1034                 argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
1035
1036                 // Once all the arguments are prepared, call the keyboard capability function
1037                 if ( argSetCount == totalArgs )
1038                 {
1039                         // Indicate that the capability was called
1040                         print( NL );
1041                         info_msg("K");
1042                         printInt8( cap );
1043                         print(" - ");
1044                         printHex( argSet[0] );
1045                         print(" - ");
1046                         printHex( argSet[1] );
1047                         print(" - ");
1048                         printHex( argSet[2] );
1049                         print( "..." NL );
1050
1051                         void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ cap ].func);
1052                         capability( argSet[0], argSet[1], &argSet[2] );
1053                 }
1054         }
1055 }
1056
1057 void cliFunc_keyHold( char* args )
1058 {
1059         // Parse codes from arguments
1060         char* curArgs;
1061         char* arg1Ptr;
1062         char* arg2Ptr = args;
1063
1064         // Process all args
1065         for ( ;; )
1066         {
1067                 curArgs = arg2Ptr;
1068                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1069
1070                 // Stop processing args if no more are found
1071                 if ( *arg1Ptr == '\0' )
1072                         break;
1073
1074                 // Ignore non-Scancode numbers
1075                 switch ( arg1Ptr[0] )
1076                 {
1077                 // Scancode
1078                 case 'S':
1079                         Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
1080                         break;
1081                 }
1082         }
1083 }
1084
1085 void cliFunc_keyPress( char* args )
1086 {
1087         // Parse codes from arguments
1088         char* curArgs;
1089         char* arg1Ptr;
1090         char* arg2Ptr = args;
1091
1092         // Process all args
1093         for ( ;; )
1094         {
1095                 curArgs = arg2Ptr;
1096                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1097
1098                 // Stop processing args if no more are found
1099                 if ( *arg1Ptr == '\0' )
1100                         break;
1101
1102                 // Ignore non-Scancode numbers
1103                 switch ( arg1Ptr[0] )
1104                 {
1105                 // Scancode
1106                 case 'S':
1107                         Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
1108                         break;
1109                 }
1110         }
1111 }
1112
1113 void cliFunc_keyRelease( char* args )
1114 {
1115         // Parse codes from arguments
1116         char* curArgs;
1117         char* arg1Ptr;
1118         char* arg2Ptr = args;
1119
1120         // Process all args
1121         for ( ;; )
1122         {
1123                 curArgs = arg2Ptr;
1124                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1125
1126                 // Stop processing args if no more are found
1127                 if ( *arg1Ptr == '\0' )
1128                         break;
1129
1130                 // Ignore non-Scancode numbers
1131                 switch ( arg1Ptr[0] )
1132                 {
1133                 // Scancode
1134                 case 'S':
1135                         Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
1136                         break;
1137                 }
1138         }
1139 }
1140
1141 void cliFunc_layerList( char* args )
1142 {
1143         print( NL );
1144         info_msg("Layer List");
1145
1146         // Iterate through all of the layers and display them
1147         for ( uint16_t layer = 0; layer < LayerNum; layer++ )
1148         {
1149                 print( NL "\t" );
1150                 printHex( layer );
1151                 print(" - ");
1152
1153                 // Display layer name
1154                 dPrint( (char*)LayerIndex[ layer ].name );
1155
1156                 // Default map
1157                 if ( layer == 0 )
1158                         print(" \033[1m(default)\033[0m");
1159
1160                 // Layer State
1161                 print( NL "\t\t Layer State: " );
1162                 printHex( LayerIndex[ layer ].state );
1163
1164                 // Max Index
1165                 print(" Max Index: ");
1166                 printHex( LayerIndex[ layer ].max );
1167         }
1168 }
1169
1170 void cliFunc_layerState( char* args )
1171 {
1172         // Parse codes from arguments
1173         char* curArgs;
1174         char* arg1Ptr;
1175         char* arg2Ptr = args;
1176
1177         uint8_t arg1 = 0;
1178         uint8_t arg2 = 0;
1179
1180         // Process first two args
1181         for ( uint8_t c = 0; c < 2; c++ )
1182         {
1183                 curArgs = arg2Ptr;
1184                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1185
1186                 // Stop processing args if no more are found
1187                 if ( *arg1Ptr == '\0' )
1188                         break;
1189
1190                 switch ( c )
1191                 {
1192                 // First argument (e.g. L1)
1193                 case 0:
1194                         if ( arg1Ptr[0] != 'L' )
1195                                 return;
1196
1197                         arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
1198                         break;
1199                 // Second argument (e.g. 4)
1200                 case 1:
1201                         arg2 = (uint8_t)numToInt( arg1Ptr );
1202
1203                         // Display operation (to indicate that it worked)
1204                         print( NL );
1205                         info_msg("Setting Layer L");
1206                         printInt8( arg1 );
1207                         print(" to - ");
1208                         printHex( arg2 );
1209
1210                         // Set the layer state
1211                         LayerIndex[ arg1 ].state = arg2;
1212                         break;
1213                 }
1214         }
1215 }
1216
1217 void cliFunc_macroDebug( char* args )
1218 {
1219         // Toggle macro debug mode
1220         macroDebugMode = macroDebugMode ? 0 : 1;
1221
1222         print( NL );
1223         info_msg("Macro Debug Mode: ");
1224         printInt8( macroDebugMode );
1225 }
1226
1227 void cliFunc_macroList( char* args )
1228 {
1229         // Show pending key events
1230         print( NL );
1231         info_msg("Pending Key Events: ");
1232         printInt16( (uint16_t)macroTriggerListBufferSize );
1233         print(" : ");
1234         for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
1235         {
1236                 printHex( macroTriggerListBuffer[ key ].scanCode );
1237                 print(" ");
1238         }
1239
1240         // Show pending trigger macros
1241         print( NL );
1242         info_msg("Pending Trigger Macros: ");
1243         printInt16( (uint16_t)macroTriggerMacroPendingListSize );
1244         print(" : ");
1245         for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
1246         {
1247                 printHex( macroTriggerMacroPendingList[ macro ] );
1248                 print(" ");
1249         }
1250
1251         // Show pending result macros
1252         print( NL );
1253         info_msg("Pending Result Macros: ");
1254         printInt16( (uint16_t)macroResultMacroPendingListSize );
1255         print(" : ");
1256         for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
1257         {
1258                 printHex( macroResultMacroPendingList[ macro ] );
1259                 print(" ");
1260         }
1261
1262         // Show available trigger macro indices
1263         print( NL );
1264         info_msg("Trigger Macros Range: T0 -> T");
1265         printInt16( (uint16_t)TriggerMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
1266
1267         // Show available result macro indices
1268         print( NL );
1269         info_msg("Result  Macros Range: R0 -> R");
1270         printInt16( (uint16_t)ResultMacroNum - 1 ); // Hopefully large enough :P (can't assume 32-bit)
1271
1272         // Show Trigger to Result Macro Links
1273         print( NL );
1274         info_msg("Trigger : Result Macro Pairs");
1275         for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
1276         {
1277                 print( NL );
1278                 print("\tT");
1279                 printInt16( (uint16_t)macro ); // Hopefully large enough :P (can't assume 32-bit)
1280                 print(" : R");
1281                 printInt16( (uint16_t)TriggerMacroList[ macro ].result ); // Hopefully large enough :P (can't assume 32-bit)
1282         }
1283 }
1284
1285 void cliFunc_macroProc( char* args )
1286 {
1287         // Toggle macro pause mode
1288         macroPauseMode = macroPauseMode ? 0 : 1;
1289
1290         print( NL );
1291         info_msg("Macro Processing Mode: ");
1292         printInt8( macroPauseMode );
1293 }
1294
1295 void macroDebugShowTrigger( var_uint_t index )
1296 {
1297         // Only proceed if the macro exists
1298         if ( index >= TriggerMacroNum )
1299                 return;
1300
1301         // Trigger Macro Show
1302         TriggerMacro *macro = &TriggerMacroList[ index ];
1303
1304         print( NL );
1305         info_msg("Trigger Macro Index: ");
1306         printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
1307         print( NL );
1308
1309         // Read the comboLength for combo in the sequence (sequence of combos)
1310         var_uint_t pos = 0;
1311         uint8_t comboLength = macro->guide[ pos ];
1312
1313         // Iterate through and interpret the guide
1314         while ( comboLength != 0 )
1315         {
1316                 // Initial position of the combo
1317                 var_uint_t comboPos = ++pos;
1318
1319                 // Iterate through the combo
1320                 while ( pos < comboLength * TriggerGuideSize + comboPos )
1321                 {
1322                         // Assign TriggerGuide element (key type, state and scancode)
1323                         TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
1324
1325                         // Display guide information about trigger key
1326                         printHex( guide->scanCode );
1327                         print("|");
1328                         printHex( guide->type );
1329                         print("|");
1330                         printHex( guide->state );
1331
1332                         // Increment position
1333                         pos += TriggerGuideSize;
1334
1335                         // Only show combo separator if there are combos left in the sequence element
1336                         if ( pos < comboLength * TriggerGuideSize + comboPos )
1337                                 print("+");
1338                 }
1339
1340                 // Read the next comboLength
1341                 comboLength = macro->guide[ pos ];
1342
1343                 // Only show sequence separator if there is another combo to process
1344                 if ( comboLength != 0 )
1345                         print(";");
1346         }
1347
1348         // Display current position
1349         print( NL "Position: " );
1350         printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
1351
1352         // Display result macro index
1353         print( NL "Result Macro Index: " );
1354         printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
1355
1356         // Display trigger macro state
1357         print( NL "Trigger Macro State: " );
1358         switch ( macro->state )
1359         {
1360         case TriggerMacro_Press:   print("Press");   break;
1361         case TriggerMacro_Release: print("Release"); break;
1362         case TriggerMacro_Waiting: print("Waiting"); break;
1363         }
1364 }
1365
1366 void macroDebugShowResult( var_uint_t index )
1367 {
1368         // Only proceed if the macro exists
1369         if ( index >= ResultMacroNum )
1370                 return;
1371
1372         // Trigger Macro Show
1373         ResultMacro *macro = &ResultMacroList[ index ];
1374
1375         print( NL );
1376         info_msg("Result Macro Index: ");
1377         printInt16( (uint16_t)index ); // Hopefully large enough :P (can't assume 32-bit)
1378         print( NL );
1379
1380         // Read the comboLength for combo in the sequence (sequence of combos)
1381         var_uint_t pos = 0;
1382         uint8_t comboLength = macro->guide[ pos++ ];
1383
1384         // Iterate through and interpret the guide
1385         while ( comboLength != 0 )
1386         {
1387                 // Function Counter, used to keep track of the combos processed
1388                 var_uint_t funcCount = 0;
1389
1390                 // Iterate through the combo
1391                 while ( funcCount < comboLength )
1392                 {
1393                         // Assign TriggerGuide element (key type, state and scancode)
1394                         ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
1395
1396                         // Display Function Index
1397                         printHex( guide->index );
1398                         print("|");
1399
1400                         // Display Function Ptr Address
1401                         printHex( (nat_ptr_t)CapabilitiesList[ guide->index ].func );
1402                         print("|");
1403
1404                         // Display/Lookup Capability Name (utilize debug mode of capability)
1405                         void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
1406                         capability( 0xFF, 0xFF, 0 );
1407
1408                         // Display Argument(s)
1409                         print("(");
1410                         for ( var_uint_t arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
1411                         {
1412                                 // Arguments are only 8 bit values
1413                                 printHex( (&guide->args)[ arg ] );
1414
1415                                 // Only show arg separator if there are args left
1416                                 if ( arg + 1 < CapabilitiesList[ guide->index ].argCount )
1417                                         print(",");
1418                         }
1419                         print(")");
1420
1421                         // Increment position
1422                         pos += ResultGuideSize( guide );
1423
1424                         // Increment function count
1425                         funcCount++;
1426
1427                         // Only show combo separator if there are combos left in the sequence element
1428                         if ( funcCount < comboLength )
1429                                 print("+");
1430                 }
1431
1432                 // Read the next comboLength
1433                 comboLength = macro->guide[ pos++ ];
1434
1435                 // Only show sequence separator if there is another combo to process
1436                 if ( comboLength != 0 )
1437                         print(";");
1438         }
1439
1440         // Display current position
1441         print( NL "Position: " );
1442         printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
1443
1444         // Display final trigger state/type
1445         print( NL "Final Trigger State (State/Type): " );
1446         printHex( macro->state );
1447         print("/");
1448         printHex( macro->stateType );
1449 }
1450
1451 void cliFunc_macroShow( char* args )
1452 {
1453         // Parse codes from arguments
1454         char* curArgs;
1455         char* arg1Ptr;
1456         char* arg2Ptr = args;
1457
1458         // Process all args
1459         for ( ;; )
1460         {
1461                 curArgs = arg2Ptr;
1462                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1463
1464                 // Stop processing args if no more are found
1465                 if ( *arg1Ptr == '\0' )
1466                         break;
1467
1468                 // Ignore invalid codes
1469                 switch ( arg1Ptr[0] )
1470                 {
1471                 // Indexed Trigger Macro
1472                 case 'T':
1473                         macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
1474                         break;
1475                 // Indexed Result Macro
1476                 case 'R':
1477                         macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
1478                         break;
1479                 }
1480         }
1481 }
1482
1483 void cliFunc_macroStep( char* args )
1484 {
1485         // Parse number from argument
1486         //  NOTE: Only first argument is used
1487         char* arg1Ptr;
1488         char* arg2Ptr;
1489         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
1490
1491         // Default to 1, if no argument given
1492         var_uint_t count = (var_uint_t)numToInt( arg1Ptr );
1493
1494         if ( count == 0 )
1495                 count = 1;
1496
1497         // Set the macro step counter, negative int's are cast to uint
1498         macroStepCounter = count;
1499 }
1500