-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFIO_Threads.py
525 lines (482 loc) · 18.1 KB
/
BFIO_Threads.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#====================================================================#
# File Information
#====================================================================#
"""
BFIO.py
=============
Summary
-------
This file contains a class that handles a thread meant to
constantly talk with the UART handled by BrSpand addons.
It is meant to constantly send hardware requests to GamePad
and read its hardware planes that are sent back as fast as
possible. It also contains the getter functions for binders.
"""
#====================================================================#
# Loading Logs
#====================================================================#
from Libraries.BRS_Python_Libraries.BRS.Hardware.UART.receiver import UART
from Libraries.BRS_Python_Libraries.BRS.Utilities.Information import Information
from Libraries.BRS_Python_Libraries.BRS.Debug.LoadingLog import LoadingLog
from Libraries.BRS_Python_Libraries.BRS.Utilities.bfio import BFIO, NewArrival, Passenger, Plane
LoadingLog.Start("BFIODriver.py")
#====================================================================#
# Imports
#====================================================================#
#region ------------------------------------------------------ Python
LoadingLog.Import("Python")
import time
import threading
import serial
#endregion
#region --------------------------------------------------------- BRS
LoadingLog.Import("Libraries")
from Libraries.BRS_Python_Libraries.BRS.Utilities.Enums import Execution, VarTypes
from Libraries.BRS_Python_Libraries.BRS.Debug.consoleLog import Debug
#endregion
#region -------------------------------------------------------- Kivy
# LoadingLog.Import("Kivy")
#endregion
#region ------------------------------------------------------ KivyMD
# LoadingLog.Import('KivyMD')
#endregion
#====================================================================#
# Variables
#====================================================================#
hardwareRequest = Plane(20, [], [])
# LX, LY LB RX RY RB S1 S2 S3 S4 S5
hardwareVarTypes = [VarTypes.Int, VarTypes.Int, VarTypes.Bool, VarTypes.Int, VarTypes.Int, VarTypes.Bool, VarTypes.Bool, VarTypes.Bool, VarTypes.Bool, VarTypes.Bool, VarTypes.Bool]
#====================================================================#
# Classes
#====================================================================#
class BFIODriver:
"""
BFIODriver:
===========
Summary:
--------
Backend driver that reads at fasts intervals the
UART threads handled by BrSpand ports drivers.
Its goal is to send planes to Gamepad as fast
as possible to get as many hardware readout in
the least amount of time in order to update
getter functions that are called by hardware
binders in Kontrol's Control menu.
"""
thread = None
stopEvent = threading.Event()
isStarted: bool = False
errorMessage:str = ""
_realLeftJoystickX = 0
_realLeftJoystickY = 0
_realRightJoystickX = 0
_realRightJoystickY = 0
_realLeftJoystickButton = False
_realRightJoystickButton = False
_realSwitch1 = False
_realSwitch2 = False
_realSwitch3 = False
_realSwitch4 = False
_realSwitch5 = False
extractedPlane:NewArrival = None
leftJoystickPositiveX:float = 0
""" The saved left joystick value for its X axis in the positive. (0-1) """
leftJoystickNegativeX:float = 0
""" The saved left joystick value for its X axis in the negative. (0-1) """
leftJoystickPositiveY:float = 0
""" The saved left joystick value for its Y axis in the positive. (0-1) """
leftJoystickNegativeY:float = 0
""" The saved left joystick value for its Y axis in the negative. (0-1) """
rightJoystickPositiveX:float = 0
""" The saved right joystick value for its X axis in the positive. (0-1) """
rightJoystickNegativeX:float = 0
""" The saved right joystick value for its X axis in the negative. (0-1) """
rightJoystickPositiveY:float = 0
""" The saved right joystick value for its Y axis in the positive. (0-1) """
rightJoystickNegativeY:float = 0
""" The saved right joystick value for its Y axis in the negative. (0-1) """
rightJoystickButton:bool = False
""" The saved button value for the right joystick. """
leftJoystickButton:bool = False
""" The saved button value for the left joystick. """
switch1:bool = False
""" The saved state of Gamepad's switch1. """
switch2:bool = False
""" The saved state of Gamepad's switch2. """
switch3:bool = False
""" The saved state of Gamepad's switch3. """
switch4:bool = False
""" The saved state of Gamepad's switch4. """
switch5:bool = False
""" The saved state of Gamepad's switch5. """
_lock = threading.Lock()
@staticmethod
def _handlingThread(uartClass, UART:UART):
from Libraries.BRS_Python_Libraries.BRS.Utilities.bfio import BFIO, NewArrival, PassengerTypes, MandatoryPlaneIDs
################################################
receivedAPlane:bool = False
uartError:bool = False
while True:
if uartClass.stopEvent.is_set():
break
hardwareRequest = Plane(20, [], [])
UART.QueuePlaneOnTaxiway(hardwareRequest)
###########################################
time.sleep(0.040) # Gamepad sends 37 bytes at 9600 bauds meaning 31ms of TX time
###########################################
receivedPlane:Passenger = UART.GetOldestReceivedGroupOfPassengers()
if(receivedPlane != None):
if(receivedPlane != Execution.Failed):
if(receivedPlane[0].value_8bits[1] == 20):
# This is an hardware readout! Youpii
receivedAPlane = True
receivedPlane = NewArrival(receivedPlane, hardwareVarTypes)
else:
uartError = True
###########################################
with uartClass._lock:
if(uartError == True):
uartClass.isStarted = False
uartClass.errorMessage = "UART class is not started."
if(receivedAPlane):
receivedAPlane = False
uartClass.extractedPlane = receivedPlane
uartClass._realLeftJoystickX = receivedPlane.GetParameter(0)
uartClass._realLeftJoystickY = receivedPlane.GetParameter(1)
uartClass._realLeftJoystickButton = receivedPlane.GetParameter(2)
uartClass._realRightJoystickX = receivedPlane.GetParameter(3)
uartClass._realRightJoystickY = receivedPlane.GetParameter(4)
uartClass._realRightJoystickButton = receivedPlane.GetParameter(5)
uartClass._realSwitch1 = receivedPlane.GetParameter(6)
uartClass._realSwitch2 = receivedPlane.GetParameter(7)
uartClass._realSwitch3 = receivedPlane.GetParameter(8)
uartClass._realSwitch4 = receivedPlane.GetParameter(9)
uartClass._realSwitch5 = receivedPlane.GetParameter(10)
pass
################################################
uartClass.isStarted = False
@staticmethod
def StartDriver():
"""
StartDriver:
============
Summary:
--------
Starts a thread that reads
all the informations of BFIODriver
pins at intervals of 3 seconds
Returns:
--------
"""
Debug.Start("BFIODriver -> StartDriver")
if(Information.platform != "Linux"):
Debug.Error(f"You cannot use this driver on your platform: {Information.platform}")
Debug.End()
return Execution.Incompatibility
if BFIODriver.isStarted == False:
BFIODriver.errorMessage = ""
if not BFIODriver.thread or not BFIODriver.thread.is_alive():
BFIODriver.stopEvent.clear()
BFIODriver.thread = threading.Thread(target=BFIODriver._handlingThread, args=(BFIODriver,UART,))
BFIODriver.thread.daemon = True
BFIODriver.thread.start()
BFIODriver.isStarted = True
Debug.End()
return Execution.Passed
else:
Debug.Error("Threads are already started. You cannot start more than 2.")
Debug.End()
return Execution.Unecessary
Debug.Log("BFIODriver is now started")
Debug.End()
return Execution.Passed
@staticmethod
def StopDriver():
"""
StopDriver:
============
Summary:
--------
Stops the thread that handles
the BFIO of GamePad.
"""
Debug.Start("BFIODriver -> StopDriver")
BFIODriver.stopEvent.set()
if BFIODriver.thread and BFIODriver.thread.is_alive():
BFIODriver.thread.join()
BFIODriver.isStarted = False
Debug.Log("thread is stopped.")
Debug.End()
return Execution.Passed
#region ----------------------------------- Getter - Left Joystick Axis
def Get_LeftJoystickXPositive() -> float:
"""
Get_LeftJoystickXPositive:
==========================
Summary:
--------
Returns the left joystick's
X axis in the positive. (0-1)
"""
if(BFIODriver.isStarted):
leftJoystickX = (BFIODriver._realLeftJoystickX / 2048)
if(leftJoystickX > 0):
BFIODriver.leftJoystickPositiveX = leftJoystickX
return leftJoystickX
else:
BFIODriver.leftJoystickPositiveX = 0
return 0
else:
return 0
def Get_LeftJoystickXNegative() -> float:
"""
Get_LeftJoystickXNegative:
==========================
Summary:
--------
Returns the left joystick's
X axis in the negative. (0-1)
"""
if(BFIODriver.isStarted):
leftJoystickX = (BFIODriver._realLeftJoystickX / 2048)
if(leftJoystickX < 0):
BFIODriver.leftJoystickNegativeX = leftJoystickX * -1
return BFIODriver.leftJoystickNegativeX
else:
BFIODriver.leftJoystickNegativeX = 0
return 0
else:
return 0
def Get_LeftJoystickYPositive() -> float:
"""
Get_LeftJoystickYPositive:
==========================
Summary:
--------
Returns the left joystick's
Y axis in the positive. (0-1)
"""
if(BFIODriver.isStarted):
leftJoystickY = (BFIODriver._realLeftJoystickY / 2048)
if(leftJoystickY > 0):
BFIODriver.leftJoystickPositiveY = leftJoystickY
return leftJoystickY
else:
BFIODriver.leftJoystickPositiveY = 0
return 0
else:
return 0
def Get_LeftJoystickYNegative() -> float:
"""
Get_LeftJoystickYNegative:
==========================
Summary:
--------
Returns the left joystick's
Y axis in the negative. (0-1)
"""
if(BFIODriver.isStarted):
leftJoystickY = (BFIODriver._realLeftJoystickY / 2048)
if(leftJoystickY < 0):
BFIODriver.leftJoystickNegativeY = leftJoystickY * -1
return BFIODriver.leftJoystickNegativeY
else:
BFIODriver.leftJoystickNegativeY = 0
return 0
else:
return 0
#endregion
#region ----------------------------------- Getter - Right Joystick Axis
def Get_RightJoystickXPositive() -> float:
"""
Get_RightJoystickXPositive:
==========================
Summary:
--------
Returns the right joystick's
X axis in the positive. (0-1)
"""
if(BFIODriver.isStarted):
rightJoystickX = (BFIODriver._realRightJoystickX / 2048)
if(rightJoystickX > 0):
BFIODriver.rightJoystickPositiveX = rightJoystickX
return rightJoystickX
else:
BFIODriver.rightJoystickPositiveX = 0
return 0
else:
return 0
def Get_RightJoystickXNegative() -> float:
"""
Get_RightJoystickXNegative:
==========================
Summary:
--------
Returns the right joystick's
X axis in the negative. (0-1)
"""
if(BFIODriver.isStarted):
rightJoystickX = (BFIODriver._realRightJoystickX / 2048)
if(rightJoystickX < 0):
BFIODriver.rightJoystickNegativeX = rightJoystickX * -1
return BFIODriver.rightJoystickNegativeX
else:
BFIODriver.rightJoystickNegativeX = 0
return 0
else:
return 0
def Get_RightJoystickYPositive() -> float:
"""
Get_RightJoystickYPositive:
==========================
Summary:
--------
Returns the right joystick's
Y axis in the positive. (0-1)
"""
if(BFIODriver.isStarted):
rightJoystickY = (BFIODriver._realRightJoystickY / 2048)
if(rightJoystickY > 0):
BFIODriver.rightJoystickPositiveY = rightJoystickY
return rightJoystickY
else:
BFIODriver.rightJoystickPositiveY = 0
return 0
else:
return 0
def Get_RightJoystickYNegative() -> float:
"""
Get_RightJoystickYNegative:
==========================
Summary:
--------
Returns the right joystick's
Y axis in the negative. (0-1)
"""
if(BFIODriver.isStarted):
rightJoystickY = (BFIODriver._realRightJoystickY / 2048)
if(rightJoystickY < 0):
BFIODriver.rightJoystickNegativeY = rightJoystickY * -1
return BFIODriver.rightJoystickNegativeY
else:
BFIODriver.rightJoystickNegativeY = 0
return 0
else:
return 0
#endregion
#region ----------------------------------- Getter - Buttons
def Get_LeftJoystickButton() -> bool:
"""
Get_LeftJoystickButton:
==========================
Summary:
--------
Returns the left joystick's
button as a boolean. Will
always return False if this
class is not started.
"""
if(BFIODriver.isStarted):
BFIODriver.leftJoystickButton = BFIODriver._realLeftJoystickButton
return BFIODriver.leftJoystickButton
else:
return False
def Get_RightJoystickButton() -> bool:
"""
Get_RightJoystickButton:
==========================
Summary:
--------
Returns the right joystick's
button as a boolean. Will
always return False if this
class is not started.
"""
if(BFIODriver.isStarted):
BFIODriver.rightJoystickButton = BFIODriver._realRightJoystickButton
return BFIODriver.rightJoystickButton
else:
return False
def Get_Switch1() -> bool:
"""
Get_Switch1:
============
Summary:
--------
Returns switch 1's value
as a boolean. Will
always return False if this
class is not started.
"""
if(BFIODriver.isStarted):
BFIODriver.switch1 = BFIODriver._realSwitch1
return BFIODriver.switch1
else:
return False
def Get_Switch2() -> bool:
"""
Get_Switch2:
============
Summary:
--------
Returns switch 2's value
as a boolean. Will
always return False if this
class is not started.
"""
if(BFIODriver.isStarted):
BFIODriver.switch2 = BFIODriver._realSwitch2
return BFIODriver.switch2
else:
return False
def Get_Switch3() -> bool:
"""
Get_Switch3:
============
Summary:
--------
Returns switch 3's value
as a boolean. Will
always return False if this
class is not started.
"""
if(BFIODriver.isStarted):
BFIODriver.switch3 = BFIODriver._realSwitch3
return BFIODriver.switch3
else:
return False
def Get_Switch4() -> bool:
"""
Get_Switch4:
============
Summary:
--------
Returns switch 4's value
as a boolean. Will
always return False if this
class is not started.
"""
if(BFIODriver.isStarted):
BFIODriver.switch4 = BFIODriver._realSwitch4
return BFIODriver.switch4
else:
return False
def Get_Switch5() -> bool:
"""
Get_Switch5:
============
Summary:
--------
Returns switch 5's value
as a boolean. Will
always return False if this
class is not started.
"""
if(BFIODriver.isStarted):
BFIODriver.switch5 = BFIODriver._realSwitch5
return BFIODriver.switch5
else:
return False
#endregion
#====================================================================#
LoadingLog.End("driver.py")