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