-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghoust_player.py
143 lines (112 loc) · 4.54 KB
/
ghoust_player.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
#!/usr/bin/env python3
class Player:
def __init__(self, pid, mqtt_client, name="", game=None):
self.pid = pid
self.client = mqtt_client
self.team = 0
self.status = "SELECT_GAME"
self.str = "GHOUST/clients/{0}".format(self.pid)
self.game = game
self.select_game(0)
self.go_color = [0, 1023, 0]
self.led_timer = None
# can be used to store game specific parameters in the player object
self.game_params = dict()
def __repr__(self):
return str(self.pid)
def setname(self, name):
self.name = name
def setteam(self, n, color = None):
print(self.pid + ": team " + str(n))
self.team = n
if color != None:
self.go_color = color
def warn(self):
print(self.pid + ": warn")
self._config("led", val=[1023, 1023, 0], duration_led=500)
def out(self):
print(self.pid + ": out")
self.status = "INACTIVE"
# reset go color to green
self.go_color = [0, 1023, 0]
self._config("led", val=[1023, 0, 0])
self._config("motor", val=[1023, 3000])
self._config("buzzer", preset=3)
def timeout(self):
print(self.pid + ": timeout")
self.status = "INACTIVE"
self._config("led", val=[1023, 1023, 0], duration_led=1000)
self._config("motor", preset=1)
def abort(self):
print(self.pid + ": abort")
self.status = "INACTIVE"
self._config("led", val=[1023, 1023, 0], duration_led=1000)
self._config("motor", preset=1)
def join(self):
print(self.pid + ": join game ", self.game.game_number)
self.status = "ACTIVE"
self._config("motor", preset=1)
self._config("led", val=[0,0,0])
def leave(self):
print(self.pid + ": leave ", self.game.game_number)
self.status = "INACTIVE"
self._config("motor", preset=1)
self._config("led", val = [0,0,0])
def start(self):
print(self.pid + ": start")
self.status = "GO"
self._config("led", val=self.go_color)
self._config("motor", val=[1023, 750])
self._config("buzzer", preset=2)
def win(self):
print(self.pid + ": win")
self._config("motor", preset=3)
self._config("led", preset=1)
self._config("buzzer", preset=1)
def select_game(self, n):
print(self.pid + ": select game " + str(n))
self.select_game_n = n
# TODO flash gamenumber periodically
def set_accel_thresh(self, out, warn):
print(self.pid + ": set acceleration threshold")
self.client.publish(self.str + "/config/accel_out", str(out))
self.client.publish(self.str + "/config/accel_warn", str(warn))
def set_game(self, game_p):
if self.game == game_p:
return
if self.game != None:
self.game._leave(self.pid, self)
self.game = game_p
self.game_params = dict()
if game_p != None:
self.status = "INACTIVE"
self.game._join(self.pid, self)
else:
self.status = "SELECT_GAME"
############# raw functions for low level access ##############
# buzzer, vibro val: [0-1023, 0-], [frequency (hz), duration (ms)]
# led val: [0-1023, 0-1023, 0-1023, 0-inf], [r, g, b, duration (ms)]
# parameter: ["motor", "buzzer", "led"]
# duration_led: ms, only used for value input
def _config(self, parameter, val=None, preset=None, duration_led=None):
if parameter not in ["motor", "buzzer", "led"]:
print("parameter not valid")
return
topic = self.str + "/config/{}".format(parameter)
if preset != None:
if not (0 <= int(preset) <= 9):
print("vibrate preset not in range")
self.client.publish(topic, "PRESET:{}".format(preset))
if val != None:
if (not (0 <= val[0] <= 1023) or
(parameter != 'led' and not(0 <= val[1])) or
(parameter == 'led' and not(0 <= val[1] <= 1023)) or
(parameter == 'led' and not(0 <= val[2] <= 1023))):
print("config values not in range")
fstring = "RAW:{:04},{:04}"
if parameter == "led":
fstring = "RAW:{:04},{:04},{:04}"
if duration_led != None:
fstring = "RAW:{:04},{:04},{:04},{:04}"
val.append(duration_led)
self.client.publish(topic, fstring.format(*val))