view python/st-util.py @ 127:ec1f2cc04d95

Added st util gui start
author Windel Bouwman
date Sun, 13 Jan 2013 13:02:29 +0100
parents
children 9e350a7dde98
line wrap: on
line source

#!/usr/bin/python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import stlink, usb

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 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()

      self.connected.connect(self.handleConnectedChange)
      self.connected.emit(False)
   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()