diff python/st-util.py @ 129:9e350a7dde98

Changed architecture and updated the util
author Windel Bouwman
date Fri, 18 Jan 2013 12:52:11 +0100
parents ec1f2cc04d95
children 654093a9a1e3
line wrap: on
line diff
--- a/python/st-util.py	Sun Jan 13 17:31:35 2013 +0100
+++ b/python/st-util.py	Fri Jan 18 12:52:11 2013 +0100
@@ -3,7 +3,8 @@
 import sys
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
-import stlink, usb
+import stlink, devices, stm32
+from devices import Interface, Device
 
 class InformationDialog(QDialog):
    def __init__(self, parent):
@@ -16,6 +17,69 @@
          fl.addRow('Current mode:', QLabel(parent.stl.CurrentModeString))
          fl.addRow('Status:', QLabel(parent.stl.StatusString))
 
+class DeviceTreeModel(QAbstractItemModel):
+   def __init__(self):
+      super().__init__()
+      self.chipPixmap = QPixmap('chip.png').scaled(32, 32)
+      self.usbPixmap = QPixmap('usb.png').scaled(32, 32)
+      self.refresh()
+   def refresh(self):
+      """ Check all usb interfaces for interfaces """
+      self.interfaces = devices.createInterfaces()
+      self.devices = []
+      self.modelReset.emit()
+   def addDevice(self, device):
+      if device.iface in [lambda d: d.iface for d in self.devices]:
+         print('device already open')
+         return
+      self.devices.append(device)
+      self.modelReset.emit()
+
+   def index(self, row, column, parent):
+      if parent.isValid():
+         ip = parent.internalPointer()
+         if isinstance(ip, Interface):
+            devs = [d for d in self.devices if d.iface is ip]
+            return self.createIndex(row, column, devs[row])
+      else:
+         # root level
+         iface = self.interfaces[row]
+         return self.createIndex(row, column, iface)
+      return idx
+   def parent(self, index):
+      if index.isValid():
+         ip = index.internalPointer()
+         if isinstance(ip, Interface):
+            return QModelIndex()
+         elif isinstance(ip, Device):
+            iface = ip.iface
+            row = self.interfaces.index(iface)
+            return self.createIndex(row, 0, iface)
+         return QModelIndex()
+   def rowCount(self, parent):
+      if parent.isValid():
+         # non-root level:
+         ip = parent.internalPointer()
+         if isinstance(ip, Interface):
+            devs = [d for d in self.devices if d.iface is ip]
+            return len(devs)
+      else:
+         # root level:
+         return len(self.interfaces)
+      return 0
+   def columnCount(self, parent):
+      return 1
+   def data(self, index, role):
+      if index.isValid():
+         ip = index.internalPointer()
+         if role == Qt.DisplayRole:
+            return str(ip)
+         elif role == Qt.DecorationRole:
+            if isinstance(ip, Interface):
+               return self.usbPixmap
+            if isinstance(ip, Device):
+               return self.chipPixmap
+
 class StUtil(QMainWindow):
    connected = pyqtSignal(bool)
    def __init__(self):
@@ -40,8 +104,23 @@
 
       sb = self.statusBar()
 
+      tv = QTreeView()
+      self.setCentralWidget(tv)
+      self.mdl = DeviceTreeModel()
+      tv.setModel(self.mdl)
+      tv.activated.connect(self.openItem)
+
       self.connected.connect(self.handleConnectedChange)
       self.connected.emit(False)
+   def openItem(self, idx):
+      if idx.isValid():
+         ip = idx.internalPointer()
+         if isinstance(ip, Interface):
+            ip.open()
+            # Try to get a device:
+            self.mdl.addDevice(ip.createDevice())
+         if isinstance(ip, Device):
+            self.device = ip
    def handleConnectedChange(self, state):
       self.miscMenu.setEnabled(state)
       self.connectAction.setEnabled(not state)