X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=kll.py;h=6d79b2e0295e75f077c0e57b401394a8c912bf80;hb=1454611c7b9b4b930566a8fbf1783c66924f142b;hp=1e477a1efb0103ad65f45923ebdd38f8f609ab6a;hpb=1f0bf65417613b1cb3a5bfd744750e1e823c8622;p=kiibohd-kll.git diff --git a/kll.py b/kll.py index 1e477a1..6d79b2e 100755 --- 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] ...", 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,13 +84,15 @@ 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" "Base .kll files (that define the scan code maps) must be defined for each partial map." ) - pArgs.add_argument( '-t', '--template', type=str, default="templateKeymap.h", + pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h", help="Specify template used to generate the keymap.\n" - "Default: templateKeymap.h" ) + "Default: templates/kiibohdKeymap.h" ) pArgs.add_argument( '-o', '--output', type=str, default="templateKeymap.h", help="Specify output file. Writes to current working directory by default.\n" "Default: generatedKeymap.h" ) @@ -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) @@ -133,10 +142,10 @@ def tokenize( string ): ( 'CodeEnd', ( r'\]', ) ), ( 'String', ( r'"[^"]*"', VERBOSE ) ), ( 'SequenceString', ( r"'[^']*'", ) ), + ( 'Operator', ( r'=>|:\+|:-|:|=', ) ), ( 'Comma', ( r',', ) ), ( 'Dash', ( r'-', ) ), ( 'Plus', ( r'\+', ) ), - ( 'Operator', ( r'=>|:|=', ) ), ( 'Parenthesis', ( r'\(|\)', ) ), ( 'Number', ( r'-?(0x[0-9a-fA-F]+)|(0|([1-9][0-9]*))', VERBOSE ) ), ( 'Name', ( r'[A-Za-z_][A-Za-z_0-9]*', ) ), @@ -155,8 +164,7 @@ def tokenize( string ): ### Parsing ### ## Map Arrays -scanCode_map = [ None ] * 0xFF # Define 8 bit address width -usbCode_map = [ None ] * 0xFF # Define 8 bit address width +macros_map = Macros() variable_dict = dict() capabilities_dict = Capabilities() @@ -301,6 +309,9 @@ eol = a( Token( 'EndOfLine', ';' ) ) def listElem( item ): return [ item ] +def listToTuple( items ): + return tuple( items ) + # Flatten only the top layer (list of lists of ...) def oneLayerFlatten( items ): mainList = [] @@ -310,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 ] ],