]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Macro/PartialMap/macro.c
Fixing layer stack evaluation
[kiibohd-controller.git] / Macro / PartialMap / macro.c
index b40dfa02fb4d7a9935b89ea7722bab435b050cdf..7161fbf9e495d92eb9ec5d6c42f50afae355555a 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 by Jacob Alexander
+/* Copyright (C) 2014-2015 by Jacob Alexander
  *
  * This file is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <led.h>
 #include <print.h>
 #include <scan_loop.h>
-#include <output_com.h>
 
 // Keymaps
 #include "usb_hid.h"
-#include <defaultMap.h>
-#include "generatedKeymap.h" // TODO Use actual generated version
+#include <generatedKeymap.h> // Generated using kll at compile time, in build directory
+
+// Connect Includes
+#if defined(ConnectEnabled_define)
+#include <connect_scan.h>
+#endif
 
 // Local Includes
 #include "macro.h"
 
 void cliFunc_capList   ( char* args );
 void cliFunc_capSelect ( char* args );
+void cliFunc_keyHold   ( char* args );
 void cliFunc_keyPress  ( char* args );
 void cliFunc_keyRelease( char* args );
+void cliFunc_layerDebug( char* args );
 void cliFunc_layerList ( char* args );
 void cliFunc_layerState( char* args );
 void cliFunc_macroDebug( char* args );
@@ -52,26 +57,71 @@ void cliFunc_macroStep ( char* args );
 
 
 
+// ----- Enums -----
+
+// Bit positions are important, passes (correct key) always trump incorrect key votes
+typedef enum TriggerMacroVote {
+       TriggerMacroVote_Release          = 0x10, // Correct key
+       TriggerMacroVote_PassRelease      = 0x18, // Correct key (both pass and release)
+       TriggerMacroVote_Pass             = 0x8,  // Correct key
+       TriggerMacroVote_DoNothingRelease = 0x4,  // Incorrect key
+       TriggerMacroVote_DoNothing        = 0x2,  // Incorrect key
+       TriggerMacroVote_Fail             = 0x1,  // Incorrect key
+       TriggerMacroVote_Invalid          = 0x0,  // Invalid state
+} TriggerMacroVote;
+
+typedef enum TriggerMacroEval {
+       TriggerMacroEval_DoNothing,
+       TriggerMacroEval_DoResult,
+       TriggerMacroEval_DoResultAndRemove,
+       TriggerMacroEval_Remove,
+} TriggerMacroEval;
+
+typedef enum ResultMacroEval {
+       ResultMacroEval_DoNothing,
+       ResultMacroEval_Remove,
+} ResultMacroEval;
+
+
+
 // ----- Variables -----
 
 // Macro Module command dictionary
-char*       macroCLIDictName = "Macro Module Commands";
-CLIDictItem macroCLIDict[] = {
-       { "capList",     "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
-       { "capSelect",   "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B", cliFunc_capSelect },
-       { "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 },
-       { "keyRelease",  "Release a key-press from the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
-       { "layerList",   "List available layers.", cliFunc_layerList },
-       { "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 },
-       { "macroDebug",  "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
-       { "macroList",   "List the defined trigger and result macros.", cliFunc_macroList },
-       { "macroProc",   "Pause/Resume macro processing.", cliFunc_macroProc },
-       { "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 },
-       { "macroStep",   "Do N macro processing steps. Defaults to 1.", cliFunc_macroStep },
+CLIDict_Entry( capList,     "Prints an indexed list of all non USB keycode capabilities." );
+CLIDict_Entry( capSelect,   "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B" );
+CLIDict_Entry( keyHold,     "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
+CLIDict_Entry( keyPress,    "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
+CLIDict_Entry( keyRelease,  "Send key-release event to macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A" );
+CLIDict_Entry( layerDebug,  "Layer debug mode. Shows layer stack and any changes." );
+CLIDict_Entry( layerList,   "List available layers." );
+CLIDict_Entry( 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" );
+CLIDict_Entry( macroDebug,  "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes." );
+CLIDict_Entry( macroList,   "List the defined trigger and result macros." );
+CLIDict_Entry( macroProc,   "Pause/Resume macro processing." );
+CLIDict_Entry( 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" );
+CLIDict_Entry( macroStep,   "Do N macro processing steps. Defaults to 1." );
+
+CLIDict_Def( macroCLIDict, "Macro Module Commands" ) = {
+       CLIDict_Item( capList ),
+       CLIDict_Item( capSelect ),
+       CLIDict_Item( keyHold ),
+       CLIDict_Item( keyPress ),
+       CLIDict_Item( keyRelease ),
+       CLIDict_Item( layerDebug ),
+       CLIDict_Item( layerList ),
+       CLIDict_Item( layerState ),
+       CLIDict_Item( macroDebug ),
+       CLIDict_Item( macroList ),
+       CLIDict_Item( macroProc ),
+       CLIDict_Item( macroShow ),
+       CLIDict_Item( macroStep ),
        { 0, 0, 0 } // Null entry for dictionary end
 };
 
 
+// Layer debug flag - If set, displays any changes to layers and the full layer stack on change
+uint8_t layerDebugMode = 0;
+
 // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
 uint8_t macroDebugMode = 0;
 
@@ -79,50 +129,426 @@ uint8_t macroDebugMode = 0;
 uint8_t macroPauseMode = 0;
 
 // Macro step counter - If non-zero, the step counter counts down every time the macro module does one processing loop
-unsigned int macroStepCounter = 0;
+uint16_t macroStepCounter = 0;
 
 
-// Key Trigger List Buffer
-//  * Item 1: scan code
-//  * Item 2: state
-//    ...
-uint8_t macroTriggerListBuffer[MaxScanCode * 2] = { 0 }; // Each key has a state to be cached
+// Key Trigger List Buffer and Layer Cache
+// The layer cache is set on press only, hold and release events refer to the value set on press
+TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
 uint8_t macroTriggerListBufferSize = 0;
+var_uint_t macroTriggerListLayerCache[ MaxScanCode ];
 
+// Pending Trigger Macro Index List
+//  * Any trigger macros that need processing from a previous macro processing loop
 // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
 //       Possibly could be calculated by the KLL compiler
 // XXX It may be possible to calculate the worst case using the KLL compiler
-TriggerMacro *triggerMacroPendingList[TriggerMacroNum];
+uint16_t macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
+uint16_t macroTriggerMacroPendingListSize = 0;
+
+// Layer Index Stack
+//  * When modifying layer state and the state is non-0x0, the stack must be adjusted
+uint16_t macroLayerIndexStack[ LayerNum + 1 ] = { 0 };
+uint16_t macroLayerIndexStackSize = 0;
+
+// Pending Result Macro Index List
+//  * Any result macro that needs processing from a previous macro processing loop
+uint16_t macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
+uint16_t macroResultMacroPendingListSize = 0;
+
+// Interconnect ScanCode Cache
+#if defined(ConnectEnabled_define)
+// TODO This can be shrunk by the size of the max node 0 ScanCode
+TriggerGuide macroInterconnectCache[ MaxScanCode ];
+uint8_t macroInterconnectCacheSize = 0;
+#endif
+
+
+
+// ----- Capabilities -----
+
+// Sets the given layer with the specified layerState
+void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t layerState )
+{
+       // Ignore if layer does not exist
+       if ( layer >= LayerNum )
+               return;
+
+       // Is layer in the LayerIndexStack?
+       uint8_t inLayerIndexStack = 0;
+       uint16_t stackItem = 0;
+       while ( stackItem < macroLayerIndexStackSize )
+       {
+               // Flag if layer is already in the LayerIndexStack
+               if ( macroLayerIndexStack[ stackItem ] == layer )
+               {
+                       inLayerIndexStack = 1;
+                       break;
+               }
+
+               // Increment to next item
+               stackItem++;
+       }
+
+       // Toggle Layer State Byte
+       if ( LayerState[ layer ] & layerState )
+       {
+               // Unset
+               LayerState[ layer ] &= ~layerState;
+       }
+       else
+       {
+               // Set
+               LayerState[ layer ] |= layerState;
+       }
+
+       // If the layer was not in the LayerIndexStack add it
+       if ( !inLayerIndexStack )
+       {
+               macroLayerIndexStack[ macroLayerIndexStackSize++ ] = layer;
+       }
+
+       // If the layer is in the LayerIndexStack and the state is 0x00, remove
+       if ( LayerState[ layer ] == 0x00 && inLayerIndexStack )
+       {
+               // Remove the layer from the LayerIndexStack
+               // Using the already positioned stackItem variable from the loop above
+               while ( stackItem < macroLayerIndexStackSize )
+               {
+                       macroLayerIndexStack[ stackItem ] = macroLayerIndexStack[ stackItem + 1 ];
+                       stackItem++;
+               }
+
+               // Reduce LayerIndexStack size
+               macroLayerIndexStackSize--;
+       }
+
+       // Layer Debug Mode
+       if ( layerDebugMode )
+       {
+               dbug_msg("Layer ");
+
+               // Iterate over each of the layers displaying the state as a hex value
+               for ( uint16_t index = 0; index < LayerNum; index++ )
+               {
+                       printHex_op( LayerState[ index ], 0 );
+               }
+
+               // Always show the default layer (it's always 0)
+               print(" 0");
+
+               // Iterate over the layer stack starting from the bottom of the stack
+               for ( uint16_t index = macroLayerIndexStackSize; index > 0; index-- )
+               {
+                       print(":");
+                       printHex_op( macroLayerIndexStack[ index - 1 ], 0 );
+               }
+
+               print( NL );
+       }
+}
+
+// Modifies the specified Layer control byte
+// Argument #1: Layer Index -> uint16_t
+// Argument #2: Layer State -> uint8_t
+void Macro_layerState_capability( uint8_t state, uint8_t stateType, uint8_t *args )
+{
+       // Display capability name
+       if ( stateType == 0xFF && state == 0xFF )
+       {
+               print("Macro_layerState(layerIndex,layerState)");
+               return;
+       }
+
+       // Only use capability on press or release
+       // TODO Analog
+       // XXX This may cause issues, might be better to implement state table here to decide -HaaTa
+       if ( stateType == 0x00 && state == 0x02 ) // Hold condition
+               return;
+
+       // Get layer index from arguments
+       // Cast pointer to uint8_t to uint16_t then access that memory location
+       uint16_t layer = *(uint16_t*)(&args[0]);
+
+       // Get layer toggle byte
+       uint8_t layerState = args[ sizeof(uint16_t) ];
+
+       Macro_layerState( state, stateType, layer, layerState );
+}
+
+
+// Latches given layer
+// Argument #1: Layer Index -> uint16_t
+void Macro_layerLatch_capability( uint8_t state, uint8_t stateType, uint8_t *args )
+{
+       // Display capability name
+       if ( stateType == 0xFF && state == 0xFF )
+       {
+               print("Macro_layerLatch(layerIndex)");
+               return;
+       }
+
+       // Only use capability on press
+       // TODO Analog
+       if ( stateType == 0x00 && state != 0x03 ) // Only on release
+               return;
+
+       // Get layer index from arguments
+       // Cast pointer to uint8_t to uint16_t then access that memory location
+       uint16_t layer = *(uint16_t*)(&args[0]);
+
+       Macro_layerState( state, stateType, layer, 0x02 );
+}
+
+
+// Locks given layer
+// Argument #1: Layer Index -> uint16_t
+void Macro_layerLock_capability( uint8_t state, uint8_t stateType, uint8_t *args )
+{
+       // Display capability name
+       if ( stateType == 0xFF && state == 0xFF )
+       {
+               print("Macro_layerLock(layerIndex)");
+               return;
+       }
+
+       // Only use capability on press
+       // TODO Analog
+       // XXX Could also be on release, but that's sorta dumb -HaaTa
+       if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
+               return;
+
+       // Get layer index from arguments
+       // Cast pointer to uint8_t to uint16_t then access that memory location
+       uint16_t layer = *(uint16_t*)(&args[0]);
+
+       Macro_layerState( state, stateType, layer, 0x04 );
+}
+
+
+// Shifts given layer
+// Argument #1: Layer Index -> uint16_t
+void Macro_layerShift_capability( uint8_t state, uint8_t stateType, uint8_t *args )
+{
+       // Display capability name
+       if ( stateType == 0xFF && state == 0xFF )
+       {
+               print("Macro_layerShift(layerIndex)");
+               return;
+       }
+
+       // Only use capability on press or release
+       // TODO Analog
+       if ( stateType == 0x00 && ( state == 0x00 || state == 0x02 ) ) // Only pass press or release conditions
+               return;
+
+       // Get layer index from arguments
+       // Cast pointer to uint8_t to uint16_t then access that memory location
+       uint16_t layer = *(uint16_t*)(&args[0]);
+
+       Macro_layerState( state, stateType, layer, 0x01 );
+}
 
 
 
 // ----- Functions -----
 
 // Looks up the trigger list for the given scan code (from the active layer)
-unsigned int *Macro_layerLookup( uint8_t scanCode )
+// NOTE: Calling function must handle the NULL pointer case
+nat_ptr_t *Macro_layerLookup( TriggerGuide *guide, uint8_t latch_expire )
 {
-       // TODO - No layer fallthrough lookup
-       return default_scanMap[ scanCode ];
+       uint8_t scanCode = guide->scanCode;
+
+       // TODO Analog
+       // If a normal key, and not pressed, do a layer cache lookup
+       if ( guide->type == 0x00 && guide->state != 0x01 )
+       {
+               // Cached layer
+               var_uint_t cachedLayer = macroTriggerListLayerCache[ scanCode ];
+
+               // Lookup map, then layer
+               nat_ptr_t **map = (nat_ptr_t**)LayerIndex[ cachedLayer ].triggerMap;
+               const Layer *layer = &LayerIndex[ cachedLayer ];
+
+               return map[ scanCode - layer->first ];
+       }
+
+       // If no trigger macro is defined at the given layer, fallthrough to the next layer
+       for ( uint16_t layerIndex = macroLayerIndexStackSize; layerIndex != 0xFFFF; layerIndex-- )
+       {
+               // Lookup Layer
+               const Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
+
+               // Check if latch has been pressed for this layer
+               // XXX Regardless of whether a key is found, the latch is removed on first lookup
+               uint8_t latch = LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x02;
+               if ( latch && latch_expire )
+               {
+                       Macro_layerState( 0, 0, macroLayerIndexStack[ layerIndex ], 0x02 );
+               }
+
+               // Only use layer, if state is valid
+               // XOR each of the state bits
+               // If only two are enabled, do not use this state
+               if ( (LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x01) ^ (latch>>1) ^ ((LayerState[ macroLayerIndexStack[ layerIndex ] ] & 0x04)>>2) )
+               {
+                       // Lookup layer
+                       nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap;
+
+                       // Determine if layer has key defined
+                       // Make sure scanCode is between layer first and last scancodes
+                       if ( map != 0
+                         && scanCode <= layer->last
+                         && scanCode >= layer->first
+                         && *map[ scanCode - layer->first ] != 0 )
+                       {
+                               // Set the layer cache
+                               macroTriggerListLayerCache[ scanCode ] = macroLayerIndexStack[ layerIndex ];
+
+                               return map[ scanCode - layer->first ];
+                       }
+               }
+       }
+
+       // Do lookup on default layer
+       nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
+
+       // Lookup default layer
+       const Layer *layer = &LayerIndex[0];
+
+       // Make sure scanCode is between layer first and last scancodes
+       if ( map != 0
+         && scanCode <= layer->last
+         && scanCode >= layer->first
+         && *map[ scanCode - layer->first ] != 0 )
+       {
+               // Set the layer cache to default map
+               macroTriggerListLayerCache[ scanCode ] = 0;
+
+               return map[ scanCode - layer->first ];
+       }
+
+       // Otherwise no defined Trigger Macro
+       erro_msg("Scan Code has no defined Trigger Macro: ");
+       printHex( scanCode );
+       print( NL );
+       return 0;
 }
 
 
+// Add an interconnect ScanCode
+// These are handled differently (less information is sent, hold/off states must be assumed)
+#if defined(ConnectEnabled_define)
+inline void Macro_interconnectAdd( void *trigger_ptr )
+{
+       TriggerGuide *trigger = (TriggerGuide*)trigger_ptr;
+
+       // Error checking
+       uint8_t error = 0;
+       switch ( trigger->type )
+       {
+       case 0x00: // Normal key
+               switch ( trigger->state )
+               {
+               case 0x00:
+               case 0x01:
+               case 0x02:
+               case 0x03:
+                       break;
+               default:
+                       erro_msg("Invalid key state - ");
+                       error = 1;
+                       break;
+               }
+               break;
+
+       // Invalid TriggerGuide type
+       default:
+               erro_msg("Invalid type - ");
+               error = 1;
+               break;
+       }
+
+       // Check if ScanCode is out of range
+       if ( trigger->scanCode > MaxScanCode )
+       {
+               warn_msg("ScanCode is out of range/not defined - ");
+               error = 1;
+       }
+
+       // Display TriggerGuide
+       if ( error )
+       {
+               printHex( trigger->type );
+               print(" ");
+               printHex( trigger->state );
+               print(" ");
+               printHex( trigger->scanCode );
+               print( NL );
+               return;
+       }
+
+       // Add trigger to the Interconnect Cache
+       // During each processing loop, a scancode may be re-added depending on it's state
+       for ( uint8_t c = 0; c < macroInterconnectCacheSize; c++ )
+       {
+               // Check if the same ScanCode
+               if ( macroInterconnectCache[ c ].scanCode == trigger->scanCode )
+               {
+                       // Update the state
+                       macroInterconnectCache[ c ].state = trigger->state;
+                       return;
+               }
+       }
+
+       // If not in the list, add it
+       macroInterconnectCache[ macroInterconnectCacheSize++ ] = *trigger;
+}
+#endif
+
+
 // Update the scancode key state
 // States:
-//   * 0x00 - Reserved
+//   * 0x00 - Off
 //   * 0x01 - Pressed
 //   * 0x02 - Held
 //   * 0x03 - Released
 //   * 0x04 - Unpressed (this is currently ignored)
 inline void Macro_keyState( uint8_t scanCode, uint8_t state )
 {
+#if defined(ConnectEnabled_define)
+       // Only compile in if a Connect node module is available
+       if ( !Connect_master )
+       {
+               // ScanCodes are only added if there was a state change (on/off)
+               switch ( state )
+               {
+               case 0x00: // Off
+               case 0x02: // Held
+                       return;
+               }
+       }
+#endif
+
        // Only add to macro trigger list if one of three states
        switch ( state )
        {
        case 0x01: // Pressed
        case 0x02: // Held
        case 0x03: // Released
-               macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = scanCode;
-               macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = state;
+               // Check if ScanCode is out of range
+               if ( scanCode > MaxScanCode )
+               {
+                       warn_msg("ScanCode is out of range/not defined: ");
+                       printHex( scanCode );
+                       print( NL );
+                       return;
+               }
+
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x00; // Normal key
+               macroTriggerListBufferSize++;
                break;
        }
 }
@@ -130,212 +556,635 @@ inline void Macro_keyState( uint8_t scanCode, uint8_t state )
 
 // Update the scancode analog state
 // States:
-//   * 0x00      - Reserved
+//   * 0x00      - Off
 //   * 0x01      - Released
 //   * 0x02-0xFF - Analog value (low to high)
 inline void Macro_analogState( uint8_t scanCode, uint8_t state )
 {
-       // TODO
+       // Only add to macro trigger list if non-off
+       // TODO Handle change for interconnect
+       if ( state != 0x00 )
+       {
+               // Check if ScanCode is out of range
+               if ( scanCode > MaxScanCode )
+               {
+                       warn_msg("ScanCode is out of range/not defined: ");
+                       printHex( scanCode );
+                       print( NL );
+                       return;
+               }
+
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x02; // Analog key
+               macroTriggerListBufferSize++;
+       }
 }
 
 
 // Update led state
 // States:
-//   * 0x00 - Reserved
+//   * 0x00 - Off
 //   * 0x01 - On
-//   * 0x02 - Off
 inline void Macro_ledState( uint8_t ledCode, uint8_t state )
 {
-       // TODO
+       // Only add to macro trigger list if non-off
+       // TODO Handle change for interconnect
+       if ( state != 0x00 )
+       {
+               // Check if LedCode is out of range
+               // TODO
+
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x01; // LED key
+               macroTriggerListBufferSize++;
+       }
 }
 
 
-// Evaluate/Update the TriggerMacro
-void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
+// Append result macro to pending list, checking for duplicates
+// Do nothing if duplicate
+inline void Macro_appendResultMacroToPendingList( const TriggerMacro *triggerMacro )
 {
-       // Which combo in the sequence is being evaluated
-       unsigned int comboPos = triggerMacro->pos;
+       // Lookup result macro index
+       var_uint_t resultMacroIndex = triggerMacro->result;
 
-       // If combo length is more than 1, cancel trigger macro if an incorrect key is found
-       uint8_t comboLength = triggerMacro->guide[ comboPos ];
+       // Iterate through result macro pending list, making sure this macro hasn't been added yet
+       for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
+       {
+               // If duplicate found, do nothing
+               if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
+                       return;
+       }
 
-       // Iterate over list of keys currently pressed
-       for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed += 2 )
+       // No duplicates found, add to pending list
+       macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
+
+       // Lookup scanCode of the last key in the last combo
+       var_uint_t pos = 0;
+       for ( uint8_t comboLength = triggerMacro->guide[0]; comboLength > 0; )
        {
-               // Compare with keys in combo
-               for ( unsigned int comboKey = 0; comboKey < comboLength; comboKey++ )
-               {
-                       // Lookup key in combo
-                       uint8_t guideKey = triggerMacro->guide[ comboPos + comboKey + 2 ]; // TODO Only Press/Hold/Release atm
+               pos += TriggerGuideSize * comboLength + 1;
+               comboLength = triggerMacro->guide[ pos ];
+       }
 
-                       // Sequence Case
-                       if ( comboLength == 1 )
-                       {
-                               // If key matches and only 1 key pressed, increment the TriggerMacro combo position
-                               if ( guideKey == macroTriggerListBuffer[ keyPressed ] && macroTriggerListBufferSize == 1 )
-                               {
-                                       triggerMacro->pos += comboLength * 2 + 1;
-                                       // TODO check if TriggerMacro is finished, register ResultMacro
-                                       return;
-                               }
+       uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode;
 
-                               // If key does not match or more than 1 key pressed, reset the TriggerMacro combo position
-                               triggerMacro->pos = 0;
-                               return;
-                       }
-                       // Combo Case
-                       else
-                       {
-                               // TODO
-                       }
+       // Lookup scanCode in buffer list for the current state and stateType
+       for ( uint8_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ )
+       {
+               if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode )
+               {
+                       ResultMacroRecordList[ resultMacroIndex ].state     = macroTriggerListBuffer[ keyIndex ].state;
+                       ResultMacroRecordList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type;
                }
        }
+
+       // Reset the macro position
+       ResultMacroRecordList[ resultMacroIndex ].pos = 0;
 }
 
 
+// Determine if long ResultMacro (more than 1 seqence element)
+inline uint8_t Macro_isLongResultMacro( const ResultMacro *macro )
+{
+       // Check the second sequence combo length
+       // If non-zero return non-zero (long sequence)
+       // 0 otherwise (short sequence)
+       var_uint_t position = 1;
+       for ( var_uint_t result = 0; result < macro->guide[0]; result++ )
+               position += ResultGuideSize( (ResultGuide*)&macro->guide[ position ] );
+       return macro->guide[ position ];
+}
 
 
-/*
-inline void Macro_bufferAdd( uint8_t byte )
+// Determine if long TriggerMacro (more than 1 sequence element)
+inline uint8_t Macro_isLongTriggerMacro( const TriggerMacro *macro )
 {
-       // Make sure we haven't overflowed the key buffer
-       // Default function for adding keys to the KeyIndex_Buffer, does a DefaultMap_Lookup
-       if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
+       // Check the second sequence combo length
+       // If non-zero return non-zero (long sequence)
+       // 0 otherwise (short sequence)
+       return macro->guide[ macro->guide[0] * TriggerGuideSize + 1 ];
+}
+
+
+// Votes on the given key vs. guide, short macros
+inline TriggerMacroVote Macro_evalShortTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
+{
+       // Depending on key type
+       switch ( guide->type )
        {
-               uint8_t key = DefaultMap_Lookup[byte];
-               for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
+       // Normal State Type
+       case 0x00:
+               // For short TriggerMacros completely ignore incorrect keys
+               if ( guide->scanCode == key->scanCode )
                {
-                       // Key already in the buffer
-                       if ( KeyIndex_Buffer[c] == key )
-                               return;
+                       switch ( key->state )
+                       {
+                       // Correct key, pressed, possible passing
+                       case 0x01:
+                               return TriggerMacroVote_Pass;
+
+                       // Correct key, held, possible passing or release
+                       case 0x02:
+                               return TriggerMacroVote_PassRelease;
+
+                       // Correct key, released, possible release
+                       case 0x03:
+                               return TriggerMacroVote_Release;
+                       }
                }
 
-               // Add to the buffer
-               KeyIndex_Buffer[KeyIndex_BufferUsed++] = key;
+               return TriggerMacroVote_DoNothing;
+
+       // LED State Type
+       case 0x01:
+               erro_print("LED State Type - Not implemented...");
+               break;
+
+       // Analog State Type
+       case 0x02:
+               erro_print("Analog State Type - Not implemented...");
+               break;
+
+       // Invalid State Type
+       default:
+               erro_print("Invalid State Type. This is a bug.");
+               break;
        }
+
+       // XXX Shouldn't reach here
+       return TriggerMacroVote_Invalid;
 }
 
-inline void Macro_bufferRemove( uint8_t byte )
-{
-       uint8_t key = DefaultMap_Lookup[byte];
 
-       // Check for the released key, and shift the other keys lower on the buffer
-       for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
+// Votes on the given key vs. guide, long macros
+// A long macro is defined as a guide with more than 1 combo
+inline TriggerMacroVote Macro_evalLongTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
+{
+       // Depending on key type
+       switch ( guide->type )
        {
-               // Key to release found
-               if ( KeyIndex_Buffer[c] == key )
+       // Normal State Type
+       case 0x00:
+               // Depending on the state of the buffered key, make voting decision
+               // Incorrect key
+               if ( guide->scanCode != key->scanCode )
                {
-                       // Shift keys from c position
-                       for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
-                               KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
+                       switch ( key->state )
+                       {
+                       // Wrong key, pressed, fail
+                       case 0x01:
+                               return TriggerMacroVote_Fail;
 
-                       // Decrement Buffer
-                       KeyIndex_BufferUsed--;
+                       // Wrong key, held, do not pass (no effect)
+                       case 0x02:
+                               return TriggerMacroVote_DoNothing;
 
-                       return;
+                       // Wrong key released, fail out if pos == 0
+                       case 0x03:
+                               return TriggerMacroVote_DoNothing | TriggerMacroVote_DoNothingRelease;
+                       }
+               }
+
+               // Correct key
+               else
+               {
+                       switch ( key->state )
+                       {
+                       // Correct key, pressed, possible passing
+                       case 0x01:
+                               return TriggerMacroVote_Pass;
+
+                       // Correct key, held, possible passing or release
+                       case 0x02:
+                               return TriggerMacroVote_PassRelease;
+
+                       // Correct key, released, possible release
+                       case 0x03:
+                               return TriggerMacroVote_Release;
+                       }
                }
+
+               break;
+
+       // LED State Type
+       case 0x01:
+               erro_print("LED State Type - Not implemented...");
+               break;
+
+       // Analog State Type
+       case 0x02:
+               erro_print("Analog State Type - Not implemented...");
+               break;
+
+       // Invalid State Type
+       default:
+               erro_print("Invalid State Type. This is a bug.");
+               break;
        }
 
-       // Error case (no key to release)
-       erro_msg("Could not find key to release: ");
-       printHex( key );
+       // XXX Shouldn't reach here
+       return TriggerMacroVote_Invalid;
 }
-*/
 
-inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
-{
-}
 
-inline void Macro_process()
+// Evaluate/Update TriggerMacro
+TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex )
 {
-       // Only do one round of macro processing between Output Module timer sends
-       if ( USBKeys_Sent != 0 )
-               return;
+       // Lookup TriggerMacro
+       const TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
+       TriggerMacroRecord *record = &TriggerMacroRecordList[ triggerMacroIndex ];
 
-       // If the pause flag is set, only process if the step counter is non-zero
-       if ( macroPauseMode && macroStepCounter == 0 )
+       // Check if macro has finished and should be incremented sequence elements
+       if ( record->state == TriggerMacro_Release )
        {
-               return;
+               record->state = TriggerMacro_Waiting;
+               record->pos = record->pos + macro->guide[ record->pos ] * TriggerGuideSize + 1;
        }
-       // Proceed, decrementing the step counter
-       else
+
+       // Current Macro position
+       var_uint_t pos = record->pos;
+
+       // Length of the combo being processed
+       uint8_t comboLength = macro->guide[ pos ] * TriggerGuideSize;
+
+       // If no combo items are left, remove the TriggerMacro from the pending list
+       if ( comboLength == 0 )
        {
-               macroStepCounter--;
+               return TriggerMacroEval_Remove;
        }
 
-       // Loop through macro trigger buffer
-       for ( uint8_t index = 0; index < macroTriggerListBufferSize; index += 2 )
+       // Check if this is a long Trigger Macro
+       uint8_t longMacro = Macro_isLongTriggerMacro( macro );
+
+       // Iterate through the items in the combo, voting the on the key state
+       // If any of the pressed keys do not match, fail the macro
+       //
+       // The macro is waiting for input when in the TriggerMacro_Waiting state
+       // Once all keys have been pressed/held (only those keys), entered TriggerMacro_Press state (passing)
+       // Transition to the next combo (if it exists) when a single key is released (TriggerMacro_Release state)
+       // On scan after position increment, change to TriggerMacro_Waiting state
+       // TODO Add support for system LED states (NumLock, CapsLock, etc.)
+       // TODO Add support for analog key states
+       // TODO Add support for 0x00 Key state (not pressing a key, not all that useful in general)
+       // TODO Add support for Press/Hold/Release differentiation when evaluating (not sure if useful)
+       TriggerMacroVote overallVote = TriggerMacroVote_Invalid;
+       for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
        {
-               // Get scanCode, first item of macroTriggerListBuffer pairs
-               uint8_t scanCode = macroTriggerListBuffer[ index ];
+               // Assign TriggerGuide element (key type, state and scancode)
+               TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ comboItem ]);
 
-               // Lookup trigger list for this key
-               unsigned int *triggerList = Macro_layerLookup( scanCode );
+               TriggerMacroVote vote = TriggerMacroVote_Invalid;
+               // Iterate through the key buffer, comparing to each key in the combo
+               for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
+               {
+                       // Lookup key information
+                       TriggerGuide *keyInfo = &macroTriggerListBuffer[ key ];
+
+                       // If vote is a pass (>= 0x08, no more keys in the combo need to be looked at)
+                       // Also mask all of the non-passing votes
+                       vote |= longMacro
+                               ? Macro_evalLongTriggerMacroVote( keyInfo, guide )
+                               : Macro_evalShortTriggerMacroVote( keyInfo, guide );
+                       if ( vote >= TriggerMacroVote_Pass )
+                       {
+                               vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
+                               break;
+                       }
+               }
 
-               // The first element is the length of the trigger list
-               unsigned int triggerListSize = triggerList[0];
+               // If no pass vote was found after scanning all of the keys
+               // Fail the combo, if this is a short macro (long macros already will have a fail vote)
+               if ( !longMacro && vote < TriggerMacroVote_Pass )
+                       vote |= TriggerMacroVote_Fail;
 
-               // Loop through the trigger list
-               for ( unsigned int trigger = 0; trigger < triggerListSize; trigger++ )
-               {
-                       // Lookup TriggerMacro
-                       TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
+               // After voting, append to overall vote
+               overallVote |= vote;
+       }
+
+       // If no pass vote was found after scanning the entire combo
+       // And this is the first position in the combo, just remove it (nothing important happened)
+       if ( longMacro && overallVote & TriggerMacroVote_DoNothingRelease && pos == 0 )
+               overallVote |= TriggerMacroVote_Fail;
+
+       // Decide new state of macro after voting
+       // Fail macro, remove from pending list
+       if ( overallVote & TriggerMacroVote_Fail )
+       {
+               return TriggerMacroEval_Remove;
+       }
+       // Do nothing, incorrect key is being held or released
+       else if ( overallVote & TriggerMacroVote_DoNothing && longMacro )
+       {
+               // Just doing nothing :)
+       }
+       // If ready for transition and in Press state, set to Waiting and increment combo position
+       // Position is incremented (and possibly remove the macro from the pending list) on the next iteration
+       else if ( overallVote & TriggerMacroVote_Release && record->state == TriggerMacro_Press )
+       {
+               record->state = TriggerMacro_Release;
 
-                       // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
-                       uint8_t state = macroTriggerListBuffer[ index + 1 ];
+               // If this is the last combo in the sequence, remove from the pending list
+               if ( macro->guide[ record->pos + macro->guide[ record->pos ] * TriggerGuideSize + 1 ] == 0 )
+                       return TriggerMacroEval_DoResultAndRemove;
+       }
+       // If passing and in Waiting state, set macro state to Press
+       else if ( overallVote & TriggerMacroVote_Pass
+            && ( record->state == TriggerMacro_Waiting || record->state == TriggerMacro_Press ) )
+       {
+               record->state = TriggerMacro_Press;
 
-                       // Evaluate Macro
-                       Macro_evalTriggerMacro( triggerMacro );
+               // If in press state, and this is the final combo, send request for ResultMacro
+               // Check to see if the result macro only has a single element
+               // If this result macro has more than 1 key, only send once
+               // TODO Add option to have long macro repeat rate
+               if ( macro->guide[ pos + comboLength + 1 ] == 0 )
+               {
+                       // Long result macro (more than 1 combo)
+                       if ( Macro_isLongResultMacro( &ResultMacroList[ macro->result ] ) )
+                       {
+                               // Only ever trigger result once, on press
+                               if ( overallVote == TriggerMacroVote_Pass )
+                               {
+                                       return TriggerMacroEval_DoResultAndRemove;
+                               }
+                       }
+                       // Short result macro
+                       else
+                       {
+                               // Only trigger result once, on press, if long trigger (more than 1 combo)
+                               if ( Macro_isLongTriggerMacro( macro ) )
+                               {
+                                       return TriggerMacroEval_DoResultAndRemove;
+                               }
+                               // Otherwise, trigger result continuously
+                               else
+                               {
+                                       return TriggerMacroEval_DoResult;
+                               }
+                       }
                }
        }
+       // Otherwise, just remove the macro on key release
+       // One more result has to be called to indicate to the ResultMacro that the key transitioned to the release state
+       else if ( overallVote & TriggerMacroVote_Release )
+       {
+               return TriggerMacroEval_DoResultAndRemove;
+       }
 
+       // If this is a short macro, just remove it
+       // The state can be rebuilt on the next iteration
+       if ( !longMacro )
+               return TriggerMacroEval_Remove;
 
+       return TriggerMacroEval_DoNothing;
+}
+
+
+// Evaluate/Update ResultMacro
+inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex )
+{
+       // Lookup ResultMacro
+       const ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
+       ResultMacroRecord *record = &ResultMacroRecordList[ resultMacroIndex ];
+
+       // Current Macro position
+       var_uint_t pos = record->pos;
+
+       // Length of combo being processed
+       uint8_t comboLength = macro->guide[ pos ];
 
+       // Function Counter, used to keep track of the combo items processed
+       var_uint_t funcCount = 0;
 
+       // Combo Item Position within the guide
+       var_uint_t comboItem = pos + 1;
 
-       /* TODO
-       // Loop through input buffer
-       for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
+       // Iterate through the Result Combo
+       while ( funcCount < comboLength )
        {
-               //print(" KEYS: ");
-               //printInt8( KeyIndex_BufferUsed );
-               // Get the keycode from the buffer
-               uint8_t key = KeyIndex_Buffer[index];
+               // Assign TriggerGuide element (key type, state and scancode)
+               ResultGuide *guide = (ResultGuide*)(&macro->guide[ comboItem ]);
 
-               // Set the modifier bit if this key is a modifier
-               if ( (key & KEY_LCTRL) == KEY_LCTRL ) // AND with 0xE0
-               {
-                       USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
+               // Do lookup on capability function
+               void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(CapabilitiesList[ guide->index ].func);
+
+               // Call capability
+               capability( record->state, record->stateType, &guide->args );
+
+               // Increment counters
+               funcCount++;
+               comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
+       }
+
+       // Move to next item in the sequence
+       record->pos = comboItem;
 
-                       // Modifier processed, move on to the next key
+       // If the ResultMacro is finished, remove
+       if ( macro->guide[ comboItem ] == 0 )
+       {
+               record->pos = 0;
+               return ResultMacroEval_Remove;
+       }
+
+       // Otherwise leave the macro in the list
+       return ResultMacroEval_DoNothing;
+}
+
+
+// Update pending trigger list
+inline void Macro_updateTriggerMacroPendingList()
+{
+       // Iterate over the macroTriggerListBuffer to add any new Trigger Macros to the pending list
+       for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
+       {
+               // TODO LED States
+               // TODO Analog Switches
+               // Only add TriggerMacro to pending list if key was pressed (not held, released or off)
+               if ( macroTriggerListBuffer[ key ].state == 0x00 && macroTriggerListBuffer[ key ].state != 0x01 )
                        continue;
+
+               // TODO Analog
+               // If this is a release case, indicate to layer lookup for possible latch expiry
+               uint8_t latch_expire = macroTriggerListBuffer[ key ].state == 0x03;
+
+               // Lookup Trigger List
+               nat_ptr_t *triggerList = Macro_layerLookup( &macroTriggerListBuffer[ key ], latch_expire );
+
+               // If there was an error during lookup, skip
+               if ( triggerList == 0 )
+                       continue;
+
+               // Number of Triggers in list
+               nat_ptr_t triggerListSize = triggerList[0];
+
+               // Iterate over triggerList to see if any TriggerMacros need to be added
+               // First item is the number of items in the TriggerList
+               for ( var_uint_t macro = 1; macro < triggerListSize + 1; macro++ )
+               {
+                       // Lookup trigger macro index
+                       var_uint_t triggerMacroIndex = triggerList[ macro ];
+
+                       // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
+                       //  triggerList needs to be added
+                       var_uint_t pending = 0;
+                       for ( ; pending < macroTriggerMacroPendingListSize; pending++ )
+                       {
+                               // Stop scanning if the trigger macro index is found in the pending list
+                               if ( macroTriggerMacroPendingList[ pending ] == triggerMacroIndex )
+                                       break;
+                       }
+
+                       // If the triggerMacroIndex (macro) was not found in the macroTriggerMacroPendingList
+                       // Add it to the list
+                       if ( pending == macroTriggerMacroPendingListSize )
+                       {
+                               macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex;
+
+                               // Reset macro position
+                               TriggerMacroRecordList[ triggerMacroIndex ].pos   = 0;
+                               TriggerMacroRecordList[ triggerMacroIndex ].state = TriggerMacro_Waiting;
+                       }
+               }
+       }
+}
+
+
+// Macro Procesing Loop
+// Called once per USB buffer send
+inline void Macro_process()
+{
+#if defined(ConnectEnabled_define)
+       // Only compile in if a Connect node module is available
+       // If this is a interconnect slave node, send all scancodes to master node
+       if ( !Connect_master )
+       {
+               if ( macroTriggerListBufferSize > 0 )
+               {
+                       Connect_send_ScanCode( Connect_id, macroTriggerListBuffer, macroTriggerListBufferSize );
+                       macroTriggerListBufferSize = 0;
                }
+               return;
+       }
+#endif
 
-               // Too many keys
-               if ( USBKeys_Sent >= USBKeys_MaxSize )
+       // Only do one round of macro processing between Output Module timer sends
+       if ( USBKeys_Sent != 0 )
+               return;
+
+#if defined(ConnectEnabled_define)
+       // Check if there are any ScanCodes in the interconnect cache to process
+       if ( Connect_master && macroInterconnectCacheSize > 0 )
+       {
+               // Iterate over all the cache ScanCodes
+               uint8_t currentInterconnectCacheSize = macroInterconnectCacheSize;
+               macroInterconnectCacheSize = 0;
+               for ( uint8_t c = 0; c < currentInterconnectCacheSize; c++ )
                {
-                       warn_msg("USB Key limit reached");
-                       errorLED( 1 );
-                       break;
+                       // Add to the trigger list
+                       macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = macroInterconnectCache[ c ];
+
+                       // TODO Handle other TriggerGuide types (e.g. analog)
+                       switch ( macroInterconnectCache[ c ].type )
+                       {
+                       // Normal (Press/Hold/Release)
+                       case 0x00:
+                               // Decide what to do based on the current state
+                               switch ( macroInterconnectCache[ c ].state )
+                               {
+                               // Re-add to interconnect cache in hold state
+                               case 0x01: // Press
+                               //case 0x02: // Hold // XXX Why does this not work? -HaaTa
+                                       macroInterconnectCache[ c ].state = 0x02;
+                                       macroInterconnectCache[ macroInterconnectCacheSize++ ] = macroInterconnectCache[ c ];
+                                       break;
+                               case 0x03: // Remove
+                                       break;
+                               // Otherwise, do not re-add
+                               }
+                       }
                }
+       }
+#endif
+
+       // If the pause flag is set, only process if the step counter is non-zero
+       if ( macroPauseMode )
+       {
+               if ( macroStepCounter == 0 )
+                       return;
+
+               // Proceed, decrementing the step counter
+               macroStepCounter--;
+               dbug_print("Macro Step");
+       }
 
-               // Allow ignoring keys with 0's
-               if ( key != 0 )
+       // Update pending trigger list, before processing TriggerMacros
+       Macro_updateTriggerMacroPendingList();
+
+       // Tail pointer for macroTriggerMacroPendingList
+       // Macros must be explicitly re-added
+       var_uint_t macroTriggerMacroPendingListTail = 0;
+
+       // Iterate through the pending TriggerMacros, processing each of them
+       for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
+       {
+               switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
                {
-                       USBKeys_Array[USBKeys_Sent++] = key;
+               // Trigger Result Macro (purposely falling through)
+               case TriggerMacroEval_DoResult:
+                       // Append ResultMacro to PendingList
+                       Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
+
+               default:
+                       macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
+                       break;
+
+               // Trigger Result Macro and Remove (purposely falling through)
+               case TriggerMacroEval_DoResultAndRemove:
+                       // Append ResultMacro to PendingList
+                       Macro_appendResultMacroToPendingList( &TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ] );
+
+               // Remove Macro from Pending List, nothing to do, removing by default
+               case TriggerMacroEval_Remove:
+                       break;
                }
-               else
+       }
+
+       // Update the macroTriggerMacroPendingListSize with the tail pointer
+       macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
+
+
+       // Tail pointer for macroResultMacroPendingList
+       // Macros must be explicitly re-added
+       var_uint_t macroResultMacroPendingListTail = 0;
+
+       // Iterate through the pending ResultMacros, processing each of them
+       for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
+       {
+               switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
                {
-                       // Key was not mapped
-                       erro_msg( "Key not mapped... - " );
-                       printHex( key );
-                       errorLED( 1 );
+               // Re-add macros to pending list
+               case ResultMacroEval_DoNothing:
+               default:
+                       macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
+                       break;
+
+               // Remove Macro from Pending List, nothing to do, removing by default
+               case ResultMacroEval_Remove:
+                       break;
                }
        }
-       */
+
+       // Update the macroResultMacroPendingListSize with the tail pointer
+       macroResultMacroPendingListSize = macroResultMacroPendingListTail;
 
        // Signal buffer that we've used it
-       Scan_finishedWithBuffer( KeyIndex_BufferUsed );
+       Scan_finishedWithMacro( macroTriggerListBufferSize );
+
+       // Reset TriggerList buffer
+       macroTriggerListBufferSize = 0;
 
        // If Macro debug mode is set, clear the USB Buffer
        if ( macroDebugMode )
@@ -345,6 +1194,7 @@ inline void Macro_process()
        }
 }
 
+
 inline void Macro_setup()
 {
        // Register Macro CLI dictionary
@@ -361,6 +1211,21 @@ inline void Macro_setup()
 
        // Make sure macro trigger buffer is empty
        macroTriggerListBufferSize = 0;
+
+       // Initialize TriggerMacro states
+       for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
+       {
+               TriggerMacroRecordList[ macro ].pos   = 0;
+               TriggerMacroRecordList[ macro ].state = TriggerMacro_Waiting;
+       }
+
+       // Initialize ResultMacro states
+       for ( var_uint_t macro = 0; macro < ResultMacroNum; macro++ )
+       {
+               ResultMacroRecordList[ macro ].pos       = 0;
+               ResultMacroRecordList[ macro ].state     = 0;
+               ResultMacroRecordList[ macro ].stateType = 0;
+       }
 }
 
 
@@ -369,10 +1234,11 @@ inline void Macro_setup()
 void cliFunc_capList( char* args )
 {
        print( NL );
-       info_msg("Capabilities List");
+       info_msg("Capabilities List ");
+       printHex( CapabilitiesNum );
 
        // Iterate through all of the capabilities and display them
-       for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
+       for ( var_uint_t cap = 0; cap < CapabilitiesNum; cap++ )
        {
                print( NL "\t" );
                printHex( cap );
@@ -392,15 +1258,15 @@ void cliFunc_capSelect( char* args )
        char* arg2Ptr = args;
 
        // Total number of args to scan (must do a lookup if a keyboard capability is selected)
-       unsigned int totalArgs = 2; // Always at least two args
-       unsigned int cap = 0;
+       var_uint_t totalArgs = 2; // Always at least two args
+       var_uint_t cap = 0;
 
        // Arguments used for keyboard capability function
-       unsigned int argSetCount = 0;
+       var_uint_t argSetCount = 0;
        uint8_t *argSet = (uint8_t*)args;
 
        // Process all args
-       for ( unsigned int c = 0; argSetCount < totalArgs; c++ )
+       for ( var_uint_t c = 0; argSetCount < totalArgs; c++ )
        {
                curArgs = arg2Ptr;
                CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
@@ -416,7 +1282,7 @@ void cliFunc_capSelect( char* args )
                // Keyboard Capability
                case 'K':
                        // Determine capability index
-                       cap = decToInt( &arg1Ptr[1] );
+                       cap = numToInt( &arg1Ptr[1] );
 
                        // Lookup the number of args
                        totalArgs += CapabilitiesList[ cap ].argCount;
@@ -425,7 +1291,7 @@ void cliFunc_capSelect( char* args )
 
                // Because allocating memory isn't doable, and the argument count is arbitrary
                // The argument pointer is repurposed as the argument list (much smaller anyways)
-               argSet[ argSetCount++ ] = (uint8_t)decToInt( arg1Ptr );
+               argSet[ argSetCount++ ] = (uint8_t)numToInt( arg1Ptr );
 
                // Once all the arguments are prepared, call the keyboard capability function
                if ( argSetCount == totalArgs )
@@ -448,6 +1314,34 @@ void cliFunc_capSelect( char* args )
        }
 }
 
+void cliFunc_keyHold( char* args )
+{
+       // Parse codes from arguments
+       char* curArgs;
+       char* arg1Ptr;
+       char* arg2Ptr = args;
+
+       // Process all args
+       for ( ;; )
+       {
+               curArgs = arg2Ptr;
+               CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
+
+               // Stop processing args if no more are found
+               if ( *arg1Ptr == '\0' )
+                       break;
+
+               // Ignore non-Scancode numbers
+               switch ( arg1Ptr[0] )
+               {
+               // Scancode
+               case 'S':
+                       Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
+                       break;
+               }
+       }
+}
+
 void cliFunc_keyPress( char* args )
 {
        // Parse codes from arguments
@@ -470,7 +1364,7 @@ void cliFunc_keyPress( char* args )
                {
                // Scancode
                case 'S':
-                       Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
+                       Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x01 ); // Press scancode
                        break;
                }
        }
@@ -498,26 +1392,36 @@ void cliFunc_keyRelease( char* args )
                {
                // Scancode
                case 'S':
-                       Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
+                       Macro_keyState( (uint8_t)numToInt( &arg1Ptr[1] ), 0x03 ); // Release scancode
                        break;
                }
        }
 }
 
+void cliFunc_layerDebug( char *args )
+{
+       // Toggle layer debug mode
+       layerDebugMode = layerDebugMode ? 0 : 1;
+
+       print( NL );
+       info_msg("Layer Debug Mode: ");
+       printInt8( layerDebugMode );
+}
+
 void cliFunc_layerList( char* args )
 {
        print( NL );
        info_msg("Layer List");
 
        // Iterate through all of the layers and display them
-       for ( unsigned int layer = 0; layer < LayerNum; layer++ )
+       for ( uint16_t layer = 0; layer < LayerNum; layer++ )
        {
                print( NL "\t" );
                printHex( layer );
                print(" - ");
 
                // Display layer name
-               dPrint( LayerIndex[ layer ].name );
+               dPrint( (char*)LayerIndex[ layer ].name );
 
                // Default map
                if ( layer == 0 )
@@ -525,11 +1429,13 @@ void cliFunc_layerList( char* args )
 
                // Layer State
                print( NL "\t\t Layer State: " );
-               printHex( LayerIndex[ layer ].state );
+               printHex( LayerState[ layer ] );
 
-               // Max Index
-               print(" Max Index: ");
-               printHex( LayerIndex[ layer ].max );
+               // First -> Last Indices
+               print(" First -> Last Indices: ");
+               printHex( LayerIndex[ layer ].first );
+               print(" -> ");
+               printHex( LayerIndex[ layer ].last );
        }
 }
 
@@ -560,11 +1466,11 @@ void cliFunc_layerState( char* args )
                        if ( arg1Ptr[0] != 'L' )
                                return;
 
-                       arg1 = (uint8_t)decToInt( &arg1Ptr[1] );
+                       arg1 = (uint8_t)numToInt( &arg1Ptr[1] );
                        break;
                // Second argument (e.g. 4)
                case 1:
-                       arg2 = (uint8_t)decToInt( arg1Ptr );
+                       arg2 = (uint8_t)numToInt( arg1Ptr );
 
                        // Display operation (to indicate that it worked)
                        print( NL );
@@ -574,7 +1480,7 @@ void cliFunc_layerState( char* args )
                        printHex( arg2 );
 
                        // Set the layer state
-                       LayerIndex[ arg1 ].state = arg2;
+                       LayerState[ arg1 ] = arg2;
                        break;
                }
        }
@@ -592,6 +1498,39 @@ void cliFunc_macroDebug( char* args )
 
 void cliFunc_macroList( char* args )
 {
+       // Show pending key events
+       print( NL );
+       info_msg("Pending Key Events: ");
+       printInt16( (uint16_t)macroTriggerListBufferSize );
+       print(" : ");
+       for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
+       {
+               printHex( macroTriggerListBuffer[ key ].scanCode );
+               print(" ");
+       }
+
+       // Show pending trigger macros
+       print( NL );
+       info_msg("Pending Trigger Macros: ");
+       printInt16( (uint16_t)macroTriggerMacroPendingListSize );
+       print(" : ");
+       for ( var_uint_t macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
+       {
+               printHex( macroTriggerMacroPendingList[ macro ] );
+               print(" ");
+       }
+
+       // Show pending result macros
+       print( NL );
+       info_msg("Pending Result Macros: ");
+       printInt16( (uint16_t)macroResultMacroPendingListSize );
+       print(" : ");
+       for ( var_uint_t macro = 0; macro < macroResultMacroPendingListSize; macro++ )
+       {
+               printHex( macroResultMacroPendingList[ macro ] );
+               print(" ");
+       }
+
        // Show available trigger macro indices
        print( NL );
        info_msg("Trigger Macros Range: T0 -> T");
@@ -605,7 +1544,7 @@ void cliFunc_macroList( char* args )
        // Show Trigger to Result Macro Links
        print( NL );
        info_msg("Trigger : Result Macro Pairs");
-       for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
+       for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
        {
                print( NL );
                print("\tT");
@@ -625,14 +1564,15 @@ void cliFunc_macroProc( char* args )
        printInt8( macroPauseMode );
 }
 
-void macroDebugShowTrigger( unsigned int index )
+void macroDebugShowTrigger( var_uint_t index )
 {
        // Only proceed if the macro exists
        if ( index >= TriggerMacroNum )
                return;
 
        // Trigger Macro Show
-       TriggerMacro *macro = &TriggerMacroList[ index ];
+       const TriggerMacro *macro = &TriggerMacroList[ index ];
+       TriggerMacroRecord *record = &TriggerMacroRecordList[ index ];
 
        print( NL );
        info_msg("Trigger Macro Index: ");
@@ -640,14 +1580,14 @@ void macroDebugShowTrigger( unsigned int index )
        print( NL );
 
        // Read the comboLength for combo in the sequence (sequence of combos)
-       unsigned int pos = 0;
+       var_uint_t pos = 0;
        uint8_t comboLength = macro->guide[ pos ];
 
        // Iterate through and interpret the guide
        while ( comboLength != 0 )
        {
                // Initial position of the combo
-               unsigned int comboPos = ++pos;
+               var_uint_t comboPos = ++pos;
 
                // Iterate through the combo
                while ( pos < comboLength * TriggerGuideSize + comboPos )
@@ -656,7 +1596,7 @@ void macroDebugShowTrigger( unsigned int index )
                        TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ pos ]);
 
                        // Display guide information about trigger key
-                       printHex( guide->scancode );
+                       printHex( guide->scanCode );
                        print("|");
                        printHex( guide->type );
                        print("|");
@@ -680,21 +1620,31 @@ void macroDebugShowTrigger( unsigned int index )
 
        // Display current position
        print( NL "Position: " );
-       printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
+       printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
 
        // Display result macro index
        print( NL "Result Macro Index: " );
        printInt16( (uint16_t)macro->result ); // Hopefully large enough :P (can't assume 32-bit)
+
+       // Display trigger macro state
+       print( NL "Trigger Macro State: " );
+       switch ( record->state )
+       {
+       case TriggerMacro_Press:   print("Press");   break;
+       case TriggerMacro_Release: print("Release"); break;
+       case TriggerMacro_Waiting: print("Waiting"); break;
+       }
 }
 
-void macroDebugShowResult( unsigned int index )
+void macroDebugShowResult( var_uint_t index )
 {
        // Only proceed if the macro exists
        if ( index >= ResultMacroNum )
                return;
 
        // Trigger Macro Show
-       ResultMacro *macro = &ResultMacroList[ index ];
+       const ResultMacro *macro = &ResultMacroList[ index ];
+       ResultMacroRecord *record = &ResultMacroRecordList[ index ];
 
        print( NL );
        info_msg("Result Macro Index: ");
@@ -702,14 +1652,14 @@ void macroDebugShowResult( unsigned int index )
        print( NL );
 
        // Read the comboLength for combo in the sequence (sequence of combos)
-       unsigned int pos = 0;
+       var_uint_t pos = 0;
        uint8_t comboLength = macro->guide[ pos++ ];
 
        // Iterate through and interpret the guide
        while ( comboLength != 0 )
        {
                // Function Counter, used to keep track of the combos processed
-               unsigned int funcCount = 0;
+               var_uint_t funcCount = 0;
 
                // Iterate through the combo
                while ( funcCount < comboLength )
@@ -722,7 +1672,7 @@ void macroDebugShowResult( unsigned int index )
                        print("|");
 
                        // Display Function Ptr Address
-                       printHex( (unsigned int)CapabilitiesList[ guide->index ].func );
+                       printHex( (nat_ptr_t)CapabilitiesList[ guide->index ].func );
                        print("|");
 
                        // Display/Lookup Capability Name (utilize debug mode of capability)
@@ -731,7 +1681,7 @@ void macroDebugShowResult( unsigned int index )
 
                        // Display Argument(s)
                        print("(");
-                       for ( unsigned int arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
+                       for ( var_uint_t arg = 0; arg < CapabilitiesList[ guide->index ].argCount; arg++ )
                        {
                                // Arguments are only 8 bit values
                                printHex( (&guide->args)[ arg ] );
@@ -763,13 +1713,13 @@ void macroDebugShowResult( unsigned int index )
 
        // Display current position
        print( NL "Position: " );
-       printInt16( (uint16_t)macro->pos ); // Hopefully large enough :P (can't assume 32-bit)
+       printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit)
 
        // Display final trigger state/type
        print( NL "Final Trigger State (State/Type): " );
-       printHex( macro->state );
+       printHex( record->state );
        print("/");
-       printHex( macro->stateType );
+       printHex( record->stateType );
 }
 
 void cliFunc_macroShow( char* args )
@@ -794,11 +1744,11 @@ void cliFunc_macroShow( char* args )
                {
                // Indexed Trigger Macro
                case 'T':
-                       macroDebugShowTrigger( decToInt( &arg1Ptr[1] ) );
+                       macroDebugShowTrigger( numToInt( &arg1Ptr[1] ) );
                        break;
                // Indexed Result Macro
                case 'R':
-                       macroDebugShowResult( decToInt( &arg1Ptr[1] ) );
+                       macroDebugShowResult( numToInt( &arg1Ptr[1] ) );
                        break;
                }
        }
@@ -812,7 +1762,13 @@ void cliFunc_macroStep( char* args )
        char* arg2Ptr;
        CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
 
+       // Default to 1, if no argument given
+       var_uint_t count = (var_uint_t)numToInt( arg1Ptr );
+
+       if ( count == 0 )
+               count = 1;
+
        // Set the macro step counter, negative int's are cast to uint
-       macroStepCounter = (unsigned int)decToInt( arg1Ptr );
+       macroStepCounter = count;
 }