util.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # greaseweazle/tools/util.py
  2. #
  3. # Greaseweazle control script: Utility functions.
  4. #
  5. # Written & released by Keir Fraser <keir.xen@gmail.com>
  6. #
  7. # This is free and unencumbered software released into the public domain.
  8. # See the file COPYING for more details, or visit <http://unlicense.org>.
  9. import os, sys, serial
  10. from greaseweazle import version
  11. from greaseweazle import usb as USB
  12. from greaseweazle.scp import SCP
  13. from greaseweazle.hfe import HFE
  14. def get_image_class(name):
  15. image_types = { '.scp': SCP, '.hfe': HFE }
  16. _, ext = os.path.splitext(name)
  17. if not ext.lower() in image_types:
  18. print("**Error: Unrecognised file suffix '%s'" % ext)
  19. return None
  20. return image_types[ext.lower()]
  21. def with_drive_selected(fn, usb, args):
  22. try:
  23. usb.drive_select(True)
  24. usb.drive_motor(True)
  25. fn(usb, args)
  26. except KeyboardInterrupt:
  27. print()
  28. usb.reset()
  29. usb.ser.close()
  30. usb.ser.open()
  31. finally:
  32. usb.drive_motor(False)
  33. usb.drive_select(False)
  34. def usb_open(devicename, is_update=False):
  35. usb = USB.Unit(serial.Serial(devicename))
  36. print("** %s v%u.%u, Host Tools v%u.%u"
  37. % (("Greaseweazle", "Bootloader")[usb.update_mode],
  38. usb.major, usb.minor,
  39. version.major, version.minor))
  40. if usb.update_mode and not is_update:
  41. print("Greaseweazle is in Firmware Update Mode:")
  42. print(" The only available action is \"update <update_file>\"")
  43. if usb.update_jumpered:
  44. print(" Remove the Update Jumper for normal operation")
  45. else:
  46. print(" Main firmware is erased: You *must* perform an update!")
  47. sys.exit(1)
  48. if is_update and not usb.update_mode:
  49. print("Greaseweazle is in Normal Mode:")
  50. print(" To \"update\" you must install the Update Jumper")
  51. sys.exit(1)
  52. if not usb.update_mode and usb.update_needed:
  53. print("Firmware is out of date: Require v%u.%u"
  54. % (version.major, version.minor))
  55. print("Install the Update Jumper and \"update <update_file>\"")
  56. sys.exit(1)
  57. return usb
  58. # Local variables:
  59. # python-indent: 4
  60. # End: