-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
190 lines (162 loc) · 5.29 KB
/
start.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
import GUI
GUI.loadingScreen()
import connections
from connections import Connection
from downloadImages import connectAndDownload
import time
import RPi.GPIO as GPIO
import pygame
from pygame.locals import USEREVENT
import threading
import sequenceGen
import lib
import gallery
status = False
pingAccuracy = 2
cons = []
cons.append(Connection('http://slave1.local', ':8080/'))
# cons.append(Connection('http://master.local', ':8080/'))
cons.append(Connection('http://master2.local', ':8080/'))
# cons.append(Connection('http://slave2.local', ':8080/'))
# cons.append(Connection('http://slave3.local', ':8080/'))
connectionThread = threading.Thread(target=connections.updateConnections, args=[cons, 5]) # Update connections in other thread.
connectionThread.start()
# Pin Definitons:
ledPin = 21
butPin = 20 # Broadcom pin 17 (P1 pin 11)
shutDownPin = 3
mainButton = 5
scrollRight = 13
scrollLeft = 6
# Pin Setup:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(ledPin, GPIO.OUT) # LED pin set as output
GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
GPIO.setup(scrollRight, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
GPIO.setup(scrollLeft, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
GPIO.setup(mainButton, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
GPIO.setup(shutDownPin, GPIO.IN)
GPIO.output(ledPin, GPIO.HIGH)
GUI.loadingScreen()
targetFps = 30
zigzag = sequenceGen.zigZag(len(cons))
pygameImages = []
pygame.time.set_timer(USEREVENT+1, int(1000/targetFps))
class State:
DEFAULT = 0
LOADING = 1
CAPTURE = 2
SHUT_DOWN = 3
SHOW_IMAGE = 4
GALLERY = 5
clock = pygame.time.Clock()
STATE = State()
program_state = 0
def mainLoop(pygameImages):
try:
while 1:
##########################
# Toggle Block:
##########################
# elif(GPIO.input(scrollRight) == GPIO.LOW):
# print("test")
##########################
# Control block:
##########################
global program_state
if (GPIO.input(butPin) == GPIO.LOW): # Shutter button is pressed
program_state = STATE.CAPTURE
print('test1')
elif(len(pygameImages) > 0): # if any images in buffer,
program_state = STATE.SHOW_IMAGE
print('test2')
elif(GPIO.input(shutDownPin) == GPIO.LOW):
program_state = STATE.SHUT_DOWN
# time.sleep(0.01) # sleep for ~ delta 60 fps
elif(GPIO.input(mainButton) == GPIO.LOW): #If gallery button is pressed
print(program_state)
if(program_state == STATE.GALLERY): # if we are
program_state = STATE.DEFAULT
else:
program_state = STATE.GALLERY
gallery.setup()
lib.waitForPullUp(mainButton)
elif(program_state != STATE.GALLERY):
program_state = STATE.DEFAULT
time.sleep(0.01) # sleep for ~ delta 60 fps
clock.tick(20)
##########################
# Exacution block:
##########################
if(program_state == STATE.DEFAULT):
for event in pygame.event.get():
if event.type == USEREVENT+1:
GUI.defaultScreen(cons)
elif(program_state == STATE.CAPTURE):
print('Shutter pressed')
connections.enableConnectionCheck(False)
loadingScreen()
pygameImages = captureImage()
connections.enableConnectionCheck(True)
elif(program_state == STATE.SHOW_IMAGE):
pygameImages = showLastImage(pygameImages) # show them and remove them
elif(program_state == STATE.SHUT_DOWN):
connections.shutDownPis(cons)
elif(program_state == STATE.GALLERY):
gallery.draw()
if(GPIO.input(scrollLeft) == GPIO.LOW):
GUI.gallery(-1)
lib.waitXThenPullUp(scrollLeft, 50)
elif(GPIO.input(scrollRight) == GPIO.LOW):
GUI.gallery(1)
lib.waitXThenPullUp(scrollRight, 50)
else:
GUI.gallery()
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
GPIO.cleanup() # cleanup all GPIO
connectionThread.stop()
pygame.quit()
return None
# except Exception as e: #Should not close the program on error! Just log message on screen and continue.
# print('Error in main-loop: ' + str(e))
# GUI.message(str(e))
# time.sleep(2)
# mainLoop(pygameImages)
def loadingScreen():
GUI.message('please wait', True)
def showLastImage(pygameImages):
print("showing images.")
for count in range(0,30): # show for 10 frames
image = pygameImages[zigzag[count % len(zigzag)]]
GUI.displayImage(image)
pygame.time.wait(100)
return [] # clearing buffer by returning []
def captureImage():
GPIO.output(ledPin, GPIO.LOW)
frameBuffers, thumpImageNames = connectAndDownload(cons)
try:
print('test')
except TypeError as e:
GUI.message("One or more slave(s) returned 500")
time.sleep(2)
return []
except Exception as e:
GUI.message(str(e))
time.sleep(2)
return []
GPIO.output(ledPin, GPIO.HIGH)
if(frameBuffers == False):
print("Connection error while capturing")
GUI.message('capture error - could not connect')
time.sleep(1.5)
return []
pygameImages = []
for i in range(0, len(frameBuffers)):
image = pygame.image.load(frameBuffers[i])
#image = pygame.transform.smoothscale(image, (320, 320)) #Smoothscale will output a better result. But slower.
image = pygame.transform.scale(image, (320, 320))
pygame.image.save(image, thumpImageNames[i])
pygameImages.append(image)
return pygameImages
mainLoop(pygameImages)