Archived
1
0

Adding support for layer packing

- Only keep state for the scan code that are available for the layer
- Also properly implemented scan code range checking (was not done before, only worked because the KLL compiler was well behaved)
This commit is contained in:
Jacob Alexander 2014-09-16 17:04:59 -07:00
parent 12295e8362
commit df82ffeff7
2 changed files with 50 additions and 33 deletions

View File

@ -118,7 +118,7 @@ typedef enum TriggerMacroState {
// TriggerMacro struct, one is created per TriggerMacro, no duplicates // TriggerMacro struct, one is created per TriggerMacro, no duplicates
typedef struct TriggerMacro { typedef struct TriggerMacro {
const uint8_t *guide; const uint8_t *guide;
var_uint_t result; const var_uint_t result;
var_uint_t pos; var_uint_t pos;
TriggerMacroState state; TriggerMacroState state;
} TriggerMacro; } TriggerMacro;
@ -137,8 +137,8 @@ typedef struct TriggerGuide {
// Capability // Capability
typedef struct Capability { typedef struct Capability {
void *func; const void *func;
uint8_t argCount; const uint8_t argCount;
} Capability; } Capability;
// Total Number of Capabilities // Total Number of Capabilities
@ -205,28 +205,31 @@ typedef struct Capability {
// * Shift - 0x01 // * Shift - 0x01
// * Latch - 0x02 // * Latch - 0x02
// * Lock - 0x04 // * Lock - 0x04
// Layer states are stored in the LayerState array
// //
// Except for Off, all states an exist simultaneously for each layer // Except for Off, all states an exist simultaneously for each layer
// For example: // For example:
// state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00) // state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00)
// //
// Max defines the maximum number of keys in the map, maximum of 0xFF // First defines the first used scan code (most keyboards start at 0, some start higher e.g. 0x40)
// - Compiler calculates this // - Compiler calculates this
// //
// Last defines the last scan code used (helps reduce RAM usage)
//
// The name is defined for cli debugging purposes (Null terminated string) // The name is defined for cli debugging purposes (Null terminated string)
typedef struct Layer { typedef struct Layer {
const nat_ptr_t **triggerMap; const nat_ptr_t **triggerMap;
const char *name; const char *name;
const uint8_t max; const uint8_t first;
uint8_t state; const uint8_t last;
} Layer; } Layer;
// Layer_IN( map, name, first );
// Layer_IN( map, name ); // * map - Trigger map
// * map - Trigger map // * name - Name of the trigger map
// * name - Name of the trigger map // * first - First scan code used (most keyboards start at 0, some start higher e.g. 0x40)
#define Layer_IN( map, name ) { map, name, sizeof( map ) / sizeof( nat_ptr_t ) - 1, 0 } #define Layer_IN( map, name, first ) { map, name, first, sizeof( map ) / sizeof( nat_ptr_t ) - 1 + first }
// Total number of layers // Total number of layers
#define LayerNum sizeof( LayerIndex ) / sizeof( Layer ) #define LayerNum sizeof( LayerIndex ) / sizeof( Layer )

View File

@ -155,15 +155,15 @@ void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t
} }
// Toggle Layer State Byte // Toggle Layer State Byte
if ( LayerIndex[ layer ].state & layerState ) if ( LayerState[ layer ] & layerState )
{ {
// Unset // Unset
LayerIndex[ layer ].state &= ~layerState; LayerState[ layer ] &= ~layerState;
} }
else else
{ {
// Set // Set
LayerIndex[ layer ].state |= layerState; LayerState[ layer ] |= layerState;
} }
// If the layer was not in the LayerIndexStack add it // If the layer was not in the LayerIndexStack add it
@ -173,7 +173,7 @@ void Macro_layerState( uint8_t state, uint8_t stateType, uint16_t layer, uint8_t
} }
// If the layer is in the LayerIndexStack and the state is 0x00, remove // If the layer is in the LayerIndexStack and the state is 0x00, remove
if ( LayerIndex[ layer ].state == 0x00 && inLayerIndexStack ) if ( LayerState[ layer ] == 0x00 && inLayerIndexStack )
{ {
// Remove the layer from the LayerIndexStack // Remove the layer from the LayerIndexStack
// Using the already positioned stackItem variable from the loop above // Using the already positioned stackItem variable from the loop above
@ -302,43 +302,55 @@ nat_ptr_t *Macro_layerLookup( uint8_t scanCode )
for ( uint16_t layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ ) for ( uint16_t layerIndex = 0; layerIndex < macroLayerIndexStackSize; layerIndex++ )
{ {
// Lookup Layer // Lookup Layer
Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ]; const Layer *layer = &LayerIndex[ macroLayerIndexStack[ layerIndex ] ];
// Check if latch has been pressed for this layer // Check if latch has been pressed for this layer
// XXX Regardless of whether a key is found, the latch is removed on first lookup // XXX Regardless of whether a key is found, the latch is removed on first lookup
uint8_t latch = layer->state & 0x02; uint8_t latch = LayerState[ layerIndex ] & 0x02;
if ( latch ) if ( latch )
{ {
layer->state &= ~0x02; LayerState[ layerIndex ] &= ~0x02;
} }
// Only use layer, if state is valid // Only use layer, if state is valid
// XOR each of the state bits // XOR each of the state bits
// If only two are enabled, do not use this state // If only two are enabled, do not use this state
if ( (layer->state & 0x01) ^ (latch>>1) ^ ((layer->state & 0x04)>>2) ) if ( (LayerState[ layerIndex ] & 0x01) ^ (latch>>1) ^ ((LayerState[ layerIndex ] & 0x04)>>2) )
{ {
// Lookup layer // Lookup layer
nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap; nat_ptr_t **map = (nat_ptr_t**)layer->triggerMap;
// Determine if layer has key defined // Determine if layer has key defined
if ( map != 0 && *map[ scanCode ] != 0 ) // Make sure scanCode is between layer first and last scancodes
return map[ scanCode ]; if ( map != 0
&& scanCode < layer->last
&& scanCode > layer->first
&& *map[ scanCode - layer->first ] != 0 )
{
return map[ scanCode - layer->first ];
}
} }
} }
// Do lookup on default layer // Do lookup on default layer
nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap; nat_ptr_t **map = (nat_ptr_t**)LayerIndex[0].triggerMap;
// Determine if layer has key defined // Lookup default layer
if ( map == 0 && *map[ scanCode ] == 0 ) const Layer *layer = &LayerIndex[ macroLayerIndexStack[ 0 ] ];
// Make sure scanCode is between layer first and last scancodes
if ( map != 0
&& scanCode < layer->last
&& scanCode > layer->first
&& *map[ scanCode - layer->first ] != 0 )
{ {
erro_msg("Scan Code has no defined Trigger Macro: "); return map[ scanCode - layer->first ];
printHex( scanCode );
return 0;
} }
// Return lookup result // Otherwise no defined Trigger Macro
return map[ scanCode ]; erro_msg("Scan Code has no defined Trigger Macro: ");
printHex( scanCode );
return 0;
} }
@ -1157,11 +1169,13 @@ void cliFunc_layerList( char* args )
// Layer State // Layer State
print( NL "\t\t Layer State: " ); print( NL "\t\t Layer State: " );
printHex( LayerIndex[ layer ].state ); printHex( LayerState[ layer ] );
// Max Index // First -> Last Indices
print(" Max Index: "); print(" First -> Last Indices: ");
printHex( LayerIndex[ layer ].max ); printHex( LayerIndex[ layer ].first );
print(" -> ");
printHex( LayerIndex[ layer ].last );
} }
} }
@ -1206,7 +1220,7 @@ void cliFunc_layerState( char* args )
printHex( arg2 ); printHex( arg2 );
// Set the layer state // Set the layer state
LayerIndex[ arg1 ].state = arg2; LayerState[ arg1 ] = arg2;
break; break;
} }
} }