| @@ -1,5 +1,6 @@ | |||||
| import ad2usb | import ad2usb | ||||
| import time | import time | ||||
| import traceback | |||||
| class NoDeviceError(Exception): | class NoDeviceError(Exception): | ||||
| pass | pass | ||||
| @@ -11,6 +12,11 @@ class TimeoutError(Exception): | |||||
| pass | pass | ||||
| class Firmware(object): | class Firmware(object): | ||||
| STAGE_BOOT = 1 | |||||
| STAGE_LOAD = 2 | |||||
| STAGE_UPLOADING = 3 | |||||
| STAGE_DONE = 4 | |||||
| def __init__(self): | def __init__(self): | ||||
| pass | pass | ||||
| @@ -18,17 +24,17 @@ class Firmware(object): | |||||
| pass | pass | ||||
| @staticmethod | @staticmethod | ||||
| def upload(dev, filename): | |||||
| def upload(dev, filename, progress_callback=None): | |||||
| def do_upload(): | def do_upload(): | ||||
| with open(filename) as f: | with open(filename) as f: | ||||
| print 'firmwaring this mofo!' | |||||
| for line in f: | for line in f: | ||||
| line = line.rstrip() | line = line.rstrip() | ||||
| if line[0] == ':': | if line[0] == ':': | ||||
| print "> {0}".format(line) | |||||
| dev.write(line + "\r") | dev.write(line + "\r") | ||||
| crap = dev.read_line() | |||||
| print "< {0}".format(crap) | |||||
| res = dev.read_line() | |||||
| if progress_callback is not None: | |||||
| progress_callback(Firmware.STAGE_UPLOADING) | |||||
| time.sleep(0.05) | time.sleep(0.05) | ||||
| @@ -58,16 +64,23 @@ class Firmware(object): | |||||
| raise NoDeviceError('No device specified for firmware upload.') | raise NoDeviceError('No device specified for firmware upload.') | ||||
| dev.close_reader() | dev.close_reader() | ||||
| time.sleep(1) | |||||
| time.sleep(5) | |||||
| try: | try: | ||||
| dev.write("=\r\n") | dev.write("=\r\n") | ||||
| if progress_callback is not None: | |||||
| progress_callback(Firmware.STAGE_BOOT) | |||||
| read_until('!boot', timeout=10.0) | read_until('!boot', timeout=10.0) | ||||
| dev.write("=\r\n") | dev.write("=\r\n") | ||||
| if progress_callback is not None: | |||||
| progress_callback(Firmware.STAGE_LOAD) | |||||
| read_until('!load', timeout=10.0) | read_until('!load', timeout=10.0) | ||||
| do_upload() | do_upload() | ||||
| if progress_callback is not None: | |||||
| progress_callback(Firmware.STAGE_DONE) | |||||
| except TimeoutError, err: | except TimeoutError, err: | ||||
| print traceback.print_exc(err) | print traceback.print_exc(err) | ||||
| pass | pass | ||||
| @@ -4,6 +4,7 @@ import pyad2usb.ad2usb | |||||
| import time | import time | ||||
| import signal | import signal | ||||
| import traceback | import traceback | ||||
| import sys | |||||
| running = True | running = True | ||||
| @@ -30,6 +31,24 @@ def handle_attached(sender, args): | |||||
| def handle_detached(sender, args): | def handle_detached(sender, args): | ||||
| print 'detached', args | print 'detached', args | ||||
| def handle_firmware(stage): | |||||
| if stage == pyad2usb.ad2usb.util.Firmware.STAGE_BOOT: | |||||
| print 'Rebooting device..' | |||||
| handle_firmware.upload_tick = 0 | |||||
| elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_LOAD: | |||||
| print 'Waiting for boot loader..' | |||||
| elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_UPLOADING: | |||||
| if handle_firmware.upload_tick == 0: | |||||
| sys.stdout.write('Uploading firmware.') | |||||
| handle_firmware.upload_tick += 1 | |||||
| if handle_firmware.upload_tick % 30 == 0: | |||||
| sys.stdout.write('.') | |||||
| sys.stdout.flush() | |||||
| elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_DONE: | |||||
| print "\r\nDone!" | |||||
| signal.signal(signal.SIGINT, signal_handler) | signal.signal(signal.SIGINT, signal_handler) | ||||
| @@ -55,7 +74,7 @@ try: | |||||
| dev.on_open += handle_open | dev.on_open += handle_open | ||||
| dev.on_close += handle_close | dev.on_close += handle_close | ||||
| #dev.on_read += handle_read | #dev.on_read += handle_read | ||||
| dev.on_write += handle_write | |||||
| #dev.on_write += handle_write | |||||
| #a2u.open() | #a2u.open() | ||||
| @@ -65,7 +84,7 @@ try: | |||||
| #dev.open(baudrate=19200, interface='/dev/ttyUSB0') | #dev.open(baudrate=19200, interface='/dev/ttyUSB0') | ||||
| dev.open() | dev.open() | ||||
| pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex') | |||||
| pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware) | |||||
| while running: | while running: | ||||
| time.sleep(0.1) | time.sleep(0.1) | ||||