-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathflaskUtils.py
219 lines (177 loc) · 5.39 KB
/
flaskUtils.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
import os
import sys
from flask import Flask, render_template, request
from LEDUtils import *
from Utils import *
#Setup the flask object and get it going
app = Flask(__name__)
# Flask Index
@app.route("/", methods=['GET'])
def index():
return render_template('index.html')
# Flask BG Color API
@app.route("/api/bgcolor", methods=['POST'])
def setBGColor():
# Read the values from the POST
program = request.form['color']
red = int(request.form['red'])
green = int(request.form['green'])
blue = int(request.form['blue'])
PPB.bgDisplayChanged = True
# Change the bg color accordingly
if program == "solid":
PPB.bgColor = ["solid", Color(red,green,blue)]
else:
PPB.bgColor = ["animation", program]
return ""
# Flask Text Color API
@app.route("/api/textcolor", methods=['POST'])
def setTextColor():
# Read the values from the POST
program = request.form['color']
red = int(request.form['red'])
green = int(request.form['green'])
blue = int(request.form['blue'])
PPB.textDisplayChanged = True
# Change the bg color accordingly
if program == "solid":
PPB.textColor = ["solid", Color(red,green,blue)]
else:
PPB.textColor = ["animation", program]
return ""
# Flask Font API
@app.route("/api/font", methods=['POST'])
def setFont():
# Read the values from the POST
font = request.form['font']
# Assign the font variable in LED class
if font == "slanted":
PPB.font = slanted
elif font == "digits":
PPB.font = digits
PPB.fontName = font
PPB.fontChanged = True
PPB.updateDisplayString()
return ""
# Flask Text Animation API
@app.route("/api/textanimation", methods=['POST'])
def setTextAnimation():
# Read the values from the POST
animation = str(request.form['animation'])
lineNum = int(request.form['lineNum'])
if animation == "static":
if PPB.boardType == 'normal':
PPB.textOrigin[0] = [1,1]
elif PPB.boardType == 'xl':
if PPB.lineCount == 1:
PPB.textOrigin[0] = [2,4]
elif PPB.lineCount == 2:
if lineNum == 0:
PPB.textOrigin[lineNum] = [4,1]
elif lineNum == 1:
PPB.textOrigin[lineNum] = [1,7]
PPB.animationSpeed[lineNum] = 0
if animation == "scrolling":
speed = float(request.form['speed'])
PPB.animationSpeed[lineNum] = speed
# Reset the text on the screen
PPB.textStateWipe()
PPB.textOriginMoved = [True, True]
PPB.updateDisplayString()
return ""
# Flask Select Content API
@app.route("/api/setcontent", methods=['POST'])
def setContent():
# Read the values from the POST
content = str(request.form['content'])
lineNum = str(request.form['lineNum'])
checked = str(request.form['checked'])
contentChunk = content + ' ' + lineNum
if checked == 'true' and (contentChunk in PPB.content) == False:
PPB.content.append(contentChunk)
elif checked == 'false' and (contentChunk in PPB.content) == True:
PPB.content.remove(contentChunk)
print PPB.content
PPB.textStateWipe()
return ""
# Flask Set Custom Text API
@app.route("/api/setcustomtext", methods=['POST'])
def setCustomText():
# Read the values from the POST
text = str(request.form['text'])
PPB.customText = text
return ""
# Flask Set Custom Text API
@app.route("/api/weather", methods=['POST'])
def setWeather():
# Read the values from the POST
PPB.tempUnits = str(request.form['unit'])
PPB.weatherZipLocation = str(request.form['zip'])
PPB.weatherCityLocation = str(request.form['city'])
PPB.updateWeather = True
return ""
# Flask Settings API
@app.route("/api/settings", methods=['POST'])
def updateSettings():
# Read the values from the POST
action = str(request.form['action'])
if action == 'save':
PPB.dumpSettings()
elif action == 'load':
PPB.loadSettings(False)
return ""
# Flask Set Brightness API
@app.route("/api/brightness", methods=['POST'])
def setBrightness():
# Read the values from the POST
brightness = int(request.form['brightness'])
PPB.strip.setBrightness(brightness)
return ""
# Flask Set Brightness API
@app.route("/api/timeformat", methods=['POST'])
def setTimeFormat():
# Read the values from the POST
timeFormat = str(request.form['timeFormat'])
PPB.timeFormat = timeFormat
return ""
# Flask Web PageSettings API
@app.route("/api/webpagesettings", methods=['GET','POST'])
def updateWebPageSettings():
if request.method == 'GET':
# Get the web page settings from webpagesettings.txt
with open('/home/pi/pingPongBallClock/code/webpagesettings.txt', 'r') as filehandle:
return filehandle.read()
elif request.method == 'POST':
settings = str(request.form['settings'])
# Write the settings to webpagesettings.txt
with open('/home/pi/pingPongBallClock/code/webpagesettings.txt', 'w') as filehandle:
filehandle.write(settings)
return ""
# Board Type API
@app.route("/api/boardtype", methods=['POST'])
def setBoardType():
# Read the values from the POST
boardType = str(request.form['boardType'])
if boardType == 'normal':
PPB.lineCount = 1
# Change the board type and save the settings
PPB.boardType = boardType
PPB.dumpSettings()
return ""
# XL Settings API
@app.route("/api/xlsettings", methods=['POST'])
def setXLSettings():
# Read the values from the POST
lineCount = int(request.form['lineCount'])
PPB.lineCount = lineCount
if PPB.boardType == 'xl':
if PPB.lineCount == 1:
PPB.textOrigin[0] = [2,4]
elif PPB.lineCount == 2:
PPB.textOrigin[0] = [4,1]
PPB.textOrigin[1] = [1,7]
else:
print "Board is not set to XL"
# Perform a reset of the board to eliminate the ghost text balls
PPB.colorFill(Color(0,0,0), True)
return ""