-
Notifications
You must be signed in to change notification settings - Fork 1
/
lasermaze.py
398 lines (332 loc) · 11.6 KB
/
lasermaze.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Code for the display for the Bloominglabs/Wonderlab Laser Tunnel
SDC 8/21/2014
"""
from subprocess import call
import RPi.GPIO as GPIO
import pygame
from pygame import *
import pygame.mixer
from credits import Credit
import sys,os
import time
import serial
text = """CREDITS
_ _
_Construction Team_\\Jay Sissom
\\Stephen Charlesworth
\\Naomi Charlesworth
\\Kristy Kallback-Rose
\\David Rose
\\Tomoko Oishi
_Arduino and Raspberry Pi Programming_\\Jay Sissom
\\Stephen Charlesworth
_Thanks to_\\Bob Fargas
\\Nathan Heald
\\Jordan Loewe
\\Jenett Tilottson
\\Paul Wolfe
_ _
©Copyright 2014 by Bloominglabs
http://bloominglabs.org
For Wonderlab"""
small_text = """
_Construction Team_
Jay Sissom
Stephen Charlesworth
Naomi Charlesworth
Kristy Kallback-Rose
David Rose
Tomoko Oishi
_Software_
Jay Sissom
Stephen Charlesworth
_Thanks to_
Bob Fargas
Nathan Heald
Jordan Loewe
Jenett Tilottson
Paul Wolfe
_ _
©Copyright 2014 by Bloominglabs
http://bloominglabs.org
For Wonderlab"""
#~ utiliser '\\' pour aligner les lignes de texte
# and now framebuffer biz
USE_FRAMEBUFFER = True # False
# Check which frame buffer drivers are available
# Start with fbcon since directfb hangs with composite output
drivers = ['fbcon', 'directfb', 'svgalib']
found = False
if USE_FRAMEBUFFER:
for driver in drivers:
print "trying driver %s" % driver
# Make sure that SDL_VIDEODRIVER is set
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print 'Driver: {0} failed.'.format(driver)
continue
found = True
break
if not found:
raise Exception('No suitable video driver found!')
# key constants
DISPLAY_WIDTH = 990
DISPLAY_HEIGHT = 500
DISPLAY_SURF = display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
FPS = 30
NUM_SENSORS = 6
SENSOR_STATE = [0] * NUM_SENSORS
# set to True if you want a billion print statements
DEBUG=False
#serial stuff. for debugging purposes also allows keyboard input
SERIAL_MODE = False
SERIAL_PORT = "/dev/ttyAMA0" #"/dev/tty.usbmodem1421" # change as appropriate
# Fonts
small_font = font.Font("./fonts/Roboto-MediumItalic.ttf",20)
medium_font = font.Font("./fonts/Roboto-MediumItalic.ttf",32)
big_font = font.Font("./fonts/Roboto-MediumItalic.ttf",64)
huge_font = font.Font("./fonts/Roboto-MediumItalic.ttf",80)
big_computer_font = font.Font("./fonts/Computer.ttf",100)
medium_computer_font = font.Font("./fonts/Computer.ttf",50)
big_LCD_font = font.Font("./fonts/LCD2 Bold.ttf",80)
small_computer_font = font.Font("./fonts/Computer.ttf",12)
# Images
wonderlab_img = pygame.image.load("./pix/wonderlab400x128.png")
blabs_img = pygame.image.load("./pix/blabsweirdfont990x180.png")
#sounds - pre init solved laggy sounds (mostly)
pygame.mixer.pre_init(44100,-16,2 ,512)
pygame.init()
pygame.mixer.music.set_volume(1)
pygame.mixer.set_num_channels(2) # music or fx
fx_sounds = pygame.mixer.Channel(0)
bg_sounds = pygame.mixer.Channel(1)
# don't add music unless you actually use it. But now u know what to do
pygame.mouse.set_visible(False)
click = pygame.mixer.Sound("./sounds/goodClick.ogg")
motion_detected = pygame.mixer.Sound("./sounds/motionDetected.ogg")
activating_alarm = pygame.mixer.Sound("./sounds/activatingAlarm.ogg")
#intruder_alert = pygame.mixer.Sound("./sounds/powerUp.ogg")
# Use this sound for alarm
intruder_alert = pygame.mixer.Sound("./sounds/battleStations.ogg")
#pygame.mixer.Sound("./sounds/IntruderAlert.ogg")
# GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.output(7,False)
# for panel. need to clean up tho
w_margin = 20
h_margin = 20
"""
show simple panel. Basically just: is alarm armed or not,
and are we safe or was ALERT TRIGGERED
show dots for our sensors also.
Green for not triggered, red for triggered
"""
def draw_panel(display_surf, armed, alert):
rect_width = (DISPLAY_WIDTH - 4*w_margin)/3
rect_height = (DISPLAY_HEIGHT - 2*h_margin)
ARMED_COLOR = (255,0,0)
DISARMED_COLOR = (0,255,0)
if armed:
armed_str = "ARMED"
armed_color = ARMED_COLOR
else:
armed_str = "DISARMED"
armed_color = DISARMED_COLOR
if alert:
alert_str = "ALERT"
alert_color = ARMED_COLOR
else:
alert_str = "SECURE"
alert_color = DISARMED_COLOR
display_surf.fill((30,30,30))
big_left_width = (DISPLAY_WIDTH*2/3 - 2*w_margin)
pygame.draw.rect(display_surf, armed_color,
(w_margin, h_margin, big_left_width,
DISPLAY_HEIGHT/2 - (3*h_margin/2)) ,0)
# pygame.draw.rect(display_surf, alert_color, (2*w_margin + rect_width, h_margin, rect_width, rect_height) ,0)
pygame.draw.rect(display_surf, (0,0,0),
(3*w_margin + 2*rect_width, h_margin, rect_width, rect_height) ,0)
text_surface_obj = medium_computer_font.render(armed_str,True,(0,0,0),armed_color)
text_rect_obj = text_surface_obj.get_rect()
text_rect_obj.center = (
w_margin + big_left_width/2 ,
DISPLAY_HEIGHT/3 - h_margin)
display_surf.blit(text_surface_obj,text_rect_obj)
# draw the friendly sensors
sensor_y = DISPLAY_HEIGHT * 3/4
sensor_width = big_left_width / (NUM_SENSORS + 1)
for (idx, sens_val) in enumerate(SENSOR_STATE):
if SENSOR_STATE[idx]:
color = ARMED_COLOR
else:
color = DISARMED_COLOR
pygame.draw.circle(display_surf, color, (w_margin + (1+idx) * sensor_width,sensor_y),
big_left_width/(NUM_SENSORS * 4), 0)
time_str = "%02d:%02d" % (GAME_TIME/60000, (GAME_TIME % 60000)/1000)
time_surface_obj = big_LCD_font.render(time_str,True,(255,0,0),(0,0,0))
time_rect_obj = time_surface_obj.get_rect()
time_rect_obj.center = (
display_surf.get_size()[0]/2 + rect_width + w_margin,
display_surf.get_size()[1]*3/4 )
display_surf.blit(time_surface_obj,time_rect_obj)
display_surf.blit(small_creds.scr, small_creds.surface_origin)
# if not small_creds.done:
small_creds.advance_credit()
# else:
# small_creds.reset()
def draw_splash(display_surf):
display_surf.fill((255,255,255))
display_surf.blit(blabs_img,(0,0))
display_surf.blit(wonderlab_img,(display_surf.get_size()[0]/2 -
wonderlab_img.get_size()[0]/2,240))
time_surface_obj = big_font.render('and',True,(0,0,0),(255,255,255))
text_rect_obj = time_surface_obj.get_rect()
text_rect_obj.center = (
display_surf.get_size()[0]/2 ,200)
display_surf.blit(time_surface_obj,text_rect_obj)
time_surface_obj = big_font.render('present...',True,(0,0,0),(255,255,255))
text_rect_obj = time_surface_obj.get_rect()
text_rect_obj.center = (
display_surf.get_size()[0]/2 ,
display_surf.get_size()[1] - text_rect_obj.h)
display_surf.blit(time_surface_obj,text_rect_obj)
pygame.display.update()
time.sleep(2)
display_surf.fill((0,0,0))
time_surface_obj = big_computer_font.render('Laser',True,(0,255,0),(0,0,0))
text_rect_obj = time_surface_obj.get_rect()
text_rect_obj.center = (
display_surf.get_size()[0]/2 ,
display_surf.get_size()[1]/3)
display_surf.blit(time_surface_obj,text_rect_obj)
time_surface_obj = big_computer_font.render('Tunnel',True,(0,255,0),(0,0,0))
text_rect_obj = time_surface_obj.get_rect()
text_rect_obj.center = (
display_surf.get_size()[0]/2 ,
display_surf.get_size()[1]*2/3)
display_surf.blit(time_surface_obj,text_rect_obj)
pygame.display.update()
time.sleep(2)
def get_serial_command(serial_port,stuff):
line = ""
ed = serial_port.read(1)
while ed:
if DEBUG:
print "serial read: [%s]" % ed
stuff = stuff + ed
idx = stuff.find("\n")
if idx > -1:
line = stuff[:idx]
if DEBUG:
print "got a line: [%s] of len [%s]" % (line,len(line))
if len(line):
line = line[0]
stuff = stuff[idx+1:]
break
ed = serial_port.read(1)
return (stuff, line)
FPS_CLOCK = pygame.time.Clock()
cred = Credit(text,small_font,(255,255,255),(0,0,0),(0,0),(DISPLAY_WIDTH,DISPLAY_HEIGHT))
# note there's kind of a dependency btw this and draw panel, should clean up
small_creds = Credit(small_text,small_computer_font,(255,255,255),(0,0,0),
(3*w_margin + 2*(DISPLAY_WIDTH - 4*w_margin)/3 ,h_margin),
((DISPLAY_WIDTH - 4*w_margin)/3,(DISPLAY_HEIGHT - 2*h_margin)/2))
pygame.display.set_caption('Laser Tunnel')
DISPLAY_SURF.fill((255,255,255))
draw_splash(DISPLAY_SURF)
DISPLAY_SURF.fill((0,0,0))
GAME_STATE = "ATTRACT"
# also ARMED DISARMED ALERT
GAME_TIME = 0
try:
serial_port = serial.Serial(SERIAL_PORT, 9600, timeout = 0)
SERIAL_MODE = True
if DEBUG:
print "Serial be live"
while serial_port.read(10):
pass
except:
pass
serial_stuff = ""
"""
commands
A 'arm' (red button)
D 'disarm' (green button)
1 - 8 (indicates a line was touched)
X - quit
"""
armed = False
alert = False
time_increment = 0
while True: # main game loop
current_key = ""
current_serial = ""
serial_command = ""
if SERIAL_MODE == True:
(serial_stuff, serial_command) = get_serial_command(serial_port,serial_stuff)
if serial_command:
print "Serial: [%s] %s" % (serial_command,len(serial_command))
current_key = serial_command
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYUP:
if DEBUG:
print "event key be %s" % event.key
if event.key < 128: # ignore shift etc.
current_key = str(unichr(event.key))
if DEBUG:
print "%s pressed" % current_key
if current_key.upper() == "A":
GPIO.output(7,False)
GAME_STATE = "ARMED"
armed = True
alert = False #???
GAME_TIME = 0 # actually depends. allow pause/reset
click.play()
activating_alarm.play()
intruder_alert.stop()
SENSOR_STATE = [0] * NUM_SENSORS
elif current_key.upper() == "D":
GPIO.output(7,False)
armed = False
alert = False #?
click.play() # ugh. need another sound.
intruder_alert.stop()
GAME_STATE = "DISARMED"
SENSOR_STATE = [0] * NUM_SENSORS
elif current_key and ((ord(current_key) - ord("1") < NUM_SENSORS and ord(current_key) >= ord("1"))):
if GAME_STATE == "ARMED":
GPIO.output(7,True)
SENSOR_STATE[ord(current_key) - ord("1")] = 1
if not fx_sounds.get_busy():
intruder_alert.play()
alert = True
elif current_key.upper() == "R":
pygame.quit()
if serial_command.upper() == "R":
call(["/sbin/shutdown","-h","now"])
sys.exit()
if GAME_STATE == "ATTRACT":
if not cred.done:
cred.advance_credit()
DISPLAY_SURF.blit(cred.scr, cred.surface_origin)
else:
GAME_STATE = "DISARMED"
# go through state-specific activities
if GAME_STATE == "ARMED":
if not alert:
GAME_TIME = GAME_TIME + time_increment
draw_panel(DISPLAY_SURF, armed, alert)
if GAME_STATE == "DISARMED":
draw_panel(DISPLAY_SURF, armed, alert)
time_increment = FPS_CLOCK.tick(FPS)
pygame.display.update()