]> git.donarmstrong.com Git - kiibohd-kll.git/blobdiff - kll.py
Changing array type to support different uC architectures.
[kiibohd-kll.git] / kll.py
diff --git a/kll.py b/kll.py
index 06ce83d91b91a6aae3641ec2c0e0c1a94b82f3de..fc7b42648544793f9a1728ab37016d49aeff45e8 100755 (executable)
--- a/kll.py
+++ b/kll.py
@@ -61,12 +61,17 @@ argparse._ = textFormatter_gettext
 
 ### Argument Parsing ###
 
+def checkFileExists( filename ):
+       if not os.path.isfile( filename ):
+               print ( "{0} {1} does not exist...".format( ERROR, filename ) )
+               sys.exit( 1 )
+
 def processCommandLineArgs():
        # Setup argument processor
        pArgs = argparse.ArgumentParser(
                usage="%(prog)s [options] <file1>...",
                description="Generates .h file state tables and pointer indices from KLL .kll files.",
-               epilog="Example: {0} TODO".format( os.path.basename( sys.argv[0] ) ),
+               epilog="Example: {0} mykeyboard.kll -d colemak.kll -p hhkbpro2.kll -p symbols.kll".format( os.path.basename( sys.argv[0] ) ),
                formatter_class=argparse.RawTextHelpFormatter,
                add_help=False,
 )
@@ -79,6 +84,8 @@ def processCommandLineArgs():
        pArgs.add_argument( '-b', '--backend', type=str, default="kiibohd",
                help="Specify target backend for the KLL compiler.\n"
                "Default: kiibohd" )
+       pArgs.add_argument( '-d', '--default', type=str, nargs='+',
+               help="Specify .kll files to layer on top of the default map to create a combined map." )
        pArgs.add_argument( '-p', '--partial', type=str, nargs='+', action='append',
                help="Specify .kll files to generate partial map, multiple files per flag.\n"
                "Each -p defines another partial map.\n"
@@ -96,24 +103,26 @@ def processCommandLineArgs():
        args = pArgs.parse_args()
 
        # Parameters
-       defaultFiles = args.files
+       baseFiles = args.files
+       defaultFiles = args.default
        partialFileSets = args.partial
+       if defaultFiles is None:
+               defaultFiles = []
        if partialFileSets is None:
                partialFileSets = [[]]
 
        # Check file existance
+       for filename in baseFiles:
+               checkFileExists( filename )
+
        for filename in defaultFiles:
-               if not os.path.isfile( filename ):
-                       print ( "{0} {1} does not exist...".format( ERROR, filename ) )
-                       sys.exit( 1 )
+               checkFileExists( filename )
 
        for partial in partialFileSets:
                for filename in partial:
-                       if not os.path.isfile( filename ):
-                               print ( "{0} {1} does not exist...".format( ERROR, filename ) )
-                               sys.exit( 1 )
+                       checkFileExists( filename )
 
-       return (defaultFiles, partialFileSets, args.backend, args.template, args.output)
+       return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.output)
 
 
 
@@ -312,6 +321,21 @@ def oneLayerFlatten( items ):
 
        return mainList
 
+  # Capability arguments may need to be expanded (e.g. 1 16 bit argument needs to be 2 8 bit arguments for the state machine)
+def capArgExpander( items ):
+       newArgs = []
+       # For each defined argument in the capability definition
+       for arg in range( 0, len( capabilities_dict[ items[0] ][1] ) ):
+               argLen = capabilities_dict[ items[0] ][1][ arg ][1]
+               num = items[1][ arg ]
+               byteForm = num.to_bytes( argLen, byteorder='little' ) # XXX Yes, little endian from how the uC structs work
+
+               # For each sub-argument, split into byte-sized chunks
+               for byte in range( 0, argLen ):
+                       newArgs.append( byteForm[ byte ] )
+
+       return tuple( [ items[0], tuple( newArgs ) ] )
+
   # Expand ranges of values in the 3rd dimension of the list, to a list of 2nd lists
   # i.e. [ sequence, [ combo, [ range ] ] ] --> [ [ sequence, [ combo ] ], <option 2>, <option 3> ]
 def optionExpansion( sequences ):
@@ -469,7 +493,7 @@ usbCode_sequence    = oneplus( ( usbCode_combo | seqString ) + skip( maybe( comm
 
   # Capabilities
 capFunc_arguments = many( number + skip( maybe( comma ) ) ) >> listToTuple
-capFunc_elem      = name + skip( parenthesis('(') ) + capFunc_arguments + skip( parenthesis(')') ) >> listElem
+capFunc_elem      = name + skip( parenthesis('(') ) + capFunc_arguments + skip( parenthesis(')') ) >> capArgExpander >> listElem
 capFunc_combo     = oneplus( ( usbCode_expanded | usbCode_elem | capFunc_elem ) + skip( maybe( plus ) ) ) >> listElem
 capFunc_sequence  = oneplus( ( capFunc_combo | seqString ) + skip( maybe( comma ) ) ) >> oneLayerFlatten
 
@@ -507,27 +531,47 @@ def parse( tokenSequence ):
 
 
 
+def processKLLFile( filename ):
+       with open( filename ) as file:
+               data = file.read()
+               tokenSequence = tokenize( data )
+               #print ( pformat( tokenSequence ) ) # Display tokenization
+               tree = parse( tokenSequence )
+
+
+
 ### Main Entry Point ###
 
 if __name__ == '__main__':
-       (defaultFiles, partialFileSets, backend_name, template, output) = processCommandLineArgs()
+       (baseFiles, defaultFiles, partialFileSets, backend_name, template, output) = processCommandLineArgs()
 
        # Load backend module
        global backend
        backend_import = importlib.import_module( "backends.{0}".format( backend_name ) )
        backend = backend_import.Backend( template )
 
-       #TODO Move elsewhere
-       for filename in defaultFiles:
-               with open( filename ) as file:
-                       data = file.read()
+       # Process base layout files
+       for filename in baseFiles:
+               processKLLFile( filename )
+       macros_map.completeBaseLayout() # Indicates to macros_map that the base layout is complete
 
-                       tokenSequence = tokenize( data )
-                       #print ( pformat( tokenSequence ) ) # Display tokenization
-                       tree = parse( tokenSequence )
+       # Default combined layer
+       for filename in defaultFiles:
+               processKLLFile( filename )
+       # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
+       macros_map.replayCachedAssignments()
 
+       # Iterate through additional layers
+       for partial in partialFileSets:
+               # Increment layer for each -p option
+               macros_map.addLayer()
+               # Iterate and process each of the file in the layer
+               for filename in partial:
+                       processKLLFile( filename )
                # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
                macros_map.replayCachedAssignments()
+               # Remove un-marked keys to complete the partial layer
+               macros_map.removeUnmarked()
 
        # Do macro correlation and transformation
        macros_map.generate()