127
|
1 #!/usr/bin/python
|
|
2
|
|
3 import sys
|
|
4 from PyQt4.QtCore import *
|
|
5 from PyQt4.QtGui import *
|
129
|
6 import stlink, devices, stm32
|
|
7 from devices import Interface, Device
|
127
|
8
|
|
9 class InformationDialog(QDialog):
|
|
10 def __init__(self, parent):
|
|
11 super().__init__(parent)
|
|
12 self.setWindowTitle('Info')
|
|
13 fl = QFormLayout(self)
|
|
14 if parent.stl:
|
|
15 fl.addRow('ST link version:', QLabel(parent.stl.Version))
|
|
16 fl.addRow('Chip id:', QLabel('0x{0:X}'.format(parent.stl.ChipId)))
|
|
17 fl.addRow('Current mode:', QLabel(parent.stl.CurrentModeString))
|
|
18 fl.addRow('Status:', QLabel(parent.stl.StatusString))
|
|
19
|
131
|
20 class RegisterModel(QAbstractTableModel):
|
|
21 def __init__(self):
|
|
22 super().__init__()
|
|
23 self.regCount = 15
|
|
24 self.device = None
|
|
25 def rowCount(self, parent):
|
|
26 if parent.isValid():
|
|
27 return 0
|
|
28 if self.device:
|
|
29 return 21 # TODO make variable
|
|
30 else:
|
|
31 return 0
|
|
32 def setDevice(self, dev):
|
|
33 self.device = dev
|
|
34 self.modelReset.emit()
|
|
35 def columnCount(self, parent):
|
|
36 if parent.isValid():
|
|
37 return 0
|
|
38 return 2
|
|
39 def data(self, index, role):
|
|
40 if index.isValid():
|
|
41 row, col = index.row(), index.column()
|
|
42 if role == Qt.DisplayRole:
|
|
43 if col == 0:
|
|
44 return 'R{0}'.format(row)
|
|
45 elif col == 1:
|
|
46 v = self.device.iface.read_reg(row)
|
|
47 return '0x{0:X}'.format(v)
|
|
48
|
|
49 class RegisterView(QTableView):
|
|
50 def __init__(self):
|
|
51 super().__init__()
|
|
52 self.mdl = RegisterModel()
|
|
53 self.setModel(self.mdl)
|
|
54
|
129
|
55 class DeviceTreeModel(QAbstractItemModel):
|
|
56 def __init__(self):
|
|
57 super().__init__()
|
|
58 self.chipPixmap = QPixmap('chip.png').scaled(32, 32)
|
130
|
59 self.hardwarePixmap = QPixmap('hardware.png').scaled(32, 32)
|
129
|
60 self.refresh()
|
|
61 def refresh(self):
|
|
62 """ Check all usb interfaces for interfaces """
|
|
63 self.interfaces = devices.createInterfaces()
|
|
64 self.devices = []
|
|
65 self.modelReset.emit()
|
|
66 def addDevice(self, device):
|
|
67 if device.iface in [lambda d: d.iface for d in self.devices]:
|
|
68 print('device already open')
|
|
69 return
|
|
70 self.devices.append(device)
|
|
71 self.modelReset.emit()
|
|
72 def index(self, row, column, parent):
|
|
73 if parent.isValid():
|
|
74 ip = parent.internalPointer()
|
|
75 if isinstance(ip, Interface):
|
|
76 devs = [d for d in self.devices if d.iface is ip]
|
|
77 return self.createIndex(row, column, devs[row])
|
|
78 else:
|
|
79 iface = self.interfaces[row]
|
|
80 return self.createIndex(row, column, iface)
|
|
81 return idx
|
|
82 def parent(self, index):
|
|
83 if index.isValid():
|
|
84 ip = index.internalPointer()
|
|
85 if isinstance(ip, Interface):
|
|
86 return QModelIndex()
|
|
87 elif isinstance(ip, Device):
|
|
88 iface = ip.iface
|
|
89 row = self.interfaces.index(iface)
|
|
90 return self.createIndex(row, 0, iface)
|
|
91 return QModelIndex()
|
|
92 def rowCount(self, parent):
|
|
93 if parent.isValid():
|
|
94 # non-root level:
|
|
95 ip = parent.internalPointer()
|
|
96 if isinstance(ip, Interface):
|
|
97 devs = [d for d in self.devices if d.iface is ip]
|
|
98 return len(devs)
|
|
99 else:
|
|
100 # root level:
|
|
101 return len(self.interfaces)
|
|
102 return 0
|
|
103 def columnCount(self, parent):
|
|
104 return 1
|
|
105 def data(self, index, role):
|
|
106 if index.isValid():
|
|
107 ip = index.internalPointer()
|
|
108 if role == Qt.DisplayRole:
|
|
109 return str(ip)
|
|
110 elif role == Qt.DecorationRole:
|
|
111 if isinstance(ip, Interface):
|
130
|
112 return self.hardwarePixmap
|
129
|
113 if isinstance(ip, Device):
|
|
114 return self.chipPixmap
|
|
115
|
130
|
116 class DeviceExplorer(QTreeView):
|
|
117 """ Lists all interfaces plugged in and allows selection """
|
|
118 deviceSelected = pyqtSignal(Device)
|
|
119 def __init__(self):
|
|
120 super().__init__()
|
|
121 self.mdl = DeviceTreeModel()
|
|
122 self.setModel(self.mdl)
|
|
123 self.activated.connect(self.openItem)
|
|
124 self.header().close()
|
|
125 self.customContextMenuRequested.connect(self.openMenu)
|
|
126 self.setContextMenuPolicy(Qt.CustomContextMenu)
|
|
127 def openItem(self, idx):
|
|
128 if idx.isValid():
|
|
129 ip = idx.internalPointer()
|
|
130 if isinstance(ip, Interface):
|
|
131 if not ip.IsOpen:
|
|
132 ip.open()
|
|
133 # Try to get a device:
|
|
134 self.mdl.addDevice(ip.createDevice())
|
|
135 if isinstance(ip, Device):
|
|
136 self.deviceSelected.emit(ip)
|
|
137 def openMenu(self, pt):
|
|
138 idx = self.indexAt(pt)
|
|
139 menu = QMenu()
|
|
140 menu.addAction('Refresh', self.mdl.refresh)
|
|
141 if idx.isValid():
|
|
142 ip = idx.internalPointer()
|
|
143 if isinstance(ip, Interface):
|
|
144 if ip.IsOpen:
|
|
145 def closeInterface():
|
|
146 self.mdl.closeInterface(ip)
|
|
147 menu.addAction('Close', closeInterface)
|
|
148 else:
|
|
149 def openInterface():
|
|
150 ip.open()
|
|
151 # Try to get a device:
|
|
152 self.mdl.addDevice(ip.createDevice())
|
|
153 menu.addAction('Open', openInterface)
|
|
154 elif isinstance(ip, Device):
|
|
155 def selectDevice():
|
|
156 self.deviceSelected.emit(ip)
|
|
157 menu.addAction('Select', selectDevice)
|
|
158 menu.exec(self.mapToGlobal(pt))
|
|
159
|
127
|
160 class StUtil(QMainWindow):
|
|
161 connected = pyqtSignal(bool)
|
|
162 def __init__(self):
|
|
163 super().__init__()
|
|
164 self.stl = None
|
|
165 def buildAction(name, callback, shortcut=None):
|
|
166 a = QAction(name, self)
|
|
167 a.triggered.connect(callback)
|
|
168 if shortcut:
|
|
169 a.setShortcut(shortcut)
|
|
170 return a
|
|
171 mb = self.menuBar()
|
|
172 fileMenu = mb.addMenu("File")
|
|
173 self.connectAction = buildAction('Connect', self.connect)
|
|
174 fileMenu.addAction(self.connectAction)
|
|
175 self.disconnectAction = buildAction('Disconnect', self.disconnect)
|
|
176 fileMenu.addAction(self.disconnectAction)
|
|
177
|
|
178 self.miscMenu = mb.addMenu("Misc")
|
|
179 infoAction = buildAction('Info', self.info)
|
|
180 self.miscMenu.addAction(infoAction)
|
|
181
|
|
182 sb = self.statusBar()
|
|
183
|
130
|
184 devexplr = DeviceExplorer()
|
|
185 self.setCentralWidget(devexplr)
|
|
186 devexplr.deviceSelected.connect(self.handleDeviceSelected)
|
129
|
187
|
127
|
188 self.connected.connect(self.handleConnectedChange)
|
|
189 self.connected.emit(False)
|
130
|
190 def handleDeviceSelected(self, dev):
|
|
191 self.dev = dev
|
|
192 self.handleConnectedChange(True)
|
127
|
193 def handleConnectedChange(self, state):
|
|
194 self.miscMenu.setEnabled(state)
|
|
195 self.connectAction.setEnabled(not state)
|
|
196 self.disconnectAction.setEnabled(state)
|
|
197 msg = 'Connected!' if state else 'Disconnected!'
|
|
198 self.statusBar().showMessage(msg)
|
|
199 def info(self):
|
|
200 infoDialog = InformationDialog(self)
|
|
201 infoDialog.exec()
|
|
202 def connect(self):
|
|
203 try:
|
|
204 self.stl = stlink.STLink()
|
|
205 self.stl.open()
|
|
206 except (stlink.STLinkException, usb.UsbError) as e:
|
|
207 QMessageBox.warning(self, "Error", str(e))
|
|
208 self.stl = None
|
|
209 if self.stl:
|
|
210 self.connected.emit(True)
|
|
211 def disconnect(self):
|
|
212 if self.stl:
|
|
213 self.stl.close()
|
|
214 self.connected.emit(False)
|
|
215 self.stl = None
|
|
216
|
|
217 if __name__ == '__main__':
|
|
218 app = QApplication(sys.argv)
|
|
219 stu = StUtil()
|
|
220 stu.show()
|
|
221 app.exec()
|
|
222
|