-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
309 lines (251 loc) · 6.47 KB
/
main.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
from time import sleep
from pygame import mixer #for sound
import RPi.GPIO as GPIO
import board #for neopixels
import neopixel #for neopixels
########6 stones##########
#Space Stone - Blue
#Reality Stone - Red
#Power Stone - Purple
#Mind Stone - Yellow
#Time Stone - Green
#Soul Stone - Orange
#Sound Files
#background music
fMusic = './snd/av_theme.wav'
#sounds for each gate
fSpace = './snd/space.wav'
fReality = './snd/reality.wav'
fPower = './snd/power.wav'
fMind = './snd/mind.wav'
fTime = './snd/time.wav'
fSoul = './snd/soul.wav'
#end game sounds
fSnap = './snd/snap.wav'
fMsg = './snd/i-dont-feel-so-good-2.wav'
#boot sound
fReadySound = './snd/blaster-firing.wav'
#GPIO Pins for the gates and glove
#fake for now
gSpace = 9
gReality = 3
gPower = 4
gMind = 5
gTime = 6
gSoul = 7
gEnd = 8 #final gate, not a Stone)
gGlove = 13
#Colors
cSpace= (0,0,255)
cReality = (255,0,0)
cPower = (150,0,150)
cMind = (255,255,0)
cTime = (0,255,0)
cSoul = (255,80,0)
#Output neoPixel locations on the string, 0 for first
pSpace = 0
pReality = 1
pPower = 2
pMind = 3
pTime = 4
pSoul = 5
#Color Flashing
flashLoops = 3
flashOn = 0.5
flashOff = 0.2
#Glove Movement
gloveMin = 5
gloveMax = 12
gloveStep = 0.1
gloveSleep = 0.1
gloveTimeout = 5000
#Setup music and some sound effects, don't start yet
# mono instead of stereo
mixer.init(channels = 1)
mixer.music.load(fMusic)
sndSnap = mixer.Sound(fSnap)
sndMsg = mixer.Sound(fMsg)
sndReady = mixer.Sound(fReadySound)
#misc
tParkerDelay = 3
#####Even better stuff here
class Stone:
def __init__(self, name, soundfile, pin, rgb, tripped, nPixel, sndObj):
self.name = name #Stone Name
self.soundfile = soundfile #path of sound file, as string
self.pin = pin #GPIO pin for the gate associated with this stone
self.rgb = rgb #List of RGB values (0-255 each), such as (255,0,0) for Red
self.tripped = tripped #True/False, has the gate been tripped
self.nPixel = nPixel #Address of the NeoPixel associated with the stone
self.sndObj = sndObj #mixer.sound object
Stones = []
sSpace = Stone(
name = 'Space',
soundfile = fSpace,
pin = gSpace,
rgb = cSpace,
tripped = False,
nPixel = pSpace,
sndObj = mixer.Sound(fSpace) )
Stones.append(sSpace)
sReality = Stone(
name = 'Reality',
soundfile = fReality,
pin = gReality,
rgb = cReality,
tripped = False,
nPixel = pReality,
sndObj = mixer.Sound(fReality) )
Stones.append(sReality)
sPower = Stone(
name = 'Power',
soundfile = fPower,
pin = gPower,
rgb = cPower,
tripped = False,
nPixel = pPower,
sndObj = mixer.Sound(fPower) )
Stones.append(sPower)
sMind = Stone(
name = 'Mind',
soundfile = fMind,
pin = gMind,
rgb = cMind,
tripped = False,
nPixel = pMind,
sndObj = mixer.Sound(fMind) )
Stones.append(sMind)
sTime = Stone(
name = 'Time',
soundfile = fTime,
pin = gTime,
rgb = cTime,
tripped = False,
nPixel = pTime,
sndObj = mixer.Sound(fTime) )
Stones.append(sTime)
sSoul = Stone(
name = 'Soul',
soundfile = fSoul,
pin = gSoul,
rgb = cSoul,
tripped = False,
nPixel = pSoul,
sndObj = mixer.Sound(fSoul) )
Stones.append(sSoul)
for s in Stones:
print(s.name)
#Stub for logic
def findStoneByChannel(channel):
for s in Stones:
if s.pin == channel:
return s
def gate_passed(channel):
gatePassed = findStoneByChannel(channel)
if not mixer.music.get_busy():
mixer.music.play()
if not gatePassed.tripped: #this is the first time the gate has been passed
gatePassed.tripped = True
gatePassed.sndObj.play() #play a sound effect
flashStones(gatePassed.rgb) #flash all of the stones the color just passed
for s in Stones: #finish with only collected stones turned on
if not s.tripped:
pixels[s.nPixel] = (0,0,0)
else:
pixels[s.nPixel] = s.rgb
def flashStones(rgb):
for i in range(flashLoops):
pixels.fill(rgb)
sleep(flashOn)
pixels.fill((0,0,0))
sleep(flashOff)
def bAllGatesPassed(): #Returns True if there aren't any untriggered stones
val = True
for s in Stones:
if not s.tripped:
val = False
return val
def openGlove():
print('open Glove')
DC = gloveMin
glovePWM.start(DC)
glovePWM.ChangeDutyCycle(DC)
while DC < gloveMax:
glovePWM.ChangeDutyCycle(DC)
sleep(gloveSleep)
DC += gloveStep
def closeGlove():
print('close Glove')
DC = gloveMax
glovePWM.start(DC)
while DC > gloveMin:
glovePWM.ChangeDutyCycle(DC)
sleep(gloveSleep)
DC -= gloveStep
def finalGatePassed(): #channel is passed from GPIO, but not used
mixer.music.stop()
sndSnap.play()
pixels.fill((0,0,0))
sleep(tParkerDelay)
sndMsg.play()
for s in Stones:
s.tripped = False
closeGlove()
#Setup music and some sound effects, don't start yet
# mono instead of stereo
mixer.music.load(fMusic)
sndSnap = mixer.Sound(fSnap)
sndMsg = mixer.Sound(fMsg)
sndReady = mixer.Sound(fReadySound)
#Setup neoPixels
pixel_pin = board.D21
num_pixels = 6
ORDER = neopixel.RGB
BRIGHTNESS = 0.2 #Experiment with this...
pixels = neopixel.NeoPixel(pixel_pin, num_pixels)
#GPIO Config
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#Setup GPIO for glove
GPIO.setup(gGlove, GPIO.OUT)
glovePWM = GPIO.PWM(gGlove, 50)
glovePWM.start(gloveMin)
sleep(3)
glovePWM.stop()
#Setup GPIO for the stones, includting callback events for everything
for s in Stones:
GPIO.setup(s.pin, GPIO.IN, pull_up_down = GPIO.PUD_UP) #change PUD_UP to PUD_DOWN if no response?
GPIO.add_event_detect(s.pin, GPIO.FALLING, callback=gate_passed, bouncetime = 200)
#Setup GPIO for exit chute
GPIO.setup(gEnd, GPIO.IN, pull_up_down = GPIO.PUD_UP) #needs same pullup/pulldown value as the stone gates
#Play bootup sound
sndReady.play()
#testloop
while 1:
testInput = input()
if testInput == '1':
gate_passed(gSpace)
elif testInput == '2':
gate_passed(gReality)
elif testInput == '3':
gate_passed(gPower)
elif testInput == '4':
gate_passed(gMind)
elif testInput == '5':
gate_passed(gTime)
elif testInput == '6':
gate_passed(gSoul)
elif testInput == 'q':
quit()
if bAllGatesPassed():
openGlove()
GPIO.wait_for_edge(gEnd, GPIO.FALLING, timeout = gloveTimeout)
finalGatePassed()
sleep(1)
#do a pip3 install readchar
#import readchar
#c = readchar.readchar()
#key = readchar.readkey()
#key is really a keystroke, so it handles ctrl- and alt-sequences, things I probably don't need.
#https://pypi.org/project/readchar/
#but this make break ctrl-c, so look at https://github.com/magmax/python-readchar/issues/40