### 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,
)
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"
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)
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 ):
# 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
#print ( pformat( tokenSequence ) ) # Display tokenization
tree = parse( tokenSequence )
- # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
- macros_map.replayCachedAssignments()
-
### 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 )
+ # Process base layout files
+ for filename in baseFiles:
+ processKLLFile( filename )
+ macros_map.completeBaseLayout() # Indicates to macros_map that the base layout is complete
+
# 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()