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