-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerConnection.py
More file actions
67 lines (48 loc) · 1.58 KB
/
ControllerConnection.py
File metadata and controls
67 lines (48 loc) · 1.58 KB
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
import serial
import glob
import time
import Config
from errors.InputError import InputError
# Serial connection
__ser = None
# Callback for when something changes with the players
__on_change = None
# Returns all usb-ports with connected devices
def __get_ports():
return glob.glob("/dev/ttyUSB*")
# Opens the port
def __open_port():
global __ser
# Gets all connected ports
ports = __get_ports()
# Ensures only one device got detected
if len(ports) != 1:
raise InputError("Es wurden " + str(len(ports)) + " gefunden. Bitte schließe mindestens bzw. nur ein Gerät an.")
# Opens the serial-connection
__ser = serial.Serial(ports[0], Config.ESP_BAUD)
# Inits the controller-input
def init(on_change):
global __on_change
__on_change = on_change
# Updates the controller-input
def update():
global __ser, __on_change
try:
# Ensures an open connection to the esp
if __ser is None:
__open_port()
# Waits until data got found
while __ser.inWaiting() >= 3:
# Gets next data
data = __ser.read(3)
# Performs checksum-check using XOR
if data[0] ^ data[1] != data[2]:
print("Error detected")
# Kills remaining bytes to prevent out-of-sync
__ser.read(__ser.inWaiting())
return
# Reads in the packet and executes the callback
__on_change(data[0] | (data[1] << 8))
except serial.serialutil.SerialException:
print("Serial-port error. Retrying...")
time.sleep(1)