From 9b37bebceb8cc5db06246da3f11cebc33a942460 Mon Sep 17 00:00:00 2001 From: Scott Petersen Date: Mon, 14 Oct 2013 16:28:35 -0700 Subject: [PATCH] Added send to ad2usb class and a basic firmwareupload utility. --- firmwareupload.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++ pyad2usb/ad2usb.py | 4 ++++ 2 files changed, 61 insertions(+) create mode 100755 firmwareupload.py diff --git a/firmwareupload.py b/firmwareupload.py new file mode 100755 index 0000000..9a8f932 --- /dev/null +++ b/firmwareupload.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +import sys, time +import pyad2usb.ad2usb + +def handle_firmware(stage): + if stage == pyad2usb.ad2usb.util.Firmware.STAGE_START: + handle_firmware.wait_tick = 0 + handle_firmware.upload_tick = 0 + elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_WAITING: + if handle_firmware.wait_tick == 0: + sys.stdout.write('Waiting for device.') + handle_firmware.wait_tick += 1 + + sys.stdout.write('.') + sys.stdout.flush() + elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_BOOT: + if handle_firmware.wait_tick > 0: print "" + print "Rebooting device.." + 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!" + +def main(): + device = '/dev/ttyUSB0' + firmware = None + + if len(sys.argv) < 2: + print "Syntax: {0} [interface]".format(sys.argv[0]) + sys.exit(1) + + firmware = sys.argv[1] + if len(sys.argv) > 2: + device = sys.argv[2] + + print "Flashing device: {0}\r\nFirmware: {1}".format(device, firmware) + + dev = pyad2usb.ad2usb.devices.SerialDevice(interface=device) + dev.open(interface=device, baudrate=19200) + + time.sleep(3) + pyad2usb.ad2usb.util.Firmware.upload(dev, firmware, handle_firmware) + + dev.close() + +if __name__ == "__main__": + main() diff --git a/pyad2usb/ad2usb.py b/pyad2usb/ad2usb.py index c3af11e..132bced 100644 --- a/pyad2usb/ad2usb.py +++ b/pyad2usb/ad2usb.py @@ -276,6 +276,10 @@ class AD2USB(object): del self._device self._device = None + def send(self, data): + if self._device: + self._device.write(data) + def get_config(self): """ Retrieves the configuration from the device.