]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Macro/PartialMap/macro.c
Adding layer rotation (next/prev) capability
[kiibohd-controller.git] / Macro / PartialMap / macro.c
index 89444dcfc1e390f0f3ec0753853ba7658cf0a545..690231cc889628b158ca6dac33ab86be0509318b 100644 (file)
@@ -170,8 +170,8 @@ uint8_t macroInterconnectCacheSize = 0;
 // Sets the given layer with the specified layerState
 void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t layerState )
 {
-       // Ignore if layer does not exist
-       if ( layer >= LayerNum )
+       // Ignore if layer does not exist or trying to manipulate layer 0/Default layer
+       if ( layer >= LayerNum || layer == 0 )
                return;
 
        // Is layer in the LayerIndexStack?
@@ -350,6 +350,58 @@ void Macro_layerShift_capability( uint8_t state, uint8_t stateType, uint8_t *arg
 }
 
 
+// Rotate layer to next/previous
+// Uses state variable to keep track of the current layer position
+// Layers are still evaluated using the layer stack
+uint16_t Macro_rotationLayer;
+void Macro_layerRotate_capability( uint8_t state, uint8_t stateType, uint8_t *args )
+{
+       // Display capability name
+       if ( stateType == 0xFF && state == 0xFF )
+       {
+               print("Macro_layerRotate(previous)");
+               return;
+       }
+
+       // Only use capability on press
+       // TODO Analog
+       // XXX Could also be on release, but that's sorta dumb -HaaTa
+       if ( stateType == 0x00 && state != 0x01 ) // All normal key conditions except press
+               return;
+
+       // Unset previous rotation layer if not 0
+       if ( Macro_rotationLayer != 0 )
+       {
+               Macro_layerState( state, stateType, Macro_rotationLayer, 0x04 );
+       }
+
+       // Get direction of rotation, 0, next, non-zero previous
+       uint8_t direction = *args;
+
+       // Next
+       if ( !direction )
+       {
+               Macro_rotationLayer++;
+
+               // Invalid layer
+               if ( Macro_rotationLayer >= LayerNum )
+                       Macro_rotationLayer = 0;
+       }
+       // Previous
+       else
+       {
+               Macro_rotationLayer--;
+
+               // Layer wrap
+               if ( Macro_rotationLayer >= LayerNum )
+                       Macro_rotationLayer = LayerNum - 1;
+       }
+
+       // Toggle the computed layer rotation
+       Macro_layerState( state, stateType, Macro_rotationLayer, 0x04 );
+}
+
+
 
 // ----- Functions -----
 
@@ -374,7 +426,7 @@ nat_ptr_t *Macro_layerLookup( TriggerGuide *guide, uint8_t latch_expire )
        }
 
        // If no trigger macro is defined at the given layer, fallthrough to the next layer
-       for ( uint16_t layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ )
+       for ( uint16_t layerIndex = macroLayerIndexStackSize; layerIndex != 0xFFFF; layerIndex-- )
        {
                // Lookup Layer
                const Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
@@ -398,9 +450,9 @@ nat_ptr_t *Macro_layerLookup( TriggerGuide *guide, uint8_t latch_expire )
                        // Determine if layer has key defined
                        // Make sure scanCode is between layer first and last scancodes
                        if ( map != 0
-                         && scanCode <= layer->last
-                         && scanCode >= layer->first
-                         && *map[ scanCode - layer->first ] != 0 )
+                               && scanCode <= layer->last
+                               && scanCode >= layer->first
+                               && *map[ scanCode - layer->first ] != 0 )
                        {
                                // Set the layer cache
                                macroTriggerListLayerCache[ scanCode ] = macroLayerIndexStack[ layerIndex ];
@@ -418,9 +470,9 @@ nat_ptr_t *Macro_layerLookup( TriggerGuide *guide, uint8_t latch_expire )
 
        // Make sure scanCode is between layer first and last scancodes
        if ( map != 0
-         && scanCode <= layer->last
-         && scanCode >= layer->first
-         && *map[ scanCode - layer->first ] != 0 )
+               && scanCode <= layer->last
+               && scanCode >= layer->first
+               && *map[ scanCode - layer->first ] != 0 )
        {
                // Set the layer cache to default map
                macroTriggerListLayerCache[ scanCode ] = 0;
@@ -456,7 +508,7 @@ inline void Macro_interconnectAdd( void *trigger_ptr )
                case 0x03:
                        break;
                default:
-                       erro_print("Invalid key state");
+                       erro_msg("Invalid key state - ");
                        error = 1;
                        break;
                }
@@ -464,11 +516,18 @@ inline void Macro_interconnectAdd( void *trigger_ptr )
 
        // Invalid TriggerGuide type
        default:
-               erro_print("Invalid type");
+               erro_msg("Invalid type - ");
                error = 1;
                break;
        }
 
+       // Check if ScanCode is out of range
+       if ( trigger->scanCode > MaxScanCode )
+       {
+               warn_msg("ScanCode is out of range/not defined - ");
+               error = 1;
+       }
+
        // Display TriggerGuide
        if ( error )
        {
@@ -529,6 +588,15 @@ inline void Macro_keyState( uint8_t scanCode, uint8_t state )
        case 0x01: // Pressed
        case 0x02: // Held
        case 0x03: // Released
+               // Check if ScanCode is out of range
+               if ( scanCode > MaxScanCode )
+               {
+                       warn_msg("ScanCode is out of range/not defined: ");
+                       printHex( scanCode );
+                       print( NL );
+                       return;
+               }
+
                macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
                macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
                macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x00; // Normal key
@@ -549,6 +617,15 @@ inline void Macro_analogState( uint8_t scanCode, uint8_t state )
        // TODO Handle change for interconnect
        if ( state != 0x00 )
        {
+               // Check if ScanCode is out of range
+               if ( scanCode > MaxScanCode )
+               {
+                       warn_msg("ScanCode is out of range/not defined: ");
+                       printHex( scanCode );
+                       print( NL );
+                       return;
+               }
+
                macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
                macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
                macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x02; // Analog key
@@ -567,6 +644,9 @@ inline void Macro_ledState( uint8_t ledCode, uint8_t state )
        // TODO Handle change for interconnect
        if ( state != 0x00 )
        {
+               // Check if LedCode is out of range
+               // TODO
+
                macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
                macroTriggerListBuffer[ macroTriggerListBufferSize ].state    = state;
                macroTriggerListBuffer[ macroTriggerListBufferSize ].type     = 0x01; // LED key
@@ -864,7 +944,7 @@ TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex )
        }
        // If passing and in Waiting state, set macro state to Press
        else if ( overallVote & TriggerMacroVote_Pass
-            && ( record->state == TriggerMacro_Waiting || record->state == TriggerMacro_Press ) )
+               && ( record->state == TriggerMacro_Waiting || record->state == TriggerMacro_Press ) )
        {
                record->state = TriggerMacro_Press;
 
@@ -1184,6 +1264,9 @@ inline void Macro_setup()
        // Make sure macro trigger buffer is empty
        macroTriggerListBufferSize = 0;
 
+       // Set the current rotated layer to 0
+       Macro_rotationLayer = 0;
+
        // Initialize TriggerMacro states
        for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ )
        {