addfunc_parse.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. '''
  3. create additional functions ADC table, used for mapping analog pins to ADC channel numbers
  4. Currently only extracts ADC1 channels
  5. This assumes a csv file extracted from the datasheet using tablula:
  6. https://github.com/tabulapdf/tabula
  7. '''
  8. import sys, csv, os
  9. def is_pin(str):
  10. '''see if a string is a valid pin name'''
  11. if len(str) < 3:
  12. return False
  13. if str[0] != 'P':
  14. return False
  15. if str[1] not in "ABCDEFGH":
  16. return False
  17. try:
  18. p = int(str[2:])
  19. if p < 0 or p > 15:
  20. return False
  21. return True
  22. except ValueError:
  23. return False
  24. def parse_adc_table(fname, table):
  25. csvt = csv.reader(open(fname,'rb'))
  26. i = 0
  27. for row in csvt:
  28. for col in row:
  29. if is_pin(col) and row[-1].startswith('ADC1'):
  30. chan = row[-1]
  31. chan = chan.replace('\r', '')
  32. chan = chan.replace(' ', '')
  33. chan = chan.split('/')
  34. chan = chan[0]
  35. chan = chan.split(',')
  36. chan = chan[0]
  37. a = chan.split('_')
  38. if len(a) != 2:
  39. continue
  40. chan_num = a[1]
  41. if chan_num.startswith('IN'):
  42. chan_num = chan_num[2:]
  43. try:
  44. chan_num = int(chan_num)
  45. except Exception:
  46. continue
  47. table[col] = chan_num
  48. table = {}
  49. if len(sys.argv) != 2:
  50. print("Error: expected 1 CSV file")
  51. sys.exit(1)
  52. parse_adc_table(sys.argv[1], table)
  53. sys.stdout.write("ADC1_map = {\n");
  54. sys.stdout.write('\t# format is PIN : ADC1_CHAN\n')
  55. sys.stdout.write('\t# extracted from %s\n' % os.path.basename(sys.argv[1]))
  56. for k in sorted(table.keys()):
  57. s = '"' + k + '"'
  58. sys.stdout.write('\t%-4s\t:\t%s,\n' % (s, table[k]))
  59. sys.stdout.write("}\n");