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