/host/asp_tool.py

https://bitbucket.org/duventj/nrfasp
Python | 137 lines | 120 code | 8 blank | 9 comment | 22 complexity | 10a7b23c5d452c9bc499c359a4e40d77 MD5 | raw file
  1. #!/usr/bin/env python3
  2. # @author Mehdi MAAREF
  3. import time
  4. import sys, os
  5. import threading
  6. import getopt
  7. from cmd import Cmd
  8. from usbasp import *
  9. from array import array
  10. class CmdPromptBase(Cmd):
  11. def __init__(self):
  12. super().__init__()
  13. self.prompt = 'Usbasp # '
  14. self.stop = False
  15. ''' Usefull commands '''
  16. def emptyline(self):
  17. pass
  18. def do_shell(self, cmd):
  19. try:
  20. self.res = os.system(cmd)
  21. except Exception as e:
  22. print(e)
  23. def postcmd(self, stop, line):
  24. self.stop = stop
  25. return stop
  26. ''' Exiting '''
  27. def help_exit(self):
  28. print('Exit the command prompt')
  29. def do_exit(self, arg=''):
  30. return not print('')
  31. def do_EOF(self, line='') :
  32. return self.do_exit('')
  33. class UsbaspPrompt(CmdPromptBase):
  34. def __init__(self):
  35. super().__init__()
  36. self.device = UsbASP()
  37. #self.device = None
  38. ''' Init commands '''
  39. def do_mirf_start_daemon(self, args):
  40. pass
  41. def do_mirf_stop_daemon(self, args):
  42. pass
  43. ''' Basic Read Write commands '''
  44. def do_read_str(self, args=''):
  45. print(self.device.read_str())
  46. def do_read_buffer(self, args=''):
  47. print(self.device.read_buffer())
  48. def help_read_buffer(self):
  49. print('reads the usb buffer in the device')
  50. def do_write_buffer(self, args):
  51. self.device.write(args.encode('utf-8'))
  52. def help_write_buffer(self):
  53. print('send a buffer to the device')
  54. def do_bulk_recv(self, args=""):
  55. r = self.device.bulkrecv()
  56. print(r)
  57. def do_mirf_select(self, arg=None):
  58. if arg is None or arg is '':
  59. print('devices found :', len(self.device.dev))
  60. elif int(arg) >= len(self.device.dev):
  61. print('devices found :', len(self.device.dev))
  62. else :
  63. self.device.idx = int(arg)
  64. ''' Test commands '''
  65. def do_echo(self, args):
  66. if not args :
  67. args='Hello Atmega'
  68. self.do_write_buffer(args)
  69. self.do_read_buffer()
  70. def do_led(self, args):
  71. if args == "on" or args == "off":
  72. self.device.set_led(args)
  73. else:
  74. print(self.device.get_led())
  75. def help_led(self):
  76. print('led <on | off | status>')
  77. ''' MIRF commands '''
  78. def do_mirf_read_all(self, arg=None):
  79. try:
  80. while(1):
  81. self.do_bulk_recv(arg)
  82. except Exception:
  83. pass
  84. def do_mirf_write_register(self, args):
  85. args = bytes.fromhex(args)
  86. print(args)
  87. print(self.device.write_mirf_register(args[0], list(args[1:])))
  88. r = self.device.bulkrecv()
  89. print(r)
  90. def do_mirf_raw_cmds(self, args):
  91. cmd_list = list(map(lambda cmd: list(bytes.fromhex(cmd)), args.split()))
  92. cmd_buffer = concat_cmds(cmd_list)
  93. print(cmd_buffer)
  94. print(self.device.send_mirf_raw_multi_cmd(cmd_buffer))
  95. self.do_mirf_read_all()
  96. def do_mirf_config(self, args=''):
  97. print(self.device.set_rx())
  98. self.do_mirf_read_all()
  99. pass
  100. def do_mirf_send(self, args):
  101. print(self.device.send_tx(args.encode('utf-8')))
  102. self.do_mirf_read_all()
  103. pass
  104. def do_mirf_status(self, args):
  105. print(self.device.read_mirf_register())
  106. r = self.device.bulkrecv()
  107. print(r)
  108. pass
  109. def do_mirf_get_data(self, args=''):
  110. print(self.device.mirf_get_data())
  111. self.do_mirf_read_all()
  112. if __name__ == "__main__":
  113. try:
  114. p = UsbaspPrompt()
  115. exit = False
  116. except Exception as e:
  117. exit = True
  118. print(e)
  119. while(not exit):
  120. try:
  121. p.cmdloop()
  122. except KeyboardInterrupt:
  123. print('^C')
  124. except Exception as e:
  125. if p is None:
  126. exit = True
  127. print('Error :', e)
  128. finally:
  129. exit = p.stop