]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Macro/PartialMap/macro.c
Macro cleanup.
[kiibohd-controller.git] / Macro / PartialMap / macro.c
index a587b107abfdb12c52ee178a4a51ea2659acf049..02caa9796a9dff93b5235d5511ec89b6812dff58 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 );
@@ -62,9 +61,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 +83,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 +154,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,27 +165,40 @@ 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
+// Evaluate/Update TriggerMacro
 void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
 {
        // Which combo in the sequence is being evaluated
@@ -162,7 +208,7 @@ void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
        uint8_t comboLength = triggerMacro->guide[ comboPos ];
 
        // Iterate over list of keys currently pressed
-       for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed += 2 )
+       for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed++ )
        {
                // Compare with keys in combo
                for ( unsigned int comboKey = 0; comboKey < comboLength; comboKey++ )
@@ -174,7 +220,7 @@ void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
                        if ( comboLength == 1 )
                        {
                                // If key matches and only 1 key pressed, increment the TriggerMacro combo position
-                               if ( guideKey == macroTriggerListBuffer[ keyPressed ] && macroTriggerListBufferSize == 1 )
+                               if ( guideKey == macroTriggerListBuffer[ keyPressed ].scanCode && macroTriggerListBufferSize == 1 )
                                {
                                        triggerMacro->pos += comboLength * 2 + 1;
                                        // TODO check if TriggerMacro is finished, register ResultMacro
@@ -195,59 +241,22 @@ void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
 }
 
 
-
-
-/*
-inline void Macro_bufferAdd( uint8_t byte )
+// Evaluate/Update ResultMacro
+void Macro_evalResultMacro( ResultMacro *resultMacro )
 {
-       // 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 )
-       {
-               uint8_t key = DefaultMap_Lookup[byte];
-               for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
-               {
-                       // Key already in the buffer
-                       if ( KeyIndex_Buffer[c] == key )
-                               return;
-               }
-
-               // Add to the buffer
-               KeyIndex_Buffer[KeyIndex_BufferUsed++] = key;
-       }
+       // TODO
 }
 
-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++ )
-       {
-               // 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];
-
-                       // Decrement Buffer
-                       KeyIndex_BufferUsed--;
-
-                       return;
-               }
-       }
-
-       // Error case (no key to release)
-       erro_msg("Could not find key to release: ");
-       printHex( key );
-}
-*/
 
+// Called immediately after USB has finished sending a buffer
 inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
 {
+       // XXX Currently not used to trigger anything (with this particular Macro module)
 }
 
+
+// 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
@@ -266,14 +275,18 @@ inline void Macro_process()
        }
 
        // Loop through macro trigger buffer
-       for ( uint8_t index = 0; index < macroTriggerListBufferSize; index += 2 )
+       for ( uint8_t index = 0; index < macroTriggerListBufferSize; index++ )
        {
                // Get scanCode, first item of macroTriggerListBuffer pairs
-               uint8_t scanCode = macroTriggerListBuffer[ index ];
+               uint8_t scanCode = macroTriggerListBuffer[ index ].scanCode;
 
                // Lookup trigger list for this key
                unsigned int *triggerList = Macro_layerLookup( scanCode );
 
+               // Skip, if no trigger list
+               if ( triggerList == 0 )
+                       continue;
+
                // The first element is the length of the trigger list
                unsigned int triggerListSize = triggerList[0];
 
@@ -284,7 +297,7 @@ inline void Macro_process()
                        TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
 
                        // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
-                       uint8_t state = macroTriggerListBuffer[ index + 1 ];
+                       uint8_t state = macroTriggerListBuffer[ index ].state;
 
                        // Evaluate Macro
                        Macro_evalTriggerMacro( triggerMacro );
@@ -347,6 +360,7 @@ inline void Macro_process()
        }
 }
 
+
 inline void Macro_setup()
 {
        // Register Macro CLI dictionary
@@ -506,19 +520,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 +672,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("|");