1
- from PyQt5 .QtWidgets import QMainWindow
1
+ from collections import namedtuple
2
+ from PyQt5 .QtCore import QTimer
3
+ from PyQt5 .QtWidgets import QGridLayout , QHBoxLayout , QLineEdit , QMainWindow
2
4
from PyQt5 .QtWidgets import QDialog
3
5
from PyQt5 .QtWidgets import QWidget ,QGroupBox
4
6
from PyQt5 .QtWidgets import QMessageBox ,QVBoxLayout ,QCheckBox ,QButtonGroup ,QPushButton ,QLabel ,QSpinBox ,QComboBox
@@ -16,8 +18,10 @@ def __init__(self,name,id, main):
16
18
if (id == 0 ): # local buttons
17
19
self .dialog = (LocalButtonsConf (name ,self .main ))
18
20
elif (id == 1 ):
19
- self .dialog = (SPIButtonsConf (name ,self .main ))
21
+ self .dialog = (SPIButtonsConf (name ,self .main , 1 ))
20
22
elif (id == 2 ):
23
+ self .dialog = (SPIButtonsConf (name ,self .main ,2 ))
24
+ elif (id == 3 ):
21
25
self .dialog = (ShifterButtonsConf (name ,self .main ))
22
26
23
27
OptionsDialog .__init__ (self , self .dialog ,main )
@@ -84,9 +88,13 @@ def readValues(self):
84
88
85
89
class SPIButtonsConf (OptionsDialogGroupBox ):
86
90
87
- def __init__ (self ,name ,main ):
91
+ def __init__ (self ,name ,main , id ):
88
92
self .main = main
93
+ self .id = id
89
94
OptionsDialogGroupBox .__init__ (self ,name ,main )
95
+
96
+ def getPrefix (self ):
97
+ return f"spi{ self .id } _"
90
98
91
99
def initUI (self ):
92
100
vbox = QVBoxLayout ()
@@ -104,46 +112,170 @@ def initUI(self):
104
112
vbox .addWidget (self .polBox )
105
113
self .setLayout (vbox )
106
114
115
+ vbox .addWidget (QLabel ("CS #" ))
116
+ self .csBox = QSpinBox ()
117
+ self .csBox .setMinimum (1 )
118
+ self .csBox .setMaximum (3 )
119
+ vbox .addWidget (self .csBox )
120
+
107
121
def apply (self ):
108
- self .main .comms .serialWrite ("spibtn_mode=" + str (self .modeBox .currentData ()))
109
- self .main .comms .serialWrite ("spi_btnnum=" + str (self .numBtnBox .value ()))
110
- self .main .comms .serialWrite ("spi_btnpol=" + ("1" if self .polBox .isChecked () else "0" ))
122
+ self .main .comms .serialWrite (f"{ self .getPrefix ()} btn_mode=" + str (self .modeBox .currentData ()))
123
+ self .main .comms .serialWrite (f"{ self .getPrefix ()} btnnum=" + str (self .numBtnBox .value ()))
124
+ self .main .comms .serialWrite (f"{ self .getPrefix ()} btnpol=" + ("1" if self .polBox .isChecked () else "0" ))
125
+ self .main .comms .serialWrite (f"{ self .getPrefix ()} btn_cs={ self .csBox .value ()} " )
111
126
112
127
def readValues (self ):
113
- self .main .comms .serialGetAsync ("spi_btnnum ?" ,self .numBtnBox .setValue ,int )
128
+ self .main .comms .serialGetAsync (f" { self . getPrefix () } btnnum ?" ,self .numBtnBox .setValue ,int )
114
129
self .modeBox .clear ()
115
130
def modecb (mode ):
116
131
modes = mode .split ("\n " )
117
132
modes = [m .split (":" ) for m in modes if m ]
118
133
for m in modes :
119
134
self .modeBox .addItem (m [0 ],m [1 ])
120
- self .main .comms .serialGetAsync ("spibtn_mode?" ,self .modeBox .setCurrentIndex ,int )
121
- self .main .comms .serialGetAsync ("spibtn_mode!" ,modecb )
122
- self .main .comms .serialGetAsync ("spi_btnpol?" ,self .polBox .setChecked ,int )
135
+ self .main .comms .serialGetAsync (f"{ self .getPrefix ()} btn_mode?" ,self .modeBox .setCurrentIndex ,int )
136
+ self .main .comms .serialGetAsync (f"{ self .getPrefix ()} btn_mode!" ,modecb )
137
+ self .main .comms .serialGetAsync (f"{ self .getPrefix ()} btnpol?" ,self .polBox .setChecked ,int )
138
+ self .main .comms .serialGetAsync (f"{ self .getPrefix ()} btn_cs?" , self .csBox .setValue , int )
123
139
124
140
class ShifterButtonsConf (OptionsDialogGroupBox ):
141
+ class Mode (namedtuple ('Mode' , ['index' , 'name' , 'uses_spi' , 'uses_local_reverse' ])):
142
+ pass
125
143
126
144
def __init__ (self ,name ,main ):
127
145
self .main = main
128
146
OptionsDialogGroupBox .__init__ (self ,name ,main )
129
147
130
148
def initUI (self ):
149
+ def addThreshold (name ):
150
+ vbox .addWidget (QLabel (name ))
151
+ numBtnBox = QSpinBox ()
152
+ numBtnBox .setMinimum (0 )
153
+ numBtnBox .setMaximum (4096 )
154
+ vbox .addWidget (numBtnBox )
155
+ return numBtnBox
156
+
131
157
vbox = QVBoxLayout ()
132
158
vbox .addWidget (QLabel ("Mode" ))
133
159
self .modeBox = QComboBox ()
160
+ self .modeBox .currentIndexChanged .connect (self .modeBoxChanged )
134
161
vbox .addWidget (self .modeBox )
162
+
163
+ self .xPos = QLineEdit ()
164
+ self .xPos .setReadOnly (True )
165
+ self .yPos = QLineEdit ()
166
+ self .yPos .setReadOnly (True )
167
+ self .gear = QLineEdit ()
168
+ self .gear .setReadOnly (True )
169
+
170
+ posGroup = QGridLayout ()
171
+ posGroup .addWidget (QLabel ("X" ), 1 , 1 )
172
+ posGroup .addWidget (self .xPos , 1 , 2 )
173
+ posGroup .addWidget (QLabel ("Y" ), 1 , 3 )
174
+ posGroup .addWidget (self .yPos , 1 , 4 )
175
+ posGroup .addWidget (QLabel ("Calculated Gear" ), 2 , 1 , 1 , 2 )
176
+ posGroup .addWidget (self .gear , 2 , 3 , 1 , 2 )
177
+ posGroupBox = QGroupBox ()
178
+ posGroupBox .setTitle ("Current" )
179
+ posGroupBox .setLayout (posGroup )
180
+ vbox .addWidget (posGroupBox )
181
+
182
+ vbox .addWidget (QLabel ("X Channel" ))
183
+ self .xChannel = QSpinBox ()
184
+ self .xChannel .setMinimum (1 )
185
+ self .xChannel .setMaximum (6 )
186
+ vbox .addWidget (self .xChannel )
187
+
188
+ vbox .addWidget (QLabel ("Y Channel" ))
189
+ self .yChannel = QSpinBox ()
190
+ self .yChannel .setMinimum (1 )
191
+ self .yChannel .setMaximum (6 )
192
+ vbox .addWidget (self .yChannel )
193
+
194
+ self .x12 = addThreshold ("X 1,2 Threshold" )
195
+ self .x56 = addThreshold ("X 5,6 Threshold" )
196
+ self .y135 = addThreshold ("Y 1,3,5 Threshold" )
197
+ self .y246 = addThreshold ("Y 2,4,6 Threshold" )
198
+
199
+ self .revBtnLabel = QLabel ("Reverse Button Digital Input" )
200
+ vbox .addWidget (self .revBtnLabel )
201
+ self .revBtnBox = QSpinBox ()
202
+ self .revBtnBox .setMinimum (1 )
203
+ self .revBtnBox .setMaximum (8 )
204
+ vbox .addWidget (self .revBtnBox )
205
+
206
+ self .csPinLabel = QLabel ("SPI CS Pin Number" )
207
+ vbox .addWidget (self .csPinLabel )
208
+ self .csPinBox = QSpinBox ()
209
+ self .csPinBox .setMinimum (1 )
210
+ self .csPinBox .setMaximum (3 )
211
+ vbox .addWidget (self .csPinBox )
212
+
135
213
self .setLayout (vbox )
136
-
214
+
215
+ self .timer = QTimer ()
216
+ self .timer .timeout .connect (self .readXYPosition )
217
+
218
+ def onshown (self ):
219
+ self .timer .start (500 )
220
+
221
+ def onclose (self ):
222
+ self .timer .stop ()
223
+
224
+ def modeBoxChanged (self , _ ):
225
+ mode = self .modeBox .currentData ()
226
+
227
+ if mode is not None :
228
+ self .revBtnLabel .setVisible (mode .uses_local_reverse )
229
+ self .revBtnBox .setVisible (mode .uses_local_reverse )
230
+ self .csPinLabel .setVisible (mode .uses_spi )
231
+ self .csPinBox .setVisible (mode .uses_spi )
137
232
138
233
def apply (self ):
139
- self .main .comms .serialWrite ("shifter_mode=" + str (self .modeBox .currentData ()))
234
+ self .main .comms .serialWrite (f"shifter_mode={ self .modeBox .currentData ().index } " )
235
+ self .main .comms .serialWrite (f"shifter_x_chan={ self .xChannel .value ()} " )
236
+ self .main .comms .serialWrite (f"shifter_y_chan={ self .yChannel .value ()} " )
237
+ self .main .comms .serialWrite (f"shifter_x_12={ self .x12 .value ()} " )
238
+ self .main .comms .serialWrite (f"shifter_x_56={ self .x56 .value ()} " )
239
+ self .main .comms .serialWrite (f"shifter_y_135={ self .y135 .value ()} " )
240
+ self .main .comms .serialWrite (f"shifter_y_246={ self .y246 .value ()} " )
241
+ self .main .comms .serialWrite (f"shifter_rev_btn={ self .revBtnBox .value ()} " )
242
+ self .main .comms .serialWrite (f"shifter_cs_pin={ self .csPinBox .value ()} " )
243
+
244
+ def readXYPosition (self ):
245
+ def updatePosition (valueStr : str ):
246
+ x ,y = valueStr .strip ().split ("," )
247
+ self .xPos .setText (x )
248
+ self .yPos .setText (y )
249
+
250
+ def updateGear (value : str ):
251
+ value = value .strip ()
252
+ if value == "0" :
253
+ value = "N"
254
+ elif value == "7" :
255
+ value = "R"
256
+
257
+ self .gear .setText (value )
258
+
259
+ self .main .comms .serialGetAsync ("shifter_vals?" ,updatePosition , str )
260
+ self .main .comms .serialGetAsync ("shifter_gear?" , updateGear , str )
140
261
141
262
def readValues (self ):
142
263
self .modeBox .clear ()
143
264
def modecb (mode ):
144
265
modes = mode .split ("\n " )
145
266
modes = [m .split (":" ) for m in modes if m ]
146
267
for m in modes :
147
- self .modeBox .addItem (m [0 ],m [1 ])
268
+ index , uses_spi , uses_local_reverse = m [1 ].split (',' )
269
+ self .modeBox .addItem (m [0 ], ShifterButtonsConf .Mode (int (index ), m [0 ], uses_spi == "1" , uses_local_reverse == "1" ))
148
270
self .main .comms .serialGetAsync ("shifter_mode?" ,self .modeBox .setCurrentIndex ,int )
149
271
self .main .comms .serialGetAsync ("shifter_mode!" ,modecb )
272
+ self .main .comms .serialGetAsync ("shifter_x_chan?" ,self .xChannel .setValue ,int )
273
+ self .main .comms .serialGetAsync ("shifter_y_chan?" ,self .yChannel .setValue ,int )
274
+ self .main .comms .serialGetAsync ("shifter_x_12?" ,self .x12 .setValue ,int )
275
+ self .main .comms .serialGetAsync ("shifter_x_56?" ,self .x56 .setValue ,int )
276
+ self .main .comms .serialGetAsync ("shifter_y_135?" ,self .y135 .setValue ,int )
277
+ self .main .comms .serialGetAsync ("shifter_y_246?" ,self .y246 .setValue ,int )
278
+ self .main .comms .serialGetAsync ("shifter_rev_btn?" ,self .revBtnBox .setValue ,int )
279
+ self .main .comms .serialGetAsync ("shifter_cs_pin?" ,self .csPinBox .setValue , int )
280
+ self .readXYPosition ()
281
+
0 commit comments