]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Macro/PartialMap/macro.c
Changing decToInt to numToInt (adds support for Hex number interpreter)
[kiibohd-controller.git] / Macro / PartialMap / macro.c
index 5c3798cdd55290666dcfef47db0500daf3987819..e6c82ecae2a9257af9f56d9c8b03f88f560be7c2 100644 (file)
@@ -24,7 +24,6 @@
 #include <led.h>
 #include <print.h>
 #include <scan_loop.h>
-#include <output_com.h>
 
 // Keymaps
 #include "usb_hid.h"
@@ -40,6 +39,7 @@
 
 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_layerList ( char* args );
@@ -81,12 +81,13 @@ typedef enum ResultMacroEval {
 // ----- Variables -----
 
 // Macro Module command dictionary
-char*       macroCLIDictName = "Macro Module Commands";
-CLIDictItem macroCLIDict[] = {
+const char macroCLIDictName[] = "Macro Module Commands";
+const 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 },
+       { "keyHold",     "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyHold },
+       { "keyPress",    "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
+       { "keyRelease",  "Send key-release event to 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 },
@@ -132,6 +133,79 @@ unsigned int macroResultMacroPendingListSize = 0;
 
 
 
+// ----- Capabilities -----
+
+// Modifies the specified Layer control byte
+// Argument #1: Layer Index -> unsigned int
+// Argument #2: Toggle byte -> uint8_t
+void Macro_layerStateToggle_capability( uint8_t state, uint8_t stateType, uint8_t *args )
+{
+       // Display capability name
+       if ( stateType == 0xFF && state == 0xFF )
+       {
+               print("Macro_layerState(layerIndex,toggleByte)");
+               return;
+       }
+
+       // Get layer index from arguments
+       // Cast pointer to uint8_t to unsigned int then access that memory location
+       unsigned int layer = *(unsigned int*)(&args[0]);
+
+       // Get layer toggle byte
+       uint8_t toggleByte = args[ sizeof(unsigned int) ];
+
+       // Is layer in the LayerIndexStack?
+       uint8_t inLayerIndexStack = 0;
+       unsigned int 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 ( LayerIndex[ layer ].state & toggleByte )
+       {
+               // Unset
+               LayerIndex[ layer ].state &= ~toggleByte;
+       }
+       else
+       {
+               // Set
+               LayerIndex[ layer ].state |= toggleByte;
+       }
+
+       // 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 ( LayerIndex[ layer ].state == 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--;
+       }
+}
+
+
+
 // ----- Functions -----
 
 // Looks up the trigger list for the given scan code (from the active layer)
@@ -139,18 +213,35 @@ unsigned int macroResultMacroPendingListSize = 0;
 unsigned int *Macro_layerLookup( uint8_t scanCode )
 {
        // If no trigger macro is defined at the given layer, fallthrough to the next layer
-       for ( unsigned int layer = 0; layer < macroLayerIndexStackSize; layer++ )
+       for ( unsigned int layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ )
        {
-               // Lookup layer
-               unsigned int **map = LayerIndex[ macroLayerIndexStack[ layer ] ].triggerMap;
+               // Lookup Layer
+               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;
+               if ( latch )
+               {
+                       layer->state &= ~0x02;
+               }
 
-               // Determine if layer has key defined
-               if ( map != 0 && *map[ scanCode ] != 0 )
-                       return map[ scanCode ];
+               // 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) )
+               {
+                       // Lookup layer
+                       unsigned int **map = (unsigned int**)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;
+       unsigned int **map = (unsigned int**)LayerIndex[0].triggerMap;
 
        // Determine if layer has key defined
        if ( map == 0 && *map[ scanCode ] == 0 )
@@ -527,6 +618,7 @@ inline void Macro_process()
 
                // Proceed, decrementing the step counter
                macroStepCounter--;
+               dbug_print("Macro Step");
        }
 
        // Update pending trigger list, before processing TriggerMacros
@@ -590,34 +682,6 @@ inline void Macro_process()
        // Update the macroResultMacroPendingListSize with the tail pointer
        macroResultMacroPendingListSize = macroResultMacroPendingListTail;
 
-       /* TODO
-       // Loop through input buffer
-       for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
-       {
-               // Get the keycode from the buffer
-               uint8_t key = KeyIndex_Buffer[index];
-
-               // 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
-
-                       // Modifier processed, move on to the next key
-                       continue;
-               }
-
-               // Too many keys
-               if ( USBKeys_Sent >= USBKeys_MaxSize )
-               {
-                       warn_msg("USB Key limit reached");
-                       errorLED( 1 );
-                       break;
-               }
-
-                       USBKeys_Array[USBKeys_Sent++] = key;
-       }
-       */
-
        // Signal buffer that we've used it
        Scan_finishedWithMacro( macroTriggerListBufferSize );
 
@@ -653,7 +717,6 @@ inline void Macro_setup()
        // Initialize TriggerMacro states
        for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
        {
-               TriggerMacroList[ macro ].result = 0;
                TriggerMacroList[ macro ].pos    = 0;
                TriggerMacroList[ macro ].state  = TriggerMacro_Waiting;
        }
@@ -674,6 +737,7 @@ void cliFunc_capList( char* args )
 {
        print( NL );
        info_msg("Capabilities List");
+       printHex( CapabilitiesNum );
 
        // Iterate through all of the capabilities and display them
        for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
@@ -720,7 +784,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;
@@ -729,7 +793,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 )
@@ -752,6 +816,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
@@ -774,7 +866,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;
                }
        }
@@ -802,7 +894,7 @@ 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;
                }
        }
@@ -821,7 +913,7 @@ void cliFunc_layerList( char* args )
                print(" - ");
 
                // Display layer name
-               dPrint( LayerIndex[ layer ].name );
+               dPrint( (char*)LayerIndex[ layer ].name );
 
                // Default map
                if ( layer == 0 )
@@ -864,11 +956,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 );
@@ -896,6 +988,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 ( unsigned int 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 ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
+       {
+               printHex( macroResultMacroPendingList[ macro ] );
+               print(" ");
+       }
+
        // Show available trigger macro indices
        print( NL );
        info_msg("Trigger Macros Range: T0 -> T");
@@ -1098,11 +1223,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;
                }
        }
@@ -1116,7 +1241,13 @@ void cliFunc_macroStep( char* args )
        char* arg2Ptr;
        CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
 
+       // Default to 1, if no argument given
+       unsigned int count = (unsigned int)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;
 }