comparison 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
comparison
equal deleted inserted replaced
126:bbf4c9b138d4 127:ec1f2cc04d95
1 #!/usr/bin/python
2
3 import sys
4 from PyQt4.QtCore import *
5 from PyQt4.QtGui import *
6 import stlink, usb
7
8 class InformationDialog(QDialog):
9 def __init__(self, parent):
10 super().__init__(parent)
11 self.setWindowTitle('Info')
12 fl = QFormLayout(self)
13 if parent.stl:
14 fl.addRow('ST link version:', QLabel(parent.stl.Version))
15 fl.addRow('Chip id:', QLabel('0x{0:X}'.format(parent.stl.ChipId)))
16 fl.addRow('Current mode:', QLabel(parent.stl.CurrentModeString))
17 fl.addRow('Status:', QLabel(parent.stl.StatusString))
18
19 class StUtil(QMainWindow):
20 connected = pyqtSignal(bool)
21 def __init__(self):
22 super().__init__()
23 self.stl = None
24 def buildAction(name, callback, shortcut=None):
25 a = QAction(name, self)
26 a.triggered.connect(callback)
27 if shortcut:
28 a.setShortcut(shortcut)
29 return a
30 mb = self.menuBar()
31 fileMenu = mb.addMenu("File")
32 self.connectAction = buildAction('Connect', self.connect)
33 fileMenu.addAction(self.connectAction)
34 self.disconnectAction = buildAction('Disconnect', self.disconnect)
35 fileMenu.addAction(self.disconnectAction)
36
37 self.miscMenu = mb.addMenu("Misc")
38 infoAction = buildAction('Info', self.info)
39 self.miscMenu.addAction(infoAction)
40
41 sb = self.statusBar()
42
43 self.connected.connect(self.handleConnectedChange)
44 self.connected.emit(False)
45 def handleConnectedChange(self, state):
46 self.miscMenu.setEnabled(state)
47 self.connectAction.setEnabled(not state)
48 self.disconnectAction.setEnabled(state)
49 msg = 'Connected!' if state else 'Disconnected!'
50 self.statusBar().showMessage(msg)
51 def info(self):
52 infoDialog = InformationDialog(self)
53 infoDialog.exec()
54 def connect(self):
55 try:
56 self.stl = stlink.STLink()
57 self.stl.open()
58 except (stlink.STLinkException, usb.UsbError) as e:
59 QMessageBox.warning(self, "Error", str(e))
60 self.stl = None
61 if self.stl:
62 self.connected.emit(True)
63 def disconnect(self):
64 if self.stl:
65 self.stl.close()
66 self.connected.emit(False)
67 self.stl = None
68
69 if __name__ == '__main__':
70 app = QApplication(sys.argv)
71 stu = StUtil()
72 stu.show()
73 app.exec()
74