view python/devices.py @ 192:6cd6260789a1

Added more tests for parser generator
author Windel Bouwman
date Sun, 26 May 2013 23:19:27 +0200
parents 9e350a7dde98
children
line wrap: on
line source

import sys
import usb

# Global device list to which devices are registered.
devices = {}

def registerDevice(chipId):
   """ Decorator to register a device """
   def wrapper(dev):
      devices[chipId] = dev
      return dev
   return wrapper

# Global interface dictionary.
interfaces = {}

def registerInterface(vid_pid):
   def wrapper(iface):
      interfaces[vid_pid] = iface
      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.
   """
   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 createDevice(self):
      """ Try to get the device connected to this interface """
      if self.ChipId in devices:
         return devices[self.ChipId](self)
      raise STLinkException('No device found!')

class STLinkException(Exception):
   """ Exception used for interfaces and devices """
   pass