+#!/usr/bin/python
+
+import sys,os
+import argparse
+from intelhex import IntelHex
+
+def main(argv = None):
+ """ Configure the eeprom image for the hid keyboard device
+ --serialnum 8 --phraseA 'something' -keycodeB 4
+
+ 0: calibration byte
+ 1: calibration write count
+ 3: 6 byte serial number
+ 9: init delay value (ms/8)
+ 10: key A code
+ 11: phrase len for A
+ 12: key B code
+ 13: phrase len for B
+ 14..: phrase data
+ """
+
+ if (argv is None):
+ argv = sys.argv
+
+ parser = argparse.ArgumentParser(description=
+ r'configure keyboard device eeprom data')
+ parser.add_argument('filename', type=file,
+ help='eeprom data file in intel hex format')
+ parser.add_argument('--serial', type=int, help='set device serial number')
+ parser.add_argument('--phraseA', help='set phrase emitted by key A')
+ parser.add_argument('--keycodeA', type=int, help='set keycode for key A')
+ parser.add_argument('--phraseB', help='set phrase emitted by key B')
+ parser.add_argument('--keycodeB', type=int, help='set keycode for key B')
+ parser.add_argument('--init_delay', type=int,
+ help='set initialization delay in milliseconds')
+ parser.add_argument('--debug', action='store_true',
+ help='dump modified hex to stdout for review')
+ args = parser.parse_args(argv[1:])
+
+ lenA = 0 if args.phraseA is None else len(args.phraseA)
+ lenB = 0 if args.phraseB is None else len(args.phraseB)
+ if 14 + lenA + lenB > 255:
+ print("error: over 256 bytes of eeprom data")
+ return 1
+
+ data = IntelHex(args.filename)
+
+ if args.serial is not None:
+ data.puts(3, 'K%05d' % args.serial)
+ if args.init_delay is not None:
+ data[9] = delay / 8
+
+ if args.phraseA is not None:
+ data[11] = len(args.phraseA)
+ data.puts(14, args.phraseA)
+ else:
+ if args.keycodeA is not None:
+ data[10] = args.keycodeA
+
+ if args.phraseB is not None:
+ data[13] = len(args.phraseB)
+ data.puts(14 + lenA, args.phraseB)
+ else:
+ if args.keycodeB is not None:
+ data[12] = args.keycodeB
+
+ if args.debug:
+ data.dump()
+ else:
+ with open(argv[1], 'w') as f:
+ data.write_hex_file(f)
+
+ return 0
+
+if __name__=='__main__':
+ sys.exit(main())