usb_gen_strings.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. import json
  3. import sys
  4. def main(argv0, fn_in, fn_out, board=''):
  5. with open(fn_in, 'r') as fh_in, open(fn_out, 'w') as fh_out:
  6. # Arrays
  7. str_d = []
  8. # String 0
  9. str_d.append("""static const struct usb_str_desc _str0_desc = {
  10. .bLength = 4,
  11. .bDescriptorType = USB_DT_STR,
  12. .wString = { 0x0409 },
  13. };
  14. """)
  15. # String 1..n
  16. def sep(i, l):
  17. if i == l-1:
  18. return ''
  19. elif ((i & 7) == 7):
  20. return '\n\t\t'
  21. else:
  22. return ' '
  23. for i, ld in enumerate(fh_in.readlines()):
  24. ld = ld.strip()
  25. if ld.startswith('!{'):
  26. ld = json.loads(ld[1:])
  27. ld = ld[board] if board in ld else ld['']
  28. ll = len(ld)
  29. d = ''.join(['0x%04x,%s' % (ord(c), sep(j, ll)) for j,c in enumerate(ld)])
  30. str_d.append("""static const struct usb_str_desc _str%d_desc = {
  31. .bLength = %d,
  32. .bDescriptorType = USB_DT_STR,
  33. .wString = {
  34. %s
  35. },
  36. };
  37. """ % (i+1, ll*2+2, d))
  38. fh_out.write('\n'.join(str_d))
  39. # Array
  40. fh_out.write("\n")
  41. fh_out.write("static const struct usb_str_desc * const _str_desc_array[] = {\n")
  42. for i in range(len(str_d)):
  43. fh_out.write("\t& _str%d_desc,\n" % i)
  44. fh_out.write("};\n")
  45. if __name__ == '__main__':
  46. main(*sys.argv)