forked from Ultrawipf/OpenFFBoard-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
245 lines (193 loc) · 7.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget,QGroupBox,QDialog,QVBoxLayout,QMessageBox
from PyQt5.QtCore import QIODevice,pyqtSignal
from PyQt5.QtCore import QTimer,QThread
from PyQt5 import uic
from PyQt5.QtSerialPort import QSerialPort,QSerialPortInfo
import sys,itertools
from helper import res_path
import serial_ui
# This GUIs version
version = "1.1.3"
# Minimal supported firmware version.
# Major version of firmware must match firmware. Minor versions must be higher or equal
min_fw = "1.1.4"
# UIs
import system_ui
import ffb_ui
import tmc4671_ui
import pwmdriver_ui
import serial_comms
import midi_ui
class MainUi(QMainWindow):
serial = None
save = pyqtSignal()
mainClassUi = None
timeouting = False
def __init__(self):
super(MainUi, self).__init__()
uic.loadUi(res_path('MainWindow.ui'), self)
#self.serialThread = QThread()
self.serial = QSerialPort()
#self.serial.moveToThread(self.serialThread)
self.comms = serial_comms.SerialComms(self,self.serial)
self.timer = QTimer(self)
self.timer.timeout.connect(self.updateTimer)
self.tabWidget_main.currentChanged.connect(self.tabChanged)
self.setup()
self.activeClasses = {}
self.fwverstr = None
def setup(self):
self.serialchooser = serial_ui.SerialChooser(serial=self.serial,main = self)
self.tabWidget_main.addTab(self.serialchooser,"Serial")
#self.serial.readyRead.connect(self.serialReceive)
self.serialchooser.getPorts()
self.actionAbout.triggered.connect(self.openAbout)
self.serialchooser.connected.connect(self.serialConnected)
self.timer.start(5000)
self.systemUi = system_ui.SystemUI(main = self)
self.serialchooser.connected.connect(self.systemUi.serialConnected)
self.systemUi.pushButton_save.clicked.connect(self.saveClicked)
self.setSaveBtn(False)
self.actionFFB_Wheel_TMC_wizard.triggered.connect(self.ffbwizard)
layout = QVBoxLayout()
layout.addWidget(self.systemUi)
self.groupBox_main.setLayout(layout)
def ffbwizard(self):
msg = QMessageBox(QMessageBox.Information,"Wizard","Not implemented")
msg.exec_()
def openAbout(self):
AboutDialog(self).exec_()
def updateTimer(self):
def f(i):
if i != self.systemUi.mainID:
self.resetPort()
self.log("Communication error. Please reconnect")
else:
self.timeouting = False
if(self.serial.isOpen()):
if self.timeouting:
self.timeouting = False
self.resetPort()
self.log("Timeout. Please reconnect")
return
else:
self.timeouting = True
self.comms.serialGetAsync("id?",f,int)
def log(self,s):
self.logBox.append(s)
def setSaveBtn(self,enabled):
self.systemUi.pushButton_save.setEnabled(enabled)
def saveClicked(self):
self.log("Save")
self.save.emit()
def tabChanged(self,id):
pass
def addTab(self,widget,name):
return self.tabWidget_main.addTab(widget,name)
def delTab(self,widget):
self.tabWidget_main.removeTab(self.tabWidget_main.indexOf(widget))
del widget
def selectTab(self,idx):
self.tabWidget_main.setCurrentIndex(idx)
def hasTab(self,name) -> bool:
names = [self.tabWidget_main.tabText(i) for i in range(self.tabWidget_main.count())]
return(name in names)
def resetTabs(self):
self.setSaveBtn(False)
self.activeClasses = {}
for i in range(self.tabWidget_main.count()-1,0,-1):
self.delTab(self.tabWidget_main.widget(i))
def updateTabs(self):
def updateTabs_cb(active):
lines = [l.split(":") for l in active.split("\n") if l]
newActiveClasses = {i[0]:{"id":i[1],"ui":None} for i in lines}
deleteClasses = [c for name,c in self.activeClasses.items() if name not in newActiveClasses]
#print(newActiveClasses)
for c in deleteClasses:
self.delTab(c)
for name,c in newActiveClasses.items():
if name in self.activeClasses:
continue
if name == "FFB Wheel":
self.mainClassUi = ffb_ui.FfbUI(main = self)
self.activeClasses[name] = self.mainClassUi
if name == "TMC4671":
c = tmc4671_ui.TMC4671Ui(main = self)
self.activeClasses[name] = c
self.addTab(c,name)
if name == "PWM":
c = pwmdriver_ui.PwmDriverUI(main = self)
self.activeClasses[name] = c
self.addTab(c,name)
if name == "MIDI":
c = midi_ui.MidiUI(main = self)
self.activeClasses[name] = c
self.addTab(c,name)
self.comms.serialGetAsync("lsactive",updateTabs_cb)
def reconnect(self):
self.resetPort()
QTimer.singleShot(4000,self.serialchooser.serialConnect)
#self.serialchooser.serialConnect()
def resetPort(self):
self.log("Reset port")
self.systemUi.setEnabled(False)
self.serial.waitForBytesWritten(500)
self.serial.close()
self.comms.reset()
self.timeouting = False
self.serialchooser.getPorts()
self.resetTabs()
def versionCheck(self,ver):
self.fwverstr = ver.replace("\n","")
if not self.fwverstr:
self.log("Communication error")
self.resetPort()
fwver = [int(i) for i in self.fwverstr.split(".")]
min_fw_t = [int(i) for i in min_fw.split(".")]
self.log("FW v" + self.fwverstr)
fwoutdated = False
guioutdated = fwver[0] > min_fw_t[0] or fwver[1] > min_fw_t[1]
for v in itertools.zip_longest(min_fw_t,fwver,fillvalue=0):
if(v[0] < v[1]): # Newer
break
# If same higher version then check minor version
if(v[0] > v[1]):
fwoutdated = True
break
if guioutdated:
msg = QMessageBox(QMessageBox.Information,"Incompatible GUI","The GUI you are using ("+ version +") may be too old for this firmware.\nPlease make sure both firmware and GUI are up to date.")
msg.exec_()
elif fwoutdated:
msg = QMessageBox(QMessageBox.Information,"Incompatible firmware","The firmware you are using ("+ self.fwverstr +") is too old for this GUI.\nPlease make sure both firmware and GUI are up to date.")
msg.exec_()
def serialConnected(self,connected):
if(connected):
if(self.comms.serialGet("id?;")):
self.log("Connected")
self.fwverstr = self.comms.serialGetAsync("swver",self.versionCheck)
else:
self.log("Can't detect board")
self.resetPort()
else:
self.log("Disconnected")
self.resetTabs()
class AboutDialog(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
uic.loadUi(res_path('about.ui'), self)
verstr = "Version: " + version
if parent.fwverstr:
verstr += " / Firmware: " + parent.fwverstr
self.version.setText(verstr)
if __name__ == '__main__':
#appctxt = ApplicationContext() # 1. Instantiate ApplicationContext
app = QApplication(sys.argv)
window = MainUi()
window.setWindowTitle("Open FFBoard Configurator")
window.show()
#exit_code = appctxt.app.exec_() # 2. Invoke appctxt.app.exec_()
#sys.exit(exit_code)
sys.exit(app.exec_())