]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Macro/PartialMap/macro.c
Macro processing is now feature complete.
[kiibohd-controller.git] / Macro / PartialMap / macro.c
index a587b107abfdb12c52ee178a4a51ea2659acf049..5c3798cdd55290666dcfef47db0500daf3987819 100644 (file)
@@ -42,9 +42,8 @@ void cliFunc_capList   ( char* args );
 void cliFunc_capSelect ( char* args );
 void cliFunc_keyPress  ( char* args );
 void cliFunc_keyRelease( char* args );
-void cliFunc_layerLatch( char* args );
 void cliFunc_layerList ( char* args );
-void cliFunc_layerLock ( char* args );
+void cliFunc_layerState( char* args );
 void cliFunc_macroDebug( char* args );
 void cliFunc_macroList ( char* args );
 void cliFunc_macroProc ( char* args );
@@ -53,6 +52,32 @@ void cliFunc_macroStep ( char* args );
 
 
 
+// ----- Enums -----
+
+// Bit positions are important, passes (correct key) always trump incorrect key votes
+typedef enum TriggerMacroVote {
+       TriggerMacroVote_Release      = 0x8, // Correct key
+       TriggerMacroVote_PassRelease  = 0xC, // Correct key (both pass and release)
+       TriggerMacroVote_Pass         = 0x4, // Correct 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
@@ -62,9 +87,8 @@ CLIDictItem macroCLIDict[] = {
        { "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 },
-       { "layerLatch",  "Latch the specified indexed layer." NL "\t\t\033[35mL15\033[0m Indexed Layer 0x0F", cliFunc_layerLatch },
        { "layerList",   "List available layers.", cliFunc_layerList },
-       { "layerLock",   "Lock the specified indexed layer." NL "\t\t\033[35mL2\033[0m Indexed Layer 0x02", cliFunc_layerLock },
+       { "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 },
@@ -85,32 +109,65 @@ unsigned int 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
+TriggerGuide macroTriggerListBuffer[ MaxScanCode ];
 uint8_t macroTriggerListBufferSize = 0;
 
+// 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];
+unsigned int macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 };
+unsigned int macroTriggerMacroPendingListSize = 0;
+
+// Layer Index Stack
+//  * When modifying layer state and the state is non-0x0, the stack must be adjusted
+unsigned int macroLayerIndexStack[ LayerNum ] = { 0 };
+unsigned int macroLayerIndexStackSize = 0;
+
+// Pending Result Macro Index List
+//  * Any result macro that needs processing from a previous macro processing loop
+unsigned int macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
+unsigned int macroResultMacroPendingListSize = 0;
 
 
 
 // ----- Functions -----
 
 // Looks up the trigger list for the given scan code (from the active layer)
+// NOTE: Calling function must handle the NULL pointer case
 unsigned int *Macro_layerLookup( uint8_t scanCode )
 {
-       // TODO - No layer fallthrough lookup
-       return default_scanMap[ scanCode ];
+       // If no trigger macro is defined at the given layer, fallthrough to the next layer
+       for ( unsigned int layer = 0; layer < macroLayerIndexStackSize; layer++ )
+       {
+               // Lookup layer
+               unsigned int **map = LayerIndex[ macroLayerIndexStack[ layer ] ].triggerMap;
+
+               // Determine if layer has key defined
+               if ( map != 0 && *map[ scanCode ] != 0 )
+                       return map[ scanCode ];
+       }
+
+       // Do lookup on default layer
+       unsigned int **map = LayerIndex[0].triggerMap;
+
+       // Determine if layer has key defined
+       if ( map == 0 && *map[ scanCode ] == 0 )
+       {
+               erro_msg("Scan Code has no defined Trigger Macro: ");
+               printHex( scanCode );
+               return 0;
+       }
+
+       // Return lookup result
+       return map[ scanCode ];
 }
 
 
 // Update the scancode key state
 // States:
-//   * 0x00 - Reserved
+//   * 0x00 - Off
 //   * 0x01 - Pressed
 //   * 0x02 - Held
 //   * 0x03 - Released
@@ -123,8 +180,10 @@ inline void Macro_keyState( uint8_t scanCode, uint8_t state )
        case 0x01: // Pressed
        case 0x02: // Held
        case 0x03: // Released
-               macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = scanCode;
-               macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = state;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
+               macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x00; // Normal key
+               macroTriggerListBufferSize++;
                break;
        }
 }
@@ -132,122 +191,328 @@ 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
+       if ( state != 0x00 )
+       {
+               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
+       if ( state != 0x00 )
+       {
+               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( unsigned int resultMacroIndex )
 {
-       // Which combo in the sequence is being evaluated
-       unsigned int comboPos = triggerMacro->pos;
+       // Iterate through result macro pending list, making sure this macro hasn't been added yet
+       for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
+       {
+               // If duplicate found, do nothing
+               if ( macroResultMacroPendingList[ macro ] == resultMacroIndex )
+                       return;
+       }
+
+       // No duplicates found, add to pending list
+       macroResultMacroPendingList[ macroResultMacroPendingListSize++ ] = resultMacroIndex;
+}
+
+
+// Determine if long ResultMacro (more than 1 seqence element)
+inline uint8_t Macro_isLongResultMacro( ResultMacro *macro )
+{
+       // Check the second sequence combo length
+       // If non-zero return 1 (long sequence)
+       // 0 otherwise (short sequence)
+       return macro->guide[ macro->guide[0] * ResultGuideSize( (ResultGuide*)macro->guide ) ] > 0 ? 1 : 0;
+}
 
-       // If combo length is more than 1, cancel trigger macro if an incorrect key is found
-       uint8_t comboLength = triggerMacro->guide[ comboPos ];
 
-       // Iterate over list of keys currently pressed
-       for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed += 2 )
+// Votes on the given key vs. guide
+inline TriggerMacroVote Macro_evalTriggerMacroVote( TriggerGuide *key, TriggerGuide *guide )
+{
+       // Depending on key type
+       switch ( guide->type )
        {
-               // Compare with keys in combo
-               for ( unsigned int comboKey = 0; comboKey < comboLength; comboKey++ )
+       // Normal State Type
+       case 0x00:
+               // Depending on the state of the buffered key, make voting decision
+               // Incorrect key
+               if ( guide->scanCode != key->scanCode )
                {
-                       // Lookup key in combo
-                       uint8_t guideKey = triggerMacro->guide[ comboPos + comboKey + 2 ]; // TODO Only Press/Hold/Release atm
-
-                       // Sequence Case
-                       if ( comboLength == 1 )
+                       switch ( key->state )
                        {
-                               // 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;
-                               }
-
-                               // If key does not match or more than 1 key pressed, reset the TriggerMacro combo position
-                               triggerMacro->pos = 0;
-                               return;
+                       // Wrong key, pressed, fail
+                       case 0x01:
+                               return TriggerMacroVote_Fail;
+
+                       // Wrong key, held or released, do not pass (no effect)
+                       case 0x02:
+                       case 0x03:
+                               return TriggerMacroVote_DoNothing;
                        }
-                       // Combo Case
-                       else
+               }
+
+               // Correct key
+               else
+               {
+                       switch ( key->state )
                        {
-                               // TODO
+                       // 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;
        }
+
+       // XXX Shouldn't reach here
+       return TriggerMacroVote_Invalid;
 }
 
 
+// Evaluate/Update TriggerMacro
+inline TriggerMacroEval Macro_evalTriggerMacro( unsigned int triggerMacroIndex )
+{
+       // Lookup TriggerMacro
+       TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ];
 
+       // Check if macro has finished and should be incremented sequence elements
+       if ( macro->state == TriggerMacro_Release )
+       {
+               macro->state = TriggerMacro_Waiting;
+               macro->pos = macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize;
+       }
 
-/*
-inline void Macro_bufferAdd( uint8_t byte )
-{
-       // 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 )
+       // Current Macro position
+       unsigned int pos = macro->pos;
+
+       // Length of the combo being processed
+       uint8_t comboLength = macro->guide[ pos ];
+
+       // If no combo items are left, remove the TriggerMacro from the pending list
+       if ( comboLength == 0 )
+       {
+               return TriggerMacroEval_Remove;
+       }
+
+       // Iterate through the key buffer, comparing to each key in the combo
+       // 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 key = 0; key < macroTriggerListBufferSize; key++ )
        {
-               uint8_t key = DefaultMap_Lookup[byte];
-               for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
+               // Lookup key information
+               TriggerGuide *keyInfo = &macroTriggerListBuffer[ key ];
+
+               // Iterate through the items in the combo, voting the on the key state
+               TriggerMacroVote vote = TriggerMacroVote_Invalid;
+               for ( uint8_t comboItem = pos + 1; comboItem < pos + comboLength + 1; comboItem += TriggerGuideSize )
                {
-                       // Key already in the buffer
-                       if ( KeyIndex_Buffer[c] == key )
-                               return;
+                       // Assign TriggerGuide element (key type, state and scancode)
+                       TriggerGuide *guide = (TriggerGuide*)(&macro->guide[ comboItem ]);
+
+                       // 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 |= Macro_evalTriggerMacroVote( keyInfo, guide );
+                       if ( vote >= TriggerMacroVote_Pass )
+                       {
+                               vote &= TriggerMacroVote_Release | TriggerMacroVote_PassRelease | TriggerMacroVote_Pass;
+                               break;
+                       }
+               }
+
+               // After voting, append to overall vote
+               overallVote |= vote;
+       }
+
+       // 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 )
+       {
+               // Just doing nothing :)
+       }
+       // If passing and in Waiting state, set macro state to Press
+       else if ( overallVote & TriggerMacroVote_Pass && macro->state == TriggerMacro_Waiting )
+       {
+               macro->state = TriggerMacro_Press;
+
+               // 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 macro repeat rate
+               if ( macro->guide[ pos + comboLength ] == 0 )
+               {
+                       // Long Macro, only send once (more than 1 sequence item)
+                       // Short Macro (only 1 sequence item)
+                       return Macro_isLongResultMacro( &ResultMacroList[ macro->result ] )
+                               ? TriggerMacroEval_DoResult
+                               : TriggerMacroEval_DoResultAndRemove;
                }
 
-               // Add to the buffer
-               KeyIndex_Buffer[KeyIndex_BufferUsed++] = key;
        }
+       // 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 && macro->state == TriggerMacro_Press )
+       {
+               macro->state = TriggerMacro_Release;
+       }
+
+       return TriggerMacroEval_DoNothing;
 }
 
-inline void Macro_bufferRemove( uint8_t byte )
+
+// Evaluate/Update ResultMacro
+inline ResultMacroEval Macro_evalResultMacro( unsigned int resultMacroIndex )
 {
-       uint8_t key = DefaultMap_Lookup[byte];
+       // Lookup ResultMacro
+       ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
 
-       // Check for the released key, and shift the other keys lower on the buffer
-       for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
+       // Current Macro position
+       unsigned int pos = macro->pos;
+
+       // Length of combo being processed
+       uint8_t comboLength = macro->guide[ pos ];
+
+       // If no combo items are left, remove the ResultMacro from the pending list
+       if ( comboLength == 0 )
        {
-               // Key to release found
-               if ( KeyIndex_Buffer[c] == key )
-               {
-                       // Shift keys from c position
-                       for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
-                               KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
+               return ResultMacroEval_Remove;
+       }
 
-                       // Decrement Buffer
-                       KeyIndex_BufferUsed--;
+       // Function Counter, used to keep track of the combo items processed
+       unsigned int funcCount = 0;
 
-                       return;
-               }
+       // Combo Item Position within the guide
+       unsigned int comboItem = pos + 1;
+
+       // Iterate through the Result Combo
+       while ( funcCount < comboLength )
+       {
+               // Assign TriggerGuide element (key type, state and scancode)
+               ResultGuide *guide = (ResultGuide*)(&macro->guide[ pos ]);
+
+               // 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( macro->state, macro->stateType, &guide->args );
+
+               // Increment counters
+               funcCount++;
+               comboItem += ResultGuideSize( (ResultGuide*)(&macro->guide[ comboItem ]) );
        }
 
-       // Error case (no key to release)
-       erro_msg("Could not find key to release: ");
-       printHex( key );
+       // Move to next item in the sequence
+       macro->pos = comboItem;
+
+       // If the ResultMacro is finished, it will be removed on the next iteration
+       return ResultMacroEval_DoNothing;
 }
-*/
 
-inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
+
+// Update pending trigger list
+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++ )
+       {
+               // Lookup Trigger List
+               unsigned int *triggerList = Macro_layerLookup( macroTriggerListBuffer[ key ].scanCode );
+
+               // Number of Triggers in list
+               unsigned int 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 ( unsigned int macro = 1; macro < triggerListSize + 1; macro++ )
+               {
+                       // Lookup trigger macro index
+                       unsigned int triggerMacroIndex = triggerList[ macro ];
+
+                       // Iterate over macroTriggerMacroPendingList to see if any macro in the scancode's
+                       //  triggerList needs to be added
+                       unsigned int 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;
+                       }
+               }
+       }
 }
 
+
+// Macro Procesing Loop
+// Called once per USB buffer send
 inline void Macro_process()
 {
        // Only do one round of macro processing between Output Module timer sends
@@ -255,52 +520,80 @@ inline void Macro_process()
                return;
 
        // If the pause flag is set, only process if the step counter is non-zero
-       if ( macroPauseMode && macroStepCounter == 0 )
-       {
-               return;
-       }
-       // Proceed, decrementing the step counter
-       else
+       if ( macroPauseMode )
        {
+               if ( macroStepCounter == 0 )
+                       return;
+
+               // Proceed, decrementing the step counter
                macroStepCounter--;
        }
 
-       // Loop through macro trigger buffer
-       for ( uint8_t index = 0; index < macroTriggerListBufferSize; index += 2 )
-       {
-               // Get scanCode, first item of macroTriggerListBuffer pairs
-               uint8_t scanCode = macroTriggerListBuffer[ index ];
+       // Update pending trigger list, before processing TriggerMacros
+       Macro_updateTriggerMacroPendingList();
 
-               // Lookup trigger list for this key
-               unsigned int *triggerList = Macro_layerLookup( scanCode );
+       // Tail pointer for macroTriggerMacroPendingList
+       // Macros must be explicitly re-added
+       unsigned int macroTriggerMacroPendingListTail = 0;
 
-               // The first element is the length of the trigger list
-               unsigned int triggerListSize = triggerList[0];
-
-               // Loop through the trigger list
-               for ( unsigned int trigger = 0; trigger < triggerListSize; trigger++ )
+       // Iterate through the pending TriggerMacros, processing each of them
+       for ( unsigned int macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
+       {
+               switch ( Macro_evalTriggerMacro( macroTriggerMacroPendingList[ macro ] ) )
                {
-                       // Lookup TriggerMacro
-                       TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
+               // Trigger Result Macro (purposely falling through)
+               case TriggerMacroEval_DoResult:
+                       // Append ResultMacro to PendingList
+                       Macro_appendResultMacroToPendingList( TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ].result );
+
+               // Otherwise, just re-add
+               default:
+                       macroTriggerMacroPendingList[ macroTriggerMacroPendingListTail++ ] = macroTriggerMacroPendingList[ macro ];
+                       break;
 
-                       // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
-                       uint8_t state = macroTriggerListBuffer[ index + 1 ];
+               // Trigger Result Macro and Remove (purposely falling through)
+               case TriggerMacroEval_DoResultAndRemove:
+                       // Append ResultMacro to PendingList
+                       Macro_appendResultMacroToPendingList( TriggerMacroList[ macroTriggerMacroPendingList[ macro ] ].result );
 
-                       // Evaluate Macro
-                       Macro_evalTriggerMacro( triggerMacro );
+               // Remove Macro from Pending List, nothing to do, removing by default
+               case TriggerMacroEval_Remove:
+                       break;
                }
        }
 
+       // Update the macroTriggerMacroPendingListSize with the tail pointer
+       macroTriggerMacroPendingListSize = macroTriggerMacroPendingListTail;
+
 
+       // Tail pointer for macroResultMacroPendingList
+       // Macros must be explicitly re-added
+       unsigned int macroResultMacroPendingListTail = 0;
 
+       // Iterate through the pending ResultMacros, processing each of them
+       for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
+       {
+               switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
+               {
+               // 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;
 
        /* TODO
        // Loop through input buffer
        for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
        {
-               //print(" KEYS: ");
-               //printInt8( KeyIndex_BufferUsed );
                // Get the keycode from the buffer
                uint8_t key = KeyIndex_Buffer[index];
 
@@ -321,23 +614,15 @@ inline void Macro_process()
                        break;
                }
 
-               // Allow ignoring keys with 0's
-               if ( key != 0 )
-               {
                        USBKeys_Array[USBKeys_Sent++] = key;
-               }
-               else
-               {
-                       // Key was not mapped
-                       erro_msg( "Key not mapped... - " );
-                       printHex( key );
-                       errorLED( 1 );
-               }
        }
        */
 
        // 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 )
@@ -347,6 +632,7 @@ inline void Macro_process()
        }
 }
 
+
 inline void Macro_setup()
 {
        // Register Macro CLI dictionary
@@ -363,6 +649,22 @@ inline void Macro_setup()
 
        // Make sure macro trigger buffer is empty
        macroTriggerListBufferSize = 0;
+
+       // Initialize TriggerMacro states
+       for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
+       {
+               TriggerMacroList[ macro ].result = 0;
+               TriggerMacroList[ macro ].pos    = 0;
+               TriggerMacroList[ macro ].state  = TriggerMacro_Waiting;
+       }
+
+       // Initialize ResultMacro states
+       for ( unsigned int macro = 0; macro < ResultMacroNum; macro++ )
+       {
+               ResultMacroList[ macro ].pos       = 0;
+               ResultMacroList[ macro ].state     = 0;
+               ResultMacroList[ macro ].stateType = 0;
+       }
 }
 
 
@@ -506,19 +808,80 @@ void cliFunc_keyRelease( char* args )
        }
 }
 
-void cliFunc_layerLatch( char* args )
-{
-       // TODO
-}
-
 void cliFunc_layerList( char* args )
 {
-       // TODO
+       print( NL );
+       info_msg("Layer List");
+
+       // Iterate through all of the layers and display them
+       for ( unsigned int layer = 0; layer < LayerNum; layer++ )
+       {
+               print( NL "\t" );
+               printHex( layer );
+               print(" - ");
+
+               // Display layer name
+               dPrint( LayerIndex[ layer ].name );
+
+               // Default map
+               if ( layer == 0 )
+                       print(" \033[1m(default)\033[0m");
+
+               // Layer State
+               print( NL "\t\t Layer State: " );
+               printHex( LayerIndex[ layer ].state );
+
+               // Max Index
+               print(" Max Index: ");
+               printHex( LayerIndex[ layer ].max );
+       }
 }
 
-void cliFunc_layerLock( char* args )
+void cliFunc_layerState( char* args )
 {
-       // TODO
+       // Parse codes from arguments
+       char* curArgs;
+       char* arg1Ptr;
+       char* arg2Ptr = args;
+
+       uint8_t arg1 = 0;
+       uint8_t arg2 = 0;
+
+       // Process first two args
+       for ( uint8_t c = 0; c < 2; c++ )
+       {
+               curArgs = arg2Ptr;
+               CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
+
+               // Stop processing args if no more are found
+               if ( *arg1Ptr == '\0' )
+                       break;
+
+               switch ( c )
+               {
+               // First argument (e.g. L1)
+               case 0:
+                       if ( arg1Ptr[0] != 'L' )
+                               return;
+
+                       arg1 = (uint8_t)decToInt( &arg1Ptr[1] );
+                       break;
+               // Second argument (e.g. 4)
+               case 1:
+                       arg2 = (uint8_t)decToInt( arg1Ptr );
+
+                       // Display operation (to indicate that it worked)
+                       print( NL );
+                       info_msg("Setting Layer L");
+                       printInt8( arg1 );
+                       print(" to - ");
+                       printHex( arg2 );
+
+                       // Set the layer state
+                       LayerIndex[ arg1 ].state = arg2;
+                       break;
+               }
+       }
 }
 
 void cliFunc_macroDebug( char* args )
@@ -597,7 +960,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("|");