diff python/devices.py @ 129:9e350a7dde98

Changed architecture and updated the util
author Windel Bouwman
date Fri, 18 Jan 2013 12:52:11 +0100
parents 51cc127648e4
children
line wrap: on
line diff
--- a/python/devices.py	Sun Jan 13 17:31:35 2013 +0100
+++ b/python/devices.py	Fri Jan 18 12:52:11 2013 +0100
@@ -1,13 +1,13 @@
 import sys
-
+import usb
 
 # Global device list to which devices are registered.
-deviceList = {}
+devices = {}
 
 def registerDevice(chipId):
    """ Decorator to register a device """
    def wrapper(dev):
-      deviceList[chipId] = dev
+      devices[chipId] = dev
       return dev
    return wrapper
 
@@ -20,21 +20,41 @@
       return iface
    return wrapper
 
+def createInterfaces():
+   """ Create a list of detected interfaces """
+   ctx = usb.UsbContext()
+
+   # Retrieve all usb devices:
+   devs = ctx.DeviceList
+   keys = interfaces.keys()
+
+   # Filter function to filter only registered interfaces:
+   def filt(usbiface):
+      return (usbiface.VendorId, usbiface.ProductId) in keys
+   def buildInterface(usbiface):
+      key = (usbiface.VendorId, usbiface.ProductId)
+      iface = interfaces[key]
+      return iface(usbiface)
+   return [buildInterface(uif) for uif in filter(filt, devs)]
+
 class Device:
    """
       Base class for a device possibly connected via an interface.
    """
-   pass
+   def __init__(self, iface):
+      # Store the interface through which this device is connected:
+      assert isinstance(iface, Interface)
+      self.iface = iface
 
 class Interface:
    """
       Generic interface class. Connected via Usb to a JTAG interface.
       Possibly is connected with a certain chip.
    """
-   def getDevice(self):
+   def createDevice(self):
       """ Try to get the device connected to this interface """
-      if self.ChipId in deviceList:
-         return deviceList[self.ChipId](self)
+      if self.ChipId in devices:
+         return devices[self.ChipId](self)
       raise STLinkException('No device found!')
 
 class STLinkException(Exception):