-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_program_v3.py
315 lines (295 loc) · 14.7 KB
/
main_program_v3.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
### main_program_v3.py -- 12-02-2020 -- Andrew R Gross
### Libraries
import pygame
import time
import cv2
import RPi.GPIO as GPIO
import glob
import numpy as np
from ffpyplayer.player import MediaPlayer
import vlc
import io,sys,os,subprocess
#from tkFileDialog import askopenfilename
global process
#name= askopenfilename(filetypes=[("Video Files","*.h264")])
#subprocess.call(['vlc',name,'--play-and-exit'])
#from os import listdir
#from os.path import isfile, join
pygame.init()
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
screen.fill((255, 255, 255))
# Draw a solid blue circle in the center
pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
### Declare variables and assets
## Images
menucontrol = pygame.image.load('/home/pi/Rocket-dashboard/Images/menu-control.png')
menuradio = pygame.image.load('/home/pi/Rocket-dashboard/Images/menu-radio.png')
win_space = pygame.image.load('/home/pi/Rocket-dashboard/Images/win-deepspace.png')
win_mars = pygame.image.load('/home/pi/Rocket-dashboard/Images/win-mars.png')
win_orbit = pygame.image.load('/home/pi/Rocket-dashboard/Images/win-orbit.png')
win_vents = pygame.image.load('/home/pi/Rocket-dashboard/Images/win-vents.png')
win_hole = pygame.image.load('/home/pi/Rocket-dashboard/Images/win-observatory.png')
## Sounds
beep1 = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/beep1.wav')
beep2 = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/beep2.wav')
bip = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/bip.wav')
biz = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/biz.wav')
bloop = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/bloop.wav')
blorp = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/blorp.wav')
boop = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/boop.wav')
buzz = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/buzz.wav')
chirp = pygame.mixer.Sound('/home/pi/Rocket-dashboard/Sounds/chirp.wav')
## Videos
full_video_list = glob.glob('/home/pi/Rocket-dashboard/Videos/*')
video_list = full_video_list[0:4]
color_list = ((255,0,255), (255,0,127), (255,0,0), (255,127,0))
### Add a system for sorting just the most recent for videos
## Pins
button1 = 2
button2 = 3
button3 = 25
#button4
switch1 = 4
switch2 = 17
switch3 = 27
switch4 = 22
## LEDs
led1 = 9
led2 = 1
led3 = 1
led4 = 1
GPIO.setmode(GPIO.BCM)
#GPIO.setwarnings(False)
GPIO.setup(button1,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button2,GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(button3,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch1,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch2,GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(button4,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch3,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch4,GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(led1,GPIO.OUT)
screenstate = 0
switch1state = True
switch2state = True
switch3state = True
switch4state = True
light1color = (230,240,230)
light2color = (230,230,240)
light3color = (240,230,230)
switch4color = (130, 130, 130)
rollingvalue = 20
modifier = 1
window_list = (win_orbit, win_mars, win_space, win_hole, win_vents)
current_window = 1
#
#ffpyplayer for playing audio
def PlayVideo(video_path):
video=cv2.VideoCapture(video_path)
player = MediaPlayer(video_path)
while True:
grabbed, frame=video.read()
audio_frame, val = player.get_frame()
if not grabbed:
print("End of video")
break
if cv2.waitKey(20) & 0xFF == ord("q"):
break
cv2.namedWindow("Video")
cv2.moveWindow("Video", 0, 0)
cv2.imshow("Video", frame)
if val != 'eof' and audio_frame is not None:
img, t = audio_frame
video.release()
cv2.destroyAllWindows()
#PlayVideo(video_path)
print('Setup complete. Running test')
screen.blit(menucontrol, (0,0))
pygame.display.update()
try:
while True:
# time.sleep(0.1)
### Button1: Select
if GPIO.input(button1) == False:
beep1.play()
print('b1 pressed')
if GPIO.input(button2) == False:
print('button1-2 pressed')
pygame.quit()
quit()
elif screenstate == 0: # If on the control-menu screen:
print('ss = 0')
### Draw dashboard
screen.fill((255,255,250))
pygame.draw.circle(screen, (230,255,230), (130,370), 50)
pygame.draw.circle(screen, ( 0, 0, 0), (130,370), 51, 15) # Ring
pygame.draw.circle(screen, (255,230,210), (370,370), 50)
pygame.draw.circle(screen, ( 0, 0, 0), (370,370), 51, 15)
pygame.draw.circle(screen, (255,230,210), (610,370), 50)
pygame.draw.circle(screen, ( 0, 0, 0), (610,370), 51, 15)
pygame.draw.rect(screen, (100,100,100), (50,300,640,150), 26)
screen.blit(win_orbit,(30,7))
screenstate = 2
# pygame.display.update()
time.sleep(0.6)
elif screenstate == 1: # If on the radio menu screen:
screen.fill((10,10,10))
font = pygame.font.SysFont(None, 66)
font2 = pygame.font.SysFont(None, 50)
text1 = font.render(video_list[0].split('/')[-1], True, color_list[0])
text2 = font2.render(video_list[1].split('/')[-1], True, color_list[1])
text3 = font2.render(video_list[2].split('/')[-1], True, color_list[2])
text4 = font2.render(video_list[3].split('/')[-1], True, color_list[3])
screen.blit(text1, (30, 50))
screen.blit(text2, (60, 160))
screen.blit(text3, (60, 260))
screen.blit(text4, (60, 360))
pygame.draw.rect(screen, (230,230,200), (10,10,640,130), 9)
screenstate = 3
# pygame.display.update()
elif screenstate == 2:
screen.blit(menucontrol, (0,0))
screenstate = 0
elif screenstate == 3: # If on the radio select screen, play selected video
#chirp.play()
pygame.quit()
#video_file = video_list[0]
# PlayVideo(video_file)
print('Playing '+video_file)
#media_player = vlc.MediaPlayer()
#media_player = vlc.MediaPlayer()
#vid = vlc.Media(video_file)
#media_player.set_media(vid)
#media_player.play()
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screen.blit(menucontrol, (0,0))
screenstate = 0
# pygame.display.update()
pygame.display.update()
time.sleep(0.5)
### Button2: Cycle
if GPIO.input(button2) == False:
bloop.play()
print('b2 pressed')
if GPIO.input(button1) == False:
print('b2-1 pressed')
pygame.quit()
quit()
elif screenstate == 0: # If on control menu screen, switch to radio menu
screen.blit(menuradio, (0,0))
screenstate = 1
elif screenstate == 1: # If on radio menu scree, switch to control menu
screen.blit(menucontrol, (0,0))
screenstate = 0
elif screenstate == 2: # If on dashboard, cycle windows
current_window = current_window+1
if current_window == len(window_list):
current_window = 0
screen.blit(window_list[current_window], (30,7))
print(window_list[current_window])
elif screenstate == 3: # If on video select screen
### Rotate video order and color
video_list = (video_list[1], video_list[2], video_list[3], video_list[0])
color_list = (color_list[1], color_list[2], color_list[3], color_list[0])
screen.fill((10,10,10))
font = pygame.font.SysFont(None, 66)
font2 = pygame.font.SysFont(None, 50)
text1 = font.render(video_list[0].split('/')[-1], True, color_list[0])
text2 = font2.render(video_list[1].split('/')[-1], True, color_list[1])
text3 = font2.render(video_list[2].split('/')[-1], True, color_list[2])
text4 = font2.render(video_list[3].split('/')[-1], True, color_list[3])
screen.blit(text1, (30, 50))
screen.blit(text2, (60, 160))
screen.blit(text3, (60, 260))
screen.blit(text4, (60, 360))
pygame.draw.rect(screen, (230,230,200), (10,10,640,130), 9)
else:
pass
pygame.display.update()
time.sleep(0.5)
### Switch1: Light 1
if switch1state != GPIO.input(switch1):
beep2.play()
print('switch1 thrown')
# GPIO.output(led1, switch1state)
if switch1state == True:
light1color = (255,94,0) # Safety Orange
else:
light1color = (255,231,184) # Peach
if screenstate == 2:
pygame.draw.circle(screen, (light1color), (130,370), 40)
pygame.draw.circle(screen, (150,150,150), (130,370), 42, 6)
print("Updating because we're on screenstate2")
pygame.display.update()
switch1state = GPIO.input(switch1)
### Switch2: Light 2
if GPIO.input(switch2) != switch2state:
biz.play()
print('Switch2 thrown')
# GPIO.output(led1, switch1state)
if switch2state == True:
light2color = (68,0,255)
# pygame.draw.circle(screen, (208,255,230), (370,370), 40)
else:
light2color = (201,212,255)
# pygame.draw.circle(screen, (10, 255, 15), (370,370), 40)
# pygame.draw.circle(screen, (0, 255, 10), (370,370), 41, 6)
if screenstate == 2:
pygame.draw.circle(screen, light2color, (370,370), 40)
pygame.draw.circle(screen, (100,100,100), (370,370), 42, 6)
pygame.display.update()
switch2state = GPIO.input(switch2)
### Switch3
if GPIO.input(switch3) != switch3state:
boop.play()
print('Switch3 thrown')
if switch3state == True:
switch3color = (10,255,15)
# pygame.draw.circle(screen, (208,255,230), (610,370), 40)
else:
# pygame.draw.circle(screen, (10, 255, 15), (610,370), 40)
switch3color = (208, 255, 230)
pygame.draw.circle(screen, (switch3color), (610,370), 40)
pygame.draw.circle(screen, (0, 255, 10), (610,370), 41, 6)
if screenstate == 2:
pygame.display.update()
switch3state = GPIO.input(switch3)
### Switch4
if GPIO.input(switch4) != switch4state:
bip.play()
print('Switch4 thrown!')
if switch4state == True:
switch4color = (230, 0, 0)
# pygame.draw.rect(screen, (20,20,255), (50,300,650,150), 16)
else:
switch4color = (255,150,130)
# pygame.draw.rect(screen, (200,200,255), (50,300,650,150), 16)
if screenstate == 2:
print('Updating to new color')
pygame.draw.rect(screen, (switch4color), (50,300,640,150), 16)
pygame.display.update()
time.sleep(0.5)
switch4state = GPIO.input(switch4)
### Button3
# if GPIO.input(button3) == False:
# if rollingvalue == 254:
# modifier = -1
# if rollingvalue == 5:
# modifier = 1
# rollingvalue = rollingvalue + modifier
# button3color = (10,rollingvalue,0)
# pygame.draw.polygon(screen, button3color, [(600,150),(680,200),(680,100)])
# pygame.display.update()
# pygame.draw.rect(screen, (200,200,255), (100,300,600,150), 6)
# else:
# pass
# pass
# Import and initialize the pygame library
# Set up the drawing window
#pygame.quit()
except:
GPIO.cleanup()
print('exception called?')
raise
# quit()