+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;
+
+
+
+// ----- 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--;
+ }
+}