]> 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 a1e6a34dd21e84130609d6e129db26a27f367a90..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 );
@@ -71,17 +71,23 @@ typedef enum TriggerMacroEval {
        TriggerMacroEval_Remove,
 } TriggerMacroEval;
 
+typedef enum ResultMacroEval {
+       ResultMacroEval_DoNothing,
+       ResultMacroEval_Remove,
+} 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 },
@@ -127,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)
@@ -134,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;
+               }
+
+               // 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 ];
+                       // 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 )
@@ -242,7 +338,7 @@ 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;
+       return macro->guide[ macro->guide[0] * ResultGuideSize( (ResultGuide*)macro->guide ) ] > 0 ? 1 : 0;
 }
 
 
@@ -418,9 +514,51 @@ inline TriggerMacroEval Macro_evalTriggerMacro( unsigned int triggerMacroIndex )
 
 
 // Evaluate/Update ResultMacro
-void Macro_evalResultMacro( unsigned int resultMacroIndex )
+inline ResultMacroEval Macro_evalResultMacro( unsigned int resultMacroIndex )
 {
-       // TODO
+       // Lookup ResultMacro
+       ResultMacro *macro = &ResultMacroList[ resultMacroIndex ];
+
+       // 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 )
+       {
+               return ResultMacroEval_Remove;
+       }
+
+       // Function Counter, used to keep track of the combo items processed
+       unsigned int funcCount = 0;
+
+       // 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 ]) );
+       }
+
+       // 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;
 }
 
 
@@ -480,6 +618,7 @@ inline void Macro_process()
 
                // Proceed, decrementing the step counter
                macroStepCounter--;
+               dbug_print("Macro Step");
        }
 
        // Update pending trigger list, before processing TriggerMacros
@@ -515,46 +654,39 @@ inline void Macro_process()
                }
        }
 
-       // Update the macroResultMacroPendingListSize with the tail pointer
+       // 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++ )
        {
-               Macro_evalResultMacro( macroResultMacroPendingList[ macro ] );
-       }
-
-       /* 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
+               switch ( Macro_evalResultMacro( macroResultMacroPendingList[ macro ] ) )
                {
-                       USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
-
-                       // Modifier processed, move on to the next key
-                       continue;
-               }
+               // Re-add macros to pending list
+               case ResultMacroEval_DoNothing:
+               default:
+                       macroResultMacroPendingList[ macroResultMacroPendingListTail++ ] = macroResultMacroPendingList[ macro ];
+                       break;
 
-               // Too many keys
-               if ( USBKeys_Sent >= USBKeys_MaxSize )
-               {
-                       warn_msg("USB Key limit reached");
-                       errorLED( 1 );
+               // Remove Macro from Pending List, nothing to do, removing by default
+               case ResultMacroEval_Remove:
                        break;
                }
-
-                       USBKeys_Array[USBKeys_Sent++] = key;
        }
-       */
 
-       // Signal buffer that we've used it TODO
-       Scan_finishedWithMacro( 0 );
-       //Scan_finishedWithBuffer( KeyIndex_BufferUsed );
+       // Update the macroResultMacroPendingListSize with the tail pointer
+       macroResultMacroPendingListSize = macroResultMacroPendingListTail;
+
+       // Signal buffer that we've used it
+       Scan_finishedWithMacro( macroTriggerListBufferSize );
+
+       // Reset TriggerList buffer
+       macroTriggerListBufferSize = 0;
 
        // If Macro debug mode is set, clear the USB Buffer
        if ( macroDebugMode )
@@ -585,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;
        }
@@ -606,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++ )
@@ -652,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;
@@ -661,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 )
@@ -684,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
@@ -706,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;
                }
        }
@@ -734,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;
                }
        }
@@ -753,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 )
@@ -796,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 );
@@ -828,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");
@@ -1030,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;
                }
        }
@@ -1048,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;
 }