annotate 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
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 *
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
6 import stlink, devices, stm32
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
7 from devices import Interface, Device
127
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
8
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
9 class InformationDialog(QDialog):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
10 def __init__(self, parent):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
11 super().__init__(parent)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
12 self.setWindowTitle('Info')
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
13 fl = QFormLayout(self)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
14 if parent.stl:
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
15 fl.addRow('ST link version:', QLabel(parent.stl.Version))
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
16 fl.addRow('Chip id:', QLabel('0x{0:X}'.format(parent.stl.ChipId)))
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
17 fl.addRow('Current mode:', QLabel(parent.stl.CurrentModeString))
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
18 fl.addRow('Status:', QLabel(parent.stl.StatusString))
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
19
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
20 class DeviceTreeModel(QAbstractItemModel):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
21 def __init__(self):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
22 super().__init__()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
23 self.chipPixmap = QPixmap('chip.png').scaled(32, 32)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
24 self.usbPixmap = QPixmap('usb.png').scaled(32, 32)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
25 self.refresh()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
26 def refresh(self):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
27 """ Check all usb interfaces for interfaces """
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
28 self.interfaces = devices.createInterfaces()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
29 self.devices = []
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
30 self.modelReset.emit()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
31 def addDevice(self, device):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
32 if device.iface in [lambda d: d.iface for d in self.devices]:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
33 print('device already open')
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
34 return
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
35 self.devices.append(device)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
36 self.modelReset.emit()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
37
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
38 def index(self, row, column, parent):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
39 if parent.isValid():
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
40 ip = parent.internalPointer()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
41 if isinstance(ip, Interface):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
42 devs = [d for d in self.devices if d.iface is ip]
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
43 return self.createIndex(row, column, devs[row])
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
44 else:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
45 # root level
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
46 iface = self.interfaces[row]
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
47 return self.createIndex(row, column, iface)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
48 return idx
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
49 def parent(self, index):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
50 if index.isValid():
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
51 ip = index.internalPointer()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
52 if isinstance(ip, Interface):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
53 return QModelIndex()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
54 elif isinstance(ip, Device):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
55 iface = ip.iface
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
56 row = self.interfaces.index(iface)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
57 return self.createIndex(row, 0, iface)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
58 return QModelIndex()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
59 def rowCount(self, parent):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
60 if parent.isValid():
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
61 # non-root level:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
62 ip = parent.internalPointer()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
63 if isinstance(ip, Interface):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
64 devs = [d for d in self.devices if d.iface is ip]
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
65 return len(devs)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
66 else:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
67 # root level:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
68 return len(self.interfaces)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
69 return 0
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
70 def columnCount(self, parent):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
71 return 1
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
72 def data(self, index, role):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
73 if index.isValid():
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
74 ip = index.internalPointer()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
75 if role == Qt.DisplayRole:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
76 return str(ip)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
77 elif role == Qt.DecorationRole:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
78 if isinstance(ip, Interface):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
79 return self.usbPixmap
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
80 if isinstance(ip, Device):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
81 return self.chipPixmap
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
82
127
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
83 class StUtil(QMainWindow):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
84 connected = pyqtSignal(bool)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
85 def __init__(self):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
86 super().__init__()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
87 self.stl = None
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
88 def buildAction(name, callback, shortcut=None):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
89 a = QAction(name, self)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
90 a.triggered.connect(callback)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
91 if shortcut:
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
92 a.setShortcut(shortcut)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
93 return a
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
94 mb = self.menuBar()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
95 fileMenu = mb.addMenu("File")
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
96 self.connectAction = buildAction('Connect', self.connect)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
97 fileMenu.addAction(self.connectAction)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
98 self.disconnectAction = buildAction('Disconnect', self.disconnect)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
99 fileMenu.addAction(self.disconnectAction)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
100
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
101 self.miscMenu = mb.addMenu("Misc")
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
102 infoAction = buildAction('Info', self.info)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
103 self.miscMenu.addAction(infoAction)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
104
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
105 sb = self.statusBar()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
106
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
107 tv = QTreeView()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
108 self.setCentralWidget(tv)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
109 self.mdl = DeviceTreeModel()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
110 tv.setModel(self.mdl)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
111 tv.activated.connect(self.openItem)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
112
127
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
113 self.connected.connect(self.handleConnectedChange)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
114 self.connected.emit(False)
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
115 def openItem(self, idx):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
116 if idx.isValid():
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
117 ip = idx.internalPointer()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
118 if isinstance(ip, Interface):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
119 ip.open()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
120 # Try to get a device:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
121 self.mdl.addDevice(ip.createDevice())
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
122 if isinstance(ip, Device):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 127
diff changeset
123 self.device = ip
127
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
124 def handleConnectedChange(self, state):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
125 self.miscMenu.setEnabled(state)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
126 self.connectAction.setEnabled(not state)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
127 self.disconnectAction.setEnabled(state)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
128 msg = 'Connected!' if state else 'Disconnected!'
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
129 self.statusBar().showMessage(msg)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
130 def info(self):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
131 infoDialog = InformationDialog(self)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
132 infoDialog.exec()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
133 def connect(self):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
134 try:
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
135 self.stl = stlink.STLink()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
136 self.stl.open()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
137 except (stlink.STLinkException, usb.UsbError) as e:
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
138 QMessageBox.warning(self, "Error", str(e))
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
139 self.stl = None
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
140 if self.stl:
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
141 self.connected.emit(True)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
142 def disconnect(self):
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
143 if self.stl:
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
144 self.stl.close()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
145 self.connected.emit(False)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
146 self.stl = None
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
147
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
148 if __name__ == '__main__':
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
149 app = QApplication(sys.argv)
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
150 stu = StUtil()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
151 stu.show()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
152 app.exec()
ec1f2cc04d95 Added st util gui start
Windel Bouwman
parents:
diff changeset
153