-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights.py
243 lines (225 loc) · 7.37 KB
/
lights.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
# Lights for Build-a-Beast
# Author: Aaron Meyers
import time
from pprint import pprint
from neopixel import *
import sys
import requests
import colorsys
# LED strip configuration:
LED_COUNT = 105 # Number of LED pixels.
LED_PIN = 12 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 100 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
def player1Join(strip):
while True:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
if gameState["currentPhase"] != "GameJoining" or gameState["player2"]["joined"] == True:
return
for q in range(3):
for i in range(0, strip.numPixels() / 2, 3):
strip.setPixelColor(i+q, Color(160,160,160))
strip.show()
time.sleep(50/1000.0)
for i in range(0, strip.numPixels() / 2, 3):
strip.setPixelColor(i+q, 0)
def player2Join(strip):
while True:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
if gameState["currentPhase"] != "GameJoining" or gameState["player1"]["joined"] == True:
return
for q in range(3):
for i in range(strip.numPixels(), strip.numPixels() / 2, -3):
strip.setPixelColor(i-q, Color(160,160,160))
strip.show()
time.sleep(50/1000.0)
for i in range(strip.numPixels(), strip.numPixels() / 2, -3):
strip.setPixelColor(i-q, 0)
def player1And2Join(strip):
while True:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
if gameState["currentPhase"] != "GameJoining":
return
for q in range(3):
for i in range(0, strip.numPixels() / 2, 3):
strip.setPixelColor(i+q, Color(160,160,160))
for i in range(strip.numPixels(), strip.numPixels() / 2, -3):
strip.setPixelColor(i-q, Color(160,160,160))
strip.show()
time.sleep(50/1000.0)
for i in range(0, strip.numPixels() / 2, 3):
strip.setPixelColor(i+q, 0)
for i in range(strip.numPixels(), strip.numPixels() / 2, -3):
strip.setPixelColor(i-q, 0)
def player1Win(strip):
j = 0
k = 0
while True:
if k % 10 == 0:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
if gameState["currentPhase"] != "GameWinner":
return
for i in range(0, strip.numPixels() / 2, 1):
strip.setPixelColor(i, wheel((int((strip.numPixels() / 2 - i) * 256 / strip.numPixels() / 2) + j) & 255))
# strip.setPixelColor(i, Color(200,60,0))
strip.show()
j += 5
k += 1
time.sleep(25/1000.0)
def player2Win(strip):
j = 0
k = 0
while True:
if k % 10 == 0:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
if gameState["currentPhase"] != "GameWinner":
return
for i in range(strip.numPixels(), strip.numPixels() / 2 + 1, -1):
strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels() / 2) + j) & 255))
# strip.setPixelColor(i, Color(200,60,0))
strip.show()
j += 5
k += 1
time.sleep(25/1000.0)
def wheel(pos):
if pos < 85:
return Color(pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return Color(255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return Color(0, pos * 3, 255 - pos * 3)
def rainbowCycle(strip, wait_ms=1, iterations=5):
j = 0
while True:
if j % 50 == 0:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
if gameState["currentPhase"] != "GameBiomePicking":
return
for i in range(strip.numPixels()):
strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255))
strip.show()
j += 1
time.sleep(wait_ms/1000.0)
def timer(strip, start, current):
ratio = strip.numPixels() / (2.0 * start)
for i in range(strip.numPixels()):
if i > strip.numPixels() - (current * ratio):
strip.setPixelColor(i,Color(0,0,0))
elif i < current * ratio:
strip.setPixelColor(i,Color(0,0,0))
else:
otherRatio = current / start
hue = (1-otherRatio)*.33
(r, g, b) = colorsys.hsv_to_rgb(hue, 1, 1)
strip.setPixelColor(i,Color(int(r*255),int(g*255),int(b*255)))
strip.show()
def timeUp(strip):
while True:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
strip.show()
if gameState["currentPhase"] != "GameTimeUp":
return
for i in range(strip.numPixels()):
strip.setPixelColor(i,Color(255,0,0))
strip.show()
time.sleep(250/1000.0)
for i in range(strip.numPixels()):
strip.setPixelColor(i,0)
strip.show()
time.sleep(250/1000.0)
def sadReactsOnly(strip):
j = 0
while True:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
strip.show()
if gameState["currentPhase"] != "GameWinner":
return
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(0,0,max(255-(j*5),0)))
strip.show()
j += 1
time.sleep(100/1000.0)
def clear(strip):
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(0,0,0))
strip.show()
if __name__ == '__main__':
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
strip.begin()
clear(strip)
while True:
r = requests.get('http://barnyard-nuc.local/gamestate')
gameState = r.json()
P1 = gameState["player1"]
P2 = gameState["player2"]
LED_BRIGHTNESS = int(gameState["settings"]["brightness"])
strip.setBrightness(LED_BRIGHTNESS)
strip.show()
if gameState["currentPhase"] == "GameJoining":
clear(strip)
if P1["joined"] == True and P2["joined"] == True:
player1And2Join(strip)
elif P1["joined"] == True:
player1Join(strip)
elif P2["joined"] == True:
player2Join(strip)
elif gameState["currentPhase"] == "GameBiomePicking":
clear(strip)
rainbowCycle(strip,1)
elif gameState["currentPhase"] == "GameBiomeSelection":
if gameState["location"] == "Desert":
color = Color(255,0,0)
elif gameState["location"] == "Arctic":
color = Color(0,0,255)
elif gameState["location"] == "Grassland":
color = Color(255,255,0)
else:
# rainforest
color = Color(0,255,0)
for i in range(strip.numPixels()):
strip.setPixelColor(i,color)
strip.show()
elif gameState["currentPhase"] == "GameInProgress":
timer(strip,float(gameState["phaseTime"]),float(gameState["timeSincePhaseStart"]))
elif gameState["currentPhase"] == "GameTimeUp":
clear(strip)
timeUp(strip)
elif gameState["currentPhase"] == "GameWinner":
clear(strip)
if gameState["winner"] == "Player1":
player1Win(strip)
elif gameState["winner"] == "Player2":
player2Win(strip)
else:
sadReactsOnly(strip)
else:
clear(strip)
time.sleep(50.0/1000)