Browse Source

kll compiler now working!

- Basic ScanCode to USBCode mapping now generating for the kiibohd controller
- Small fix to template
- Backend
- Macro correlation and transformation

TODO
- More testing
- Analog support
- LED support
- Layer support (only supports default layer currently)
simple
Jacob Alexander 9 years ago
parent
commit
81231a708e
5 changed files with 242 additions and 13 deletions
  1. 147
    3
      backends/kiibohd.py
  2. 1
    0
      examples/simple2.kll
  3. 11
    6
      kll.py
  4. 81
    3
      kll_lib/containers.py
  5. 2
    1
      templates/kiibohdKeymap.h

+ 147
- 3
backends/kiibohd.py View File

@@ -66,7 +66,7 @@ class Backend:


# Processes content for fill tags and does any needed dataset calculations
def process( self, capabilities ):
def process( self, capabilities, macros ):
## Capabilities ##
self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n"

@@ -78,10 +78,154 @@ class Backend:

self.fill_dict['CapabilitiesList'] += "};"

print( self.fill_dict['CapabilitiesList'] )

## Results Macros ##
self.fill_dict['ResultMacros'] = ""

# Iterate through each of the result macros
for result in range( 0, len( macros.resultsIndexSorted ) ):
self.fill_dict['ResultMacros'] += "Guide_RM( {0} ) = {{ ".format( result )

# Add the result macro capability index guide (including capability arguments)
# See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
for sequence in range( 0, len( macros.resultsIndexSorted[ result ] ) ):
# For each combo in the sequence, add the length of the combo
self.fill_dict['ResultMacros'] += "{0}, ".format( len( macros.resultsIndexSorted[ result ][ sequence ] ) )

# For each combo, add each of the capabilities used and their arguments
for combo in range( 0, len( macros.resultsIndexSorted[ result ][ sequence ] ) ):
resultItem = macros.resultsIndexSorted[ result ][ sequence ][ combo ]

# Add the capability index
self.fill_dict['ResultMacros'] += "{0}, ".format( capabilities.getIndex( resultItem[0] ) )

# Add each of the arguments of the capability
for arg in range( 0, len( resultItem[1] ) ):
self.fill_dict['ResultMacros'] += "0x{0:02X}, ".format( resultItem[1][ arg ] )

# Add list ending 0 and end of list
self.fill_dict['ResultMacros'] += "0 };\n"
self.fill_dict['ResultMacros'] = self.fill_dict['ResultMacros'][:-1] # Remove last newline


## Result Macro List ##
self.fill_dict['ResultMacroList'] = "ResultMacro ResultMacroList[] = {\n"

# Iterate through each of the result macros
for result in range( 0, len( macros.resultsIndexSorted ) ):
self.fill_dict['ResultMacroList'] += "\tDefine_RM( {0} ),\n".format( result )
self.fill_dict['ResultMacroList'] += "};"


## Trigger Macros ##
self.fill_dict['TriggerMacros'] = ""

# Iterate through each of the trigger macros
for trigger in range( 0, len( macros.triggersIndexSorted ) ):
self.fill_dict['TriggerMacros'] += "Guide_TM( {0} ) = {{ ".format( trigger )

# Add the trigger macro scan code guide
# See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
for sequence in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ] ) ):
# For each combo in the sequence, add the length of the combo
self.fill_dict['TriggerMacros'] += "{0}, ".format( len( macros.triggersIndexSorted[ trigger ][0][ sequence ] ) )

# For each combo, add the key type, key state and scan code
for combo in range( 0, len( macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ] ) ):
triggerItem = macros.triggersIndexSorted[ trigger ][ 0 ][ sequence ][ combo ]

# TODO Add support for Analog keys
# TODO Add support for LED states
self.fill_dict['TriggerMacros'] += "0x00, 0x01, 0x{0:02X}, ".format( triggerItem )

# Add list ending 0 and end of list
self.fill_dict['TriggerMacros'] += "0 };\n"
self.fill_dict['TriggerMacros'] = self.fill_dict['TriggerMacros'][ :-1 ] # Remove last newline


## Trigger Macro List ##
self.fill_dict['TriggerMacroList'] = "TriggerMacro TriggerMacroList[] = {\n"

# Iterate through each of the trigger macros
for trigger in range( 0, len( macros.triggersIndexSorted ) ):
# Use TriggerMacro Index, and the corresponding ResultMacro Index
self.fill_dict['TriggerMacroList'] += "\tDefine_TM( {0}, {1} ),\n".format( trigger, macros.triggersIndexSorted[ trigger ][1] )
self.fill_dict['TriggerMacroList'] += "};"


## Max Scan Code ##
self.fill_dict['MaxScanCode'] = "#define MaxScanCode 0x{0:X}".format( macros.overallMaxScanCode )


## Default Layer and Default Layer Scan Map ##
self.fill_dict['DefaultLayerTriggerList'] = ""
self.fill_dict['DefaultLayerScanMap'] = "const unsigned int *default_scanMap[] = {\n"

# Iterate over triggerList and generate a C trigger array for the default map and default map array
for triggerList in range( 0, len( macros.triggerList[ 0 ] ) ):
# Generate ScanCode index and triggerList length
self.fill_dict['DefaultLayerTriggerList'] += "Define_TL( default, 0x{0:02X} ) = {{ {1}".format( triggerList, len( macros.triggerList[ 0 ][ triggerList ] ) )

# Add scanCode trigger list to Default Layer Scan Map
self.fill_dict['DefaultLayerScanMap'] += "default_tl_0x{0:02X}, ".format( triggerList )

# Add each item of the trigger list
for trigger in macros.triggerList[ 0 ][ triggerList ]:
self.fill_dict['DefaultLayerTriggerList'] += ", {0}".format( trigger )

self.fill_dict['DefaultLayerTriggerList'] += " };\n"
self.fill_dict['DefaultLayerTriggerList'] = self.fill_dict['DefaultLayerTriggerList'][:-1] # Remove last newline
self.fill_dict['DefaultLayerScanMap'] = self.fill_dict['DefaultLayerScanMap'][:-2] # Remove last comma and space
self.fill_dict['DefaultLayerScanMap'] += "\n};"

#print( self.fill_dict['DefaultLayerTriggerList'] )
#print( self.fill_dict['DefaultLayerScanMap'] )


## Partial Layers ##
self.fill_dict['PartialLayerTriggerLists'] = ""
# TODO
#print( self.fill_dict['PartialLayerTriggerLists'] )


## Partial Layer Scan Maps ##
self.fill_dict['PartialLayerScanMaps'] = ""
# TODO
#print( self.fill_dict['PartialLayerScanMaps'] )


## Layer Index List ##
self.fill_dict['LayerIndexList'] = "Layer LayerIndex[] = {\n"

# Iterate over each layer, adding it to the list
for layer in range( 0, len( macros.triggerList ) ):
# Default map is a special case, always the first index
if layer == 0:
self.fill_dict['LayerIndexList'] += '\tLayer_IN( default_scanMap, "DefaultMap" ),\n'
else:
# TODO Partial Layer
pass
self.fill_dict['LayerIndexList'] += "};"

#print( self.fill_dict['LayerIndexList'] )


# Generates the output keymap with fill tags filled
def generate( self, filepath ):
print("My path: {0}".format( filepath) )
# Process each line of the template, outputting to the target path
with open( filepath, 'w' ) as outputFile:
with open( self.templatePath, 'r' ) as templateFile:
for line in templateFile:
# TODO Support multiple replacements per line
# TODO Support replacement with other text inline
match = re.findall( '<\|([^|>]+)\|>', line )

# If match, replace with processed variable
if match:
outputFile.write( self.fill_dict[ match[ 0 ] ] )
outputFile.write("\n")

# Otherwise, just append template to output file
else:
outputFile.write( line )


+ 1
- 0
examples/simple2.kll View File

@@ -19,4 +19,5 @@ S[ 0x7 - 0x9 ] : U"6";
S[ 0x7 - 0x9 ], S[0x2,0x3] : U"6";
S[ 0x2 - 0x9, 0x10 ] :+ U"r";
S0x0B :- U["Esc"];
S127 + S128 : U"0";


+ 11
- 6
kll.py View File

@@ -423,7 +423,7 @@ def eval_usbCode( triggers, operator, results ):
elif operator == ":":
macros_map.replaceScanCode( scanCode, result )

#print ( triggers )
print ( triggers )
print ( results )

def eval_variable( name, content ):
@@ -541,12 +541,17 @@ if __name__ == '__main__':
print ( pformat( tokenSequence ) ) # Display tokenization
tree = parse( tokenSequence )
#print ( tree )
print ( variable_dict )
print ( capabilities_dict )
#print ( variable_dict )
#print ( capabilities_dict )

# Do macro correlation and transformation
macros_map.generate()

# Process needed templating variables using backend
backend.process( capabilities_dict, macros_map )

# TODO Move
#macros_map.usbCodeToCapability( backend.usbCodeCapability() )
backend.process( capabilities_dict )
# Generate output file using template and backend
backend.generate( output )

# Successful Execution
sys.exit( 0 )

+ 81
- 3
kll_lib/containers.py View File

@@ -50,14 +50,14 @@ class Capabilities:
totalBytes = 0

# Iterate over the arguments, summing the total bytes
for arg in self.capabilities[ name ][1]:
totalBytes += int( arg[1] )
for arg in self.capabilities[ name ][ 1 ]:
totalBytes += int( arg[ 1 ] )

return totalBytes

# Name of the capability function
def funcName( self, name ):
return self.capabilities[ name ][0]
return self.capabilities[ name ][ 0 ]


# Only valid while dictionary keys are not added/removed
@@ -86,6 +86,14 @@ class Macros:
# Macro Storage
self.macros = [ dict() ]

# Correlated Macro Data
self.resultsIndex = dict()
self.triggersIndex = dict()
self.resultsIndexSorted = []
self.triggersIndexSorted = []
self.triggerList = []
self.maxScanCode = []

def __repr__( self ):
return "{0}".format( self.macros )

@@ -121,3 +129,73 @@ class Macros:

return scanCodeList

# Generate/Correlate Layers
def generate( self ):
self.generateIndices()
self.sortIndexLists()
self.generateTriggerLists()

# Generates Index of Results and Triggers
def generateIndices( self ):
# Iterate over every trigger result, and add to the resultsIndex and triggersIndex
for layer in range( 0, len( self.macros ) ):
for trigger in self.macros[ layer ].keys():
# Each trigger has a list of results
for result in self.macros[ layer ][ trigger ]:
# Only add, with an index, if result hasn't been added yet
if not result in self.resultsIndex:
self.resultsIndex[ result ] = len( self.resultsIndex )

# Then add a trigger for each result, if trigger hasn't been added yet
triggerItem = tuple( [ trigger, self.resultsIndex[ result ] ] )
if not triggerItem in self.triggersIndex:
self.triggersIndex[ triggerItem ] = len( self.triggersIndex )

# Sort Index Lists using the indices rather than triggers/results
def sortIndexLists( self ):
self.resultsIndexSorted = [ None ] * len( self.resultsIndex )
# Iterate over the resultsIndex and sort by index
for result in self.resultsIndex.keys():
self.resultsIndexSorted[ self.resultsIndex[ result ] ] = result

self.triggersIndexSorted = [ None ] * len( self.triggersIndex )
# Iterate over the triggersIndex and sort by index
for trigger in self.triggersIndex.keys():
self.triggersIndexSorted[ self.triggersIndex[ trigger ] ] = trigger

# Generates Trigger Lists per layer using index lists
def generateTriggerLists( self ):
for layer in range( 0, len( self.macros ) ):
# Set max scancode to 0xFF (255)
# But keep track of the actual max scancode and reduce the list size
self.triggerList.append( [ [] ] * 0xFF )
self.maxScanCode.append( 0x00 )

# Iterate through triggersIndex to locate necessary ScanCodes and corresponding triggerIndex
for triggerItem in self.triggersIndex.keys():
# Iterate over the trigger portion of the triggerItem (other part is the index)
for sequence in triggerItem[ 0 ]:
for combo in sequence:
# Append triggerIndex for each found scanCode of the Trigger List
# Do not re-add if triggerIndex is already in the Trigger List
if not triggerItem[1] in self.triggerList[ layer ][ combo ]:
# Append is working strangely with list pre-initialization
# Doing a 0 check replacement instead -HaaTa
if len( self.triggerList[ layer ][ combo ] ) == 0:
self.triggerList[ layer ][ combo ] = [ triggerItem[ 1 ] ]
else:
self.triggerList[ layer ][ combo ].append( triggerItem[1] )

# Look for max Scan Code
if combo > self.maxScanCode[ layer ]:
self.maxScanCode[ layer ] = combo

# Shrink triggerList to actual max size
self.triggerList[ layer ] = self.triggerList[ layer ][ : self.maxScanCode[ layer ] + 1 ]

# Determine overall maxScanCode
self.overallMaxScanCode = 0x00
for maxVal in self.maxScanCode:
if maxVal > self.overallMaxScanCode:
self.overallMaxScanCode = maxVal


+ 2
- 1
templates/kiibohdKeymap.h View File

@@ -64,7 +64,7 @@
// - Should be corollated with the max scan code in the scan module
// - Maximum value is 0x100 (0x0 to 0xFF)
// - Increasing it beyond the keyboard's capabilities is just a waste of ram...
#define MaxScanCode <MaxScanCode>
#define MaxScanCode <|MaxScanCode|>

// -- Trigger Lists
//
@@ -95,6 +95,7 @@
// - Default Map for ScanCode Lookup -
<|DefaultLayerScanMap|>


// - Partial Layer ScanCode Lookup Maps -
<|PartialLayerScanMaps|>