diff --git a/Lib/CMake/kll.cmake b/Lib/CMake/kll.cmake index 5d007d2..a81c024 100644 --- a/Lib/CMake/kll.cmake +++ b/Lib/CMake/kll.cmake @@ -64,7 +64,7 @@ endif () #| Configure DefaultMap if specified if ( NOT "${DefaultMap}" STREQUAL "" ) - set ( DefaultMap_Args -d ) + set ( DefaultMap_Args --default ) string ( REPLACE " " ";" MAP_LIST ${DefaultMap} ) # Change spaces to semicolons foreach ( MAP ${MAP_LIST} ) @@ -83,7 +83,7 @@ endif () if ( NOT "${PartialMaps}" STREQUAL "" ) # For each partial layer foreach ( MAP ${PartialMaps} ) - set ( PartialMap_Args ${PartialMap_Args} -p ) + set ( PartialMap_Args ${PartialMap_Args} --partial ) # Combine each layer string ( REPLACE " " ";" MAP_LIST ${MAP} ) # Change spaces to semicolons diff --git a/Macro/PartialMap/kll.h b/Macro/Common/kll.h similarity index 77% rename from Macro/PartialMap/kll.h rename to Macro/Common/kll.h index 492e1ba..d6dfd32 100644 --- a/Macro/PartialMap/kll.h +++ b/Macro/Common/kll.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 by Jacob Alexander +/* Copyright (C) 2014-2015 by Jacob Alexander * * This file is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -64,6 +64,15 @@ typedef uint16_t nat_ptr_t; // ----- Structs ----- +// -- Macro Type +// Defines what type of Macro the struct defines +// Always the first field +typedef enum MacroType { + MacroType_Normal, // Sequence of Combos + MacroType_Simple, // Single combination (no state required) +} MacroType; + + // -- Result Macro // Defines the sequence of combinations to as the Result of Trigger Macro // For RAM optimization reasons, ResultMacro has been split into ResultMacro and ResultMacroRecord structures @@ -72,23 +81,28 @@ typedef uint16_t nat_ptr_t; // Default Args (always sent): key state/analog of last key // Combo Length of 0 signifies end of sequence // -// ResultMacro.guide -> [|||||...||...|0] +// ResultMacro.type -> +// ResultMacro.guide -> [|||||...||...|0] +// ResultMacro.record -> Pointer to ResultMacroRecord if MacroType_Normal // // ResultMacroRecord.pos -> // ResultMacroRecord.state -> // ResultMacroRecord.stateType -> -// ResultMacro struct, one is created per ResultMacro, no duplicates -typedef struct ResultMacro { - const uint8_t *guide; -} ResultMacro; - +// ResultMacroRecord struct, one is created per ResultMacro, no duplicates, if a MacroType_Normal typedef struct ResultMacroRecord { var_uint_t pos; - uint8_t state; - uint8_t stateType; + uint8_t state; + uint8_t stateType; } ResultMacroRecord; +// ResultMacro struct, one is created per ResultMacro, no duplicates +typedef const struct ResultMacro { + const MacroType type; + const uint8_t *guide; + ResultMacroRecord *record; +} ResultMacro; + // Guide, key element #define ResultGuideSize( guidePtr ) sizeof( ResultGuide ) - 1 + CapabilitiesList[ (guidePtr)->index ].argCount typedef struct ResultGuide { @@ -117,8 +131,10 @@ typedef struct ResultGuide { // // Combo Length of 0 signifies end of sequence // +// TriggerMacro.type -> // TriggerMacro.guide -> [|||...|||...|0] // TriggerMacro.result -> +// TriggerMacro.record -> Pointer to TriggrMacroRecord if MacroType_Normal // // TriggerMacroRecord.pos -> // TriggerMacroRecord.state -> @@ -130,17 +146,20 @@ typedef enum TriggerMacroState { TriggerMacro_Waiting, // Awaiting user input } TriggerMacroState; -// TriggerMacro struct, one is created per TriggerMacro, no duplicates -typedef struct TriggerMacro { - const uint8_t *guide; - const var_uint_t result; -} TriggerMacro; - +// TriggerMacroRecord struct, one is created per TriggerMacro, no duplicates, if a MacroType_Normal typedef struct TriggerMacroRecord { - var_uint_t pos; + var_uint_t pos; TriggerMacroState state; } TriggerMacroRecord; +// TriggerMacro struct, one is created per TriggerMacro, no duplicates +typedef const struct TriggerMacro { + const MacroType type; + const uint8_t *guide; + const var_uint_t result; + TriggerMacroRecord *record; +} TriggerMacro; + // Guide, key element #define TriggerGuideSize sizeof( TriggerGuide ) typedef struct TriggerGuide { @@ -155,8 +174,8 @@ typedef struct TriggerGuide { // Capability typedef struct Capability { - const void *func; - const uint8_t argCount; + const void *func; + const uint8_t argCount; } Capability; // Total Number of Capabilities @@ -165,15 +184,20 @@ typedef struct Capability { // -- Result Macros -// Guide_RM / Define_RM Pair +// Guide_RM / Record_RM / Define_RM // Guide_RM( index ) = result; // * index - Result Macro index number // * result - Result Macro guide (see ResultMacro) +// Record_RM( index ); +// * index - Result Macro index number // Define_RM( index ); // * index - Result Macro index number -// Must be used after Guide_RM +// Simple macros do not have a record +// Must be used after Guide_RM and Record_RM #define Guide_RM( index ) const uint8_t rm##index##_guide[] -#define Define_RM( index ) { rm##index##_guide } +#define Record_RM( index ) ResultMacroRecord rm##index##_record +#define Define_RM_Normal( index ) { MacroType_Normal, rm##index##_guide, &rm##index##_record } +#define Define_RM_Simple( index ) { MacroType_Simple, rm##index##_guide, 0 } // -- Result Macro List @@ -185,15 +209,19 @@ typedef struct Capability { // -- Trigger Macros -// Guide_TM / Define_TM Trigger Setup +// Guide_TM / Record_TM / Define_TM Trigger Setup // Guide_TM( index ) = trigger; // * index - Trigger Macro index number // * trigger - Trigger Macro guide (see TriggerMacro) +// Record_TM( index ); +// * index - Trigger Macro index number // Define_TM( index, result ); // * index - Trigger Macro index number // * result - Result Macro index number which is triggered by this Trigger Macro #define Guide_TM( index ) const uint8_t tm##index##_guide[] -#define Define_TM( index, result ) { tm##index##_guide, result } +#define Record_TM( index ) TriggerMacroRecord tm##index##_record +#define Define_TM_Normal( index, result ) { MacroType_Normal, tm##index##_guide, result, &tm##index##_record } +#define Define_TM_Simple( index, result ) { MacroType_Simple, tm##index##_guide, result, 0 } // -- Trigger Macro List @@ -238,9 +266,9 @@ typedef struct Capability { typedef struct Layer { const nat_ptr_t **triggerMap; - const char *name; - const uint8_t first; - const uint8_t last; + const char *name; + const uint8_t first; + const uint8_t last; } Layer; // Layer_IN( map, name, first ); diff --git a/Macro/Common/setup.cmake b/Macro/Common/setup.cmake new file mode 100644 index 0000000..513b61a --- /dev/null +++ b/Macro/Common/setup.cmake @@ -0,0 +1,29 @@ +###| CMake Kiibohd Controller Macro Module |### +# +# Written by Jacob Alexander in 2014-2015 for the Kiibohd Controller +# +# Released into the Public Domain +# +### + +### +# Sub-module flag, cannot be included stand-alone +# +set ( SubModule 1 ) + + +### +# Module C files +# +set ( Module_SRCS +) + + +### +# Compiler Family Compatibility +# +set ( ModuleCompatibility + arm + avr +) + diff --git a/Macro/PartialMap/usb_hid.h b/Macro/Common/usb_hid.h similarity index 100% rename from Macro/PartialMap/usb_hid.h rename to Macro/Common/usb_hid.h diff --git a/Macro/PartialMap/macro.c b/Macro/PartialMap/macro.c index 5f2cf77..a05552b 100644 --- a/Macro/PartialMap/macro.c +++ b/Macro/PartialMap/macro.c @@ -26,7 +26,7 @@ #include // Keymaps -#include "usb_hid.h" +#include #include // Generated using kll at compile time, in build directory // Local Includes @@ -132,17 +132,17 @@ var_uint_t macroTriggerListLayerCache[ MaxScanCode ]; // 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 -uint16_t macroTriggerMacroPendingList[ TriggerMacroNum ] = { 0 }; +uint16_t macroTriggerMacroPendingList[ TriggerMacroNum ]; uint16_t macroTriggerMacroPendingListSize = 0; // Layer Index Stack // * When modifying layer state and the state is non-0x0, the stack must be adjusted -uint16_t macroLayerIndexStack[ LayerNum + 1 ] = { 0 }; +uint16_t macroLayerIndexStack[ LayerNum + 1 ]; uint16_t macroLayerIndexStackSize = 0; // Pending Result Macro Index List // * Any result macro that needs processing from a previous macro processing loop -uint16_t macroResultMacroPendingList[ ResultMacroNum ] = { 0 }; +uint16_t macroResultMacroPendingList[ ResultMacroNum ]; uint16_t macroResultMacroPendingListSize = 0; @@ -452,6 +452,32 @@ inline void Macro_ledState( uint8_t ledCode, uint8_t state ) } +// Evaluate a ResultMacro Combination +var_uint_t Macro_evalResultMacroCombo( const uint8_t *guidePos, var_uint_t comboItem, uint8_t comboLength, const uint8_t state, const uint8_t stateType ) +{ + var_uint_t funcCount = 0; + + // Iterate through the Result Combo + while ( funcCount < comboLength ) + { + // Assign TriggerGuide element (key type, state and scancode) + ResultGuide *guide = (ResultGuide*)(&guidePos[ comboItem ]); + + // 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( state, stateType, &guide->args ); + + // Increment counters + funcCount++; + comboItem += ResultGuideSize( (ResultGuide*)(&guide[ comboItem ]) ); + } + + return comboItem; +} + + // Append result macro to pending list, checking for duplicates // Do nothing if duplicate inline void Macro_appendResultMacroToPendingList( const TriggerMacro *triggerMacro ) @@ -481,40 +507,40 @@ inline void Macro_appendResultMacroToPendingList( const TriggerMacro *triggerMac uint8_t scanCode = ((TriggerGuide*)&triggerMacro->guide[ pos - TriggerGuideSize ])->scanCode; // Lookup scanCode in buffer list for the current state and stateType + TriggerGuide *guide = 0; for ( uint8_t keyIndex = 0; keyIndex < macroTriggerListBufferSize; keyIndex++ ) { if ( macroTriggerListBuffer[ keyIndex ].scanCode == scanCode ) { - ResultMacroRecordList[ resultMacroIndex ].state = macroTriggerListBuffer[ keyIndex ].state; - ResultMacroRecordList[ resultMacroIndex ].stateType = macroTriggerListBuffer[ keyIndex ].type; + guide = ¯oTriggerListBuffer[ keyIndex ]; + break; } } - // Reset the macro position - ResultMacroRecordList[ resultMacroIndex ].pos = 0; -} + // Depending on the type of ResultMacro, either call the capability immediately, or delay + switch ( ResultMacroList[ resultMacroIndex ].type ) + { + // Simple ResultMacro + case MacroType_Simple: + // Call Capability immediately instead of waiting + // This means we don't have to store the state and type information + // (It would be thrown away during this scan cycle anyways) + Macro_evalResultMacroCombo( ResultMacroList[ resultMacroIndex ].guide, 1, ResultMacroList[ resultMacroIndex ].guide[ 0 ], guide->state, guide->type ); + break; + // Complex ResultMacro + case MacroType_Normal: + ResultMacroList[ resultMacroIndex ].record->state = guide->state; + ResultMacroList[ resultMacroIndex ].record->stateType = guide->type; -// Determine if long ResultMacro (more than 1 seqence element) -inline uint8_t Macro_isLongResultMacro( const ResultMacro *macro ) -{ - // Check the second sequence combo length - // If non-zero return non-zero (long sequence) - // 0 otherwise (short sequence) - var_uint_t position = 1; - for ( var_uint_t result = 0; result < macro->guide[0]; result++ ) - position += ResultGuideSize( (ResultGuide*)¯o->guide[ position ] ); - return macro->guide[ position ]; -} + // Reset the macro position + ResultMacroList[ resultMacroIndex ].record->pos = 0; + break; - -// Determine if long TriggerMacro (more than 1 sequence element) -inline uint8_t Macro_isLongTriggerMacro( const TriggerMacro *macro ) -{ - // Check the second sequence combo length - // If non-zero return non-zero (long sequence) - // 0 otherwise (short sequence) - return macro->guide[ macro->guide[0] * TriggerGuideSize + 1 ]; + default: + erro_print("Invalid MacroType. This is a bug"); + break; + } } @@ -644,7 +670,7 @@ TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex ) { // Lookup TriggerMacro const TriggerMacro *macro = &TriggerMacroList[ triggerMacroIndex ]; - TriggerMacroRecord *record = &TriggerMacroRecordList[ triggerMacroIndex ]; + TriggerMacroRecord *record = TriggerMacroList[ triggerMacroIndex ].record; // Check if macro has finished and should be incremented sequence elements if ( record->state == TriggerMacro_Release ) @@ -666,7 +692,7 @@ TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex ) } // Check if this is a long Trigger Macro - uint8_t longMacro = Macro_isLongTriggerMacro( macro ); + uint8_t longMacro = macro->type == MacroType_Normal; // Iterate through the items in the combo, voting the on the key state // If any of the pressed keys do not match, fail the macro @@ -752,7 +778,7 @@ TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex ) if ( macro->guide[ pos + comboLength + 1 ] == 0 ) { // Long result macro (more than 1 combo) - if ( Macro_isLongResultMacro( &ResultMacroList[ macro->result ] ) ) + if ( ResultMacroList[ macro->result ].type == MacroType_Normal ) { // Only ever trigger result once, on press if ( overallVote == TriggerMacroVote_Pass ) @@ -764,7 +790,7 @@ TriggerMacroEval Macro_evalTriggerMacro( var_uint_t triggerMacroIndex ) else { // Only trigger result once, on press, if long trigger (more than 1 combo) - if ( Macro_isLongTriggerMacro( macro ) ) + if ( macro->type == MacroType_Normal ) { return TriggerMacroEval_DoResultAndRemove; } @@ -797,7 +823,13 @@ inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex ) { // Lookup ResultMacro const ResultMacro *macro = &ResultMacroList[ resultMacroIndex ]; - ResultMacroRecord *record = &ResultMacroRecordList[ resultMacroIndex ]; + ResultMacroRecord *record = ResultMacroList[ resultMacroIndex ].record; + + // If this is a simple ResultMacro, the capability has already been called, remove + if ( macro->type == MacroType_Simple ) + { + return ResultMacroEval_Remove; + } // Current Macro position var_uint_t pos = record->pos; @@ -805,28 +837,11 @@ inline ResultMacroEval Macro_evalResultMacro( var_uint_t resultMacroIndex ) // Length of combo being processed uint8_t comboLength = macro->guide[ pos ]; - // Function Counter, used to keep track of the combo items processed - var_uint_t funcCount = 0; - // Combo Item Position within the guide var_uint_t comboItem = pos + 1; - // Iterate through the Result Combo - while ( funcCount < comboLength ) - { - // Assign TriggerGuide element (key type, state and scancode) - ResultGuide *guide = (ResultGuide*)(¯o->guide[ comboItem ]); - - // 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( record->state, record->stateType, &guide->args ); - - // Increment counters - funcCount++; - comboItem += ResultGuideSize( (ResultGuide*)(¯o->guide[ comboItem ]) ); - } + // Evaluate ResultCombo + comboItem = Macro_evalResultMacroCombo( macro->guide, comboItem, comboLength, record->state, record->stateType ); // Move to next item in the sequence record->pos = comboItem; @@ -889,8 +904,8 @@ inline void Macro_updateTriggerMacroPendingList() macroTriggerMacroPendingList[ macroTriggerMacroPendingListSize++ ] = triggerMacroIndex; // Reset macro position - TriggerMacroRecordList[ triggerMacroIndex ].pos = 0; - TriggerMacroRecordList[ triggerMacroIndex ].state = TriggerMacro_Waiting; + TriggerMacroList[ triggerMacroIndex ].record->pos = 0; + TriggerMacroList[ triggerMacroIndex ].record->state = TriggerMacro_Waiting; } } } @@ -1011,16 +1026,22 @@ inline void Macro_setup() // Initialize TriggerMacro states for ( var_uint_t macro = 0; macro < TriggerMacroNum; macro++ ) { - TriggerMacroRecordList[ macro ].pos = 0; - TriggerMacroRecordList[ macro ].state = TriggerMacro_Waiting; + if ( TriggerMacroList[ macro ].type == MacroType_Normal ) + { + TriggerMacroList[ macro ].record->pos = 0; + TriggerMacroList[ macro ].record->state = TriggerMacro_Waiting; + } } // Initialize ResultMacro states for ( var_uint_t macro = 0; macro < ResultMacroNum; macro++ ) { - ResultMacroRecordList[ macro ].pos = 0; - ResultMacroRecordList[ macro ].state = 0; - ResultMacroRecordList[ macro ].stateType = 0; + if ( ResultMacroList[ macro ].type == MacroType_Normal ) + { + ResultMacroList[ macro ].record->pos = 0; + ResultMacroList[ macro ].record->state = 0; + ResultMacroList[ macro ].record->stateType = 0; + } } } @@ -1358,7 +1379,7 @@ void macroDebugShowTrigger( var_uint_t index ) // Trigger Macro Show const TriggerMacro *macro = &TriggerMacroList[ index ]; - TriggerMacroRecord *record = &TriggerMacroRecordList[ index ]; + TriggerMacroRecord *record = TriggerMacroList[ index ].record; print( NL ); info_msg("Trigger Macro Index: "); @@ -1430,7 +1451,7 @@ void macroDebugShowResult( var_uint_t index ) // Trigger Macro Show const ResultMacro *macro = &ResultMacroList[ index ]; - ResultMacroRecord *record = &ResultMacroRecordList[ index ]; + ResultMacroRecord *record = ResultMacroList[ index ].record; print( NL ); info_msg("Result Macro Index: "); @@ -1502,10 +1523,13 @@ void macroDebugShowResult( var_uint_t index ) printInt16( (uint16_t)record->pos ); // Hopefully large enough :P (can't assume 32-bit) // Display final trigger state/type - print( NL "Final Trigger State (State/Type): " ); - printHex( record->state ); - print("/"); - printHex( record->stateType ); + if ( macro->type == MacroType_Normal ) + { + print( NL "Final Trigger State (State/Type): " ); + printHex( record->state ); + print("/"); + printHex( record->stateType ); + } } void cliFunc_macroShow( char* args ) diff --git a/Macro/PartialMap/setup.cmake b/Macro/PartialMap/setup.cmake index 7f72689..ba828f6 100644 --- a/Macro/PartialMap/setup.cmake +++ b/Macro/PartialMap/setup.cmake @@ -1,4 +1,4 @@ -###| CMake Kiibohd Controller Macro Module |### +###| CMake Kiibohd Controller PartialMap Macro Module |### # # Written by Jacob Alexander in 2014-2015 for the Kiibohd Controller # @@ -7,6 +7,12 @@ ### +### +# Required Sub-modules +# +AddModule ( Macro Common ) + + ### # Module C files #