-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (61 loc) · 1.93 KB
/
app.py
File metadata and controls
77 lines (61 loc) · 1.93 KB
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
from LED import LED
from flask import Flask
from flask import render_template, request, redirect, url_for
import _thread
app = Flask(__name__)
lights = LED(255, 102, 0, 1)
lights.gpio()
@app.route('/')
@app.route('/<int:power>')
@app.route('/<int:red>,<int:green>,<int:blue>')
@app.route('/<int:red>,<int:green>,<int:blue>,<int:power>')
def index(red=None, green=None, blue=None, power=None, function=None):
lights.fading = False
# only update color if a color is specified and max is 255
if red is None:
red = lights.red
if green is None:
green = lights.green
if blue is None:
blue = lights.blue
if power is None:
power = lights.power
elif power > 1 and power <= 100:
power /= 100
elif power > 100:
power = 1
lights.update(red, green, blue, power)
if power == 0:
print("powering off")
lights.off()
else:
lights.gpio()
return render_template('index.html', lights=lights)
@app.route('/', methods=['POST'])
def post():
red = request.form['red']
green = request.form['green']
blue = request.form['blue']
power = request.form['power']
lights.update(red, green, blue, power)
return redirect(url_for('index'))
@app.route('/sleep')
def sleep():
_thread.start_new_thread(lights.sleep())
return redirect(url_for('index'))
@app.route('/fade<int:speed>')
@app.route('/fade<int:red>,<int:green>,<int:blue>')
@app.route('/fade<int:red>,<int:green>,<int:blue>,<int:speed>')
def fade(red=None, green=None, blue=None, speed=0):
if red is None:
red = lights.red
if green is None:
green = lights.green
if blue is None:
blue = lights.blue
if speed is not 0 and lights.fading:
lights.speed = speed
return ('', 204)
else:
_thread.start_new_thread(lights.fade, (red, green, blue))
return render_template('fading.html', lights=lights, r=red, g=green, b=blue)