- # 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 capArgExpander( items ):
+ '''
+ Capability arguments may need to be expanded
+ (e.g. 1 16 bit argument needs to be 2 8 bit arguments for the state machine)
+
+ If the number is negative, determine width of the final value, mask to max, subtract,
+ then convert to multiple bytes
+ '''
+ 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 ]
+
+ # Set last bit if value is negative
+ if num < 0:
+ max_val = 2 ** (argLen * 8)
+ num += max_val
+
+ # XXX Yes, little endian from how the uC structs work
+ byteForm = num.to_bytes( argLen, byteorder='little' )
+
+ # 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> ]