]> git.donarmstrong.com Git - kiibohd-kll.git/commitdiff
kll compiler now working!
authorJacob Alexander <haata@kiibohd.com>
Sun, 7 Sep 2014 03:56:46 +0000 (20:56 -0700)
committerJacob Alexander <haata@kiibohd.com>
Sun, 7 Sep 2014 03:56:46 +0000 (20:56 -0700)
- 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)

backends/kiibohd.py
examples/simple2.kll
kll.py
kll_lib/containers.py
templates/kiibohdKeymap.h

index f37e2e623cb5e96e12622ab6835895ce6ad661b0..47415a75a1f33b014c75b23692ae71b5c09a2153 100644 (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 )
 
index b5611af59196a5dbeaaabcea6cd82e85a4864fa2..367fd935ba4108a6d4118591547f602fb9db269d 100644 (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";
 
diff --git a/kll.py b/kll.py
index ca91da01cac80f109640cc415ec44efe2779d12d..21c4418fbca37b2062aa5cbcc0880f9f7b6e5590 100755 (executable)
--- a/kll.py
+++ b/kll.py
@@ -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 )
index 5d7bf1dd2939fb2efa0e7e4b1fb4a5e8b64a4ca3..d843574564b63c37ffce995bd44a1c13e277b3bc 100644 (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
+
index 5ec572e849b59a1f5b110a3210f23d10d7bd3af4..693164c264b282a9348cfdf7b8da602167d45242 100644 (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|>