]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Macro/PartialMap/macro.c
DPH controller now working with the kishsaver and macros
[kiibohd-controller.git] / Macro / PartialMap / macro.c
index 9d107da35d30981581585a8c6072e31808b815bb..a86efb09e42b916069f8b6b2ca522f4cd2a4596e 100644 (file)
@@ -155,15 +155,15 @@ void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t
        }
 
        // Toggle Layer State Byte
-       if ( LayerIndex[ layer ].state & layerState )
+       if ( LayerState[ layer ] & layerState )
        {
                // Unset
-               LayerIndex[ layer ].state &= ~layerState;
+               LayerState[ layer ] &= ~layerState;
        }
        else
        {
                // Set
-               LayerIndex[ layer ].state |= layerState;
+               LayerState[ layer ] |= layerState;
        }
 
        // If the layer was not in the LayerIndexStack add it
@@ -173,7 +173,7 @@ void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t
        }
 
        // If the layer is in the LayerIndexStack and the state is 0x00, remove
-       if ( LayerIndex[ layer ].state == 0x00 && inLayerIndexStack )
+       if ( LayerState[ layer ] == 0x00 && inLayerIndexStack )
        {
                // Remove the layer from the LayerIndexStack
                // Using the already positioned stackItem variable from the loop above
@@ -302,43 +302,55 @@ nat_ptr_t *Macro_layerLookup( uint8_t scanCode )
        for ( uint16_t layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ )
        {
                // Lookup Layer
-               Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
+               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 = layer->state & 0x02;
+               uint8_t latch = LayerState[ layerIndex ] & 0x02;
                if ( latch )
                {
-                       layer->state &= ~0x02;
+                       LayerState[ 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 ( (layer->state & 0x01) ^ (latch>>1) ^ ((layer->state & 0x04)>>2) )
+               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
-                       if ( map != 0 && *map[ scanCode ] != 0 )
-                               return map[ scanCode ];
+                       // Make sure scanCode is between layer first and last scancodes
+                       if ( map != 0
+                         && scanCode <= layer->last
+                         && scanCode >= layer->first
+                         && *map[ scanCode - layer->first ] != 0 )
+                       {
+                               return map[ scanCode - layer->first ];
+                       }
                }
        }
 
        // Do lookup on default layer
        nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
 
-       // Determine if layer has key defined
-       if ( map == 0 && *map[ scanCode ] == 0 )
+       // 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 )
        {
-               erro_msg("Scan Code has no defined Trigger Macro: ");
-               printHex( scanCode );
-               return 0;
+               return map[ scanCode - layer->first ];
        }
 
-       // Return lookup result
-       return map[ scanCode ];
+       // Otherwise no defined Trigger Macro
+       erro_msg("Scan Code has no defined Trigger Macro: ");
+       printHex( scanCode );
+       return 0;
 }
 
 
@@ -677,6 +689,16 @@ inline TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex )
        {
                // 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 && macro->state == TriggerMacro_Press )
+       {
+               macro->state = TriggerMacro_Release;
+
+               // If this is the last combo in the sequence, remove from the pending list
+               if ( macro->guide[ macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1 ] == 0 )
+                       return TriggerMacroEval_DoResultAndRemove;
+       }
        // If passing and in Waiting state, set macro state to Press
        else if ( overallVote & TriggerMacroVote_Pass
             && ( macro->state == TriggerMacro_Waiting || macro->state == TriggerMacro_Press ) )
@@ -714,16 +736,6 @@ inline TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex )
                        }
                }
        }
-       // 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;
-
-               // If this is the last combo in the sequence, remove from the pending list
-               if ( macro->guide[ macro->pos + macro->guide[ macro->pos ] * TriggerGuideSize + 1 ] == 0 )
-                       return TriggerMacroEval_Remove;
-       }
        // 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 )
@@ -781,6 +793,7 @@ inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex )
        // If the ResultMacro is finished, remove
        if ( macro->guide[ comboItem ] == 0 )
        {
+               macro->pos = 0;
                return ResultMacroEval_Remove;
        }
 
@@ -1157,11 +1170,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 );
        }
 }
 
@@ -1206,7 +1221,7 @@ void cliFunc_layerState( char* args )
                        printHex( arg2 );
 
                        // Set the layer state
-                       LayerIndex[ arg1 ].state = arg2;
+                       LayerState[ arg1 ] = arg2;
                        break;
                }
        }