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