view python/st-util.py @ 131:04e45faafd1d

Added register view
author Windel Bouwman
date Sat, 19 Jan 2013 18:41:49 +0100
parents 654093a9a1e3
children 205578c96a79
line wrap: on
line source

#!/usr/bin/python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import stlink, devices, stm32
from devices import Interface, Device

class InformationDialog(QDialog):
   def __init__(self, parent):
      super().__init__(parent)
      self.setWindowTitle('Info')
      fl = QFormLayout(self)
      if parent.stl:
         fl.addRow('ST link version:', QLabel(parent.stl.Version))
         fl.addRow('Chip id:', QLabel('0x{0:X}'.format(parent.stl.ChipId)))
         fl.addRow('Current mode:', QLabel(parent.stl.CurrentModeString))
         fl.addRow('Status:', QLabel(parent.stl.StatusString))

class RegisterModel(QAbstractTableModel):
   def __init__(self):
      super().__init__()
      self.regCount = 15
      self.device = None
   def rowCount(self, parent):
      if parent.isValid():
         return 0
      if self.device:
         return 21 # TODO make variable
      else:
         return 0
   def setDevice(self, dev):
      self.device = dev
      self.modelReset.emit()
   def columnCount(self, parent):
      if parent.isValid():
         return 0
      return 2
   def data(self, index, role):
      if index.isValid():
         row, col = index.row(), index.column()
         if role == Qt.DisplayRole:
            if col == 0:
               return 'R{0}'.format(row)
            elif col == 1:
               v = self.device.iface.read_reg(row)
               return '0x{0:X}'.format(v)

class RegisterView(QTableView):
   def __init__(self):
      super().__init__()
      self.mdl = RegisterModel()
      self.setModel(self.mdl)

class DeviceTreeModel(QAbstractItemModel):
   def __init__(self):
      super().__init__()
      self.chipPixmap = QPixmap('chip.png').scaled(32, 32)
      self.hardwarePixmap = QPixmap('hardware.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:
         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.hardwarePixmap
            if isinstance(ip, Device):
               return self.chipPixmap

class DeviceExplorer(QTreeView):
   """ Lists all interfaces plugged in and allows selection """
   deviceSelected = pyqtSignal(Device)
   def __init__(self):
      super().__init__()
      self.mdl = DeviceTreeModel()
      self.setModel(self.mdl)
      self.activated.connect(self.openItem)
      self.header().close()
      self.customContextMenuRequested.connect(self.openMenu)
      self.setContextMenuPolicy(Qt.CustomContextMenu)
   def openItem(self, idx):
      if idx.isValid():
         ip = idx.internalPointer()
         if isinstance(ip, Interface):
            if not ip.IsOpen:
               ip.open()
               # Try to get a device:
               self.mdl.addDevice(ip.createDevice())
         if isinstance(ip, Device):
            self.deviceSelected.emit(ip)
   def openMenu(self, pt):
      idx = self.indexAt(pt)
      menu = QMenu()
      menu.addAction('Refresh', self.mdl.refresh)
      if idx.isValid():
         ip = idx.internalPointer()
         if isinstance(ip, Interface):
            if ip.IsOpen:
               def closeInterface():
                  self.mdl.closeInterface(ip)
               menu.addAction('Close', closeInterface)
            else:
               def openInterface():
                  ip.open()
                  # Try to get a device:
                  self.mdl.addDevice(ip.createDevice())
               menu.addAction('Open', openInterface)
         elif isinstance(ip, Device):
            def selectDevice():
               self.deviceSelected.emit(ip)
            menu.addAction('Select', selectDevice)
      menu.exec(self.mapToGlobal(pt))

class StUtil(QMainWindow):
   connected = pyqtSignal(bool)
   def __init__(self):
      super().__init__()
      self.stl = None
      def buildAction(name, callback, shortcut=None):
         a = QAction(name, self)
         a.triggered.connect(callback)
         if shortcut:
            a.setShortcut(shortcut)
         return a
      mb = self.menuBar()
      fileMenu = mb.addMenu("File")
      self.connectAction = buildAction('Connect', self.connect)
      fileMenu.addAction(self.connectAction)
      self.disconnectAction = buildAction('Disconnect', self.disconnect)
      fileMenu.addAction(self.disconnectAction)

      self.miscMenu = mb.addMenu("Misc")
      infoAction = buildAction('Info', self.info)
      self.miscMenu.addAction(infoAction)

      sb = self.statusBar()

      devexplr = DeviceExplorer()
      self.setCentralWidget(devexplr)
      devexplr.deviceSelected.connect(self.handleDeviceSelected)

      self.connected.connect(self.handleConnectedChange)
      self.connected.emit(False)
   def handleDeviceSelected(self, dev):
      self.dev = dev
      self.handleConnectedChange(True)
   def handleConnectedChange(self, state):
      self.miscMenu.setEnabled(state)
      self.connectAction.setEnabled(not state)
      self.disconnectAction.setEnabled(state)
      msg = 'Connected!' if state else 'Disconnected!'
      self.statusBar().showMessage(msg)
   def info(self):
      infoDialog = InformationDialog(self)
      infoDialog.exec()
   def connect(self):
      try:
         self.stl = stlink.STLink()
         self.stl.open()
      except (stlink.STLinkException, usb.UsbError) as e:
         QMessageBox.warning(self, "Error", str(e))
         self.stl = None
      if self.stl:
         self.connected.emit(True)
   def disconnect(self):
      if self.stl:
         self.stl.close()
         self.connected.emit(False)
      self.stl = None

if __name__ == '__main__':
   app = QApplication(sys.argv)
   stu = StUtil()
   stu.show()
   app.exec()