-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
98 lines (80 loc) · 2.5 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
from flask import Flask, render_template, request, Response
from helpers import get_audio_files
import RPi.GPIO as io
import pygame
import time
from camera_pi import Camera
from io_control import IOController
# global variables
app = Flask(__name__)
ioCtrl = IOController()
# render index page template
@app.route('/')
def index():
# return page html as a response
return render_template('index.html', audio_files=list(get_audio_files().keys()))
@app.route('/camera')
def camera():
return render_template('camera.html')
# video streaming generator function
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
# path for the video stream image resource, use as src attribute of html img tag
@app.route('/video_feed')
def video_feed():
return Response(gen(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
# render laser control page template
@app.route('/laser_control')
def laser_control():
return render_template('laser_control.html')
# handlers for GET requests made using XMLHttpRequest from the client
# each function is responsible for handling the state for the corresponding key
# key events
@app.route('/w<int:state>')
def w_state(state):
global ioCtrl
print("W state:", state)
ioCtrl.set_laser_controls(None, state)
return "w"+str(state)
@app.route('/a<int:state>')
def a_state(state):
global ioCtrl
print("A state:", state)
ioCtrl.set_laser_controls(state, None)
return "a"+str(state)
@app.route('/s<int:state>')
def s_state(state):
global ioCtrl
print("S state:", state)
ioCtrl.set_laser_controls(None, -state)
return "s"+str(state)
@app.route('/d<int:state>')
def d_state(state):
global ioCtrl
print("D state:", state)
ioCtrl.set_laser_controls(-state, None)
return "d"+str(state)
@app.route('/laser<int:state>')
def laser_state(state):
print("Laser state:", state)
laser_on = False
if state == 1:
laser_on = True
ioCtrl.set_laser_state(laser_on)
return "laser"+str(state)
# handler for XMLHttpRequests for playing audio files
@app.route('/play_audio/<filename>')
def play_audio(filename):
filepath = get_audio_files()[filename]
print("Playing audio from file", filepath)
pygame.mixer.init()
pygame.mixer.music.load(filepath)
pygame.mixer.music.play()
return ""
if __name__ == '__main__':
ioCtrl.start()
app.run(host='0.0.0.0', threaded=True)