-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensehat-api.py
308 lines (266 loc) · 8.51 KB
/
sensehat-api.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
from os import popen
from sense_hat import SenseHat
from flask import Flask, jsonify, request
app = Flask(__name__)
sensehat = SenseHat()
#-#
#-# LED Matrix
#-#
@app.route('/sensehat/ledmatrix/pixel/<int:x>/<int:y>', methods=['GET'])
def ledmatrix_pixel(x, y):
'''
Gets an individual LED matrix pixel at the specified X-Y coordinate.
'''
return jsonify(sensehat.get_pixel(x, y))
@app.route('/sensehat/ledmatrix/pixel/<int:x>/<int:y>', methods=['POST'])
def ledmatrix_pixel_set(x, y):
'''
Sets an individual LED matrix pixel at the specified X-Y coordinate
to the specified colour.
'''
payload = request.json
if payload.get('pixel'):
sensehat.set_pixel(x, y, payload.get('pixel'))
else:
sensehat.set_pixel(
x, y,
payload.get('red'),
payload.get('green'),
payload.get('blue')
)
return 'OK'
@app.route('/sensehat/ledmatrix/pixels', methods=['GET'])
def ledmatrix_pixels():
'''
Gets a list containing 64 smaller lists of [R,G,B] pixels
representing what is currently displayed on the LED matrix.
'''
return jsonify(sensehat.get_pixels())
@app.route('/sensehat/ledmatrix/pixels', methods=['POST'])
def ledmatrix_pixels_set():
'''
Updates the entire LED matrix based on a 64 length list of pixel values.
'''
sensehat.set_pixels(request.json)
return 'OK'
@app.route('/sensehat/ledmatrix/letter', methods=['POST'])
def ledmatrix_letter():
'''
Scrolls a string of text across the LED matrix using the specified
speed and colours.
'''
sensehat.show_letter(
str(request.json.get('letter')),
request.json.get('text_colour', [255, 255, 255]),
request.json.get('background_colour', [0, 0, 0])
)
return 'OK'
@app.route('/sensehat/ledmatrix/message', methods=['POST'])
def ledmatrix_message():
'''
Scrolls a string of text across the LED matrix using the specified
speed and colours.
'''
sensehat.show_message(
request.json.get('message'),
request.json.get('scroll_speed', .1),
request.json.get('text_colour', [255, 255, 255]),
request.json.get('background_colour', [0, 0, 0])
)
return 'OK'
@app.route('/sensehat/ledmatrix/rotation', methods=['GET'])
def ledmatrix_rotation():
'''
Gets the LED matrix rotation.
'''
return jsonify(sensehat.rotation)
@app.route('/sensehat/ledmatrix/rotation', methods=['POST'])
def ledmatrix_rotation_set():
'''
Sets the LED matrix rotation for viewing, adjust if the Pi is upside
down or sideways. 0 is with the Pi HDMI port facing downwards.
'''
sensehat.set_rotation(
request.json.get('rotation'),
request.json.get('redraw', True),
)
return 'OK'
@app.route('/sensehat/ledmatrix/flip', methods=['POST'])
def ledmatrix_flip():
'''
Flips the image on the LED matrix horizontally or vertically.
'''
orientation = request.json.get('orientation')
redraw = request.json.get('redraw', True)
if orientation == 'horizontal':
return jsonify(sensehat.flip_h(redraw))
elif orientation == 'vertical':
return jsonify(sensehat.flip_v(redraw))
@app.route('/sensehat/ledmatrix/clear', methods=['POST'])
def ledmatrix_clear():
'''
Sets the entire LED matrix to a single colour, defaults to blank/off.
'''
payload = request.json or {}
sensehat.clear(
payload.get('red', 0),
payload.get('green', 0),
payload.get('blue', 0)
)
return 'OK'
@app.route('/sensehat/ledmatrix/lowlight', methods=['GET'])
def ledmatrix_lowlight():
'''
Returns whether the LED matrix is in low light mode or not.
'''
return ('OFF', 'ON')[sensehat.low_light]
@app.route('/sensehat/ledmatrix/lowlight/<string:status>', methods=['POST'])
def ledmatrix_lowlight_set(status):
'''
Sets the LED matrix low light mode on/off.
Useful if the Sense HAT is being used in a dark environment.
'''
sensehat.low_light = status.lower() == 'on'
return 'OK'
@app.route('/sensehat/ledmatrix/gamma', methods=['GET'])
def ledmatrix_gamma():
'''
Gets the gamma lookup table.
'''
return jsonify(sensehat.gamma)
@app.route('/sensehat/ledmatrix/gamma', methods=['POST'])
def ledmatrix_gamma_set():
'''
Sets the gamma lookup table.
The lookup table is a list of 32 numbers that must be between 0 and 31.
'''
sensehat.gamma = request.json
return 'OK'
@app.route('/sensehat/ledmatrix/gamma/reset', methods=['POST'])
def ledmatrix_gamma_reset():
'''
Sets the gamma lookup table to default, ideal if you've been
messing with it and want to get it back to a default state.
'''
sensehat.gamma_reset()
return 'OK'
#-#
#-# Environmental sensors
#-#
def temperature_from_cpu():
'''
Gets the Raspberry Pi's CPU temperature in Celsius.
'''
cpu_temp = popen('/opt/vc/bin/vcgencmd measure_temp').read()
cpu_temp = cpu_temp.replace('temp=','')
cpu_temp = cpu_temp.replace('\'C\n','')
return float(cpu_temp)
@app.route('/sensehat/temperature', methods=['GET'])
def temperature():
'''
Gets the temperature in Celsius calibrated with respect for the
parasitic heat generated by the Raspberry Pi's CPU.
'''
raw_temp = sensehat.temperature
cpu_temp = temperature_from_cpu()
cal_temp = raw_temp - ((cpu_temp - raw_temp) / 2)
return jsonify(cal_temp)
@app.route('/sensehat/temperature/raw', methods=['GET'])
def temperature_raw():
'''
Gets the temperature in Celsius.
'''
return jsonify(sensehat.temperature)
@app.route('/sensehat/temperature/from/humidity', methods=['GET'])
def temperature_from_humidity():
'''
Gets the temperature in Celsius from the humidity sensor.
'''
return jsonify(sensehat.temperature_from_humidity)
@app.route('/sensehat/temperature/from/pressure', methods=['GET'])
def temperature_from_pressure():
'''
Gets the temperature in Celsius from the pressure sensor.
'''
return jsonify(sensehat.temperature_from_pressure)
@app.route('/sensehat/humidity', methods=['GET'])
def humidity():
'''
Gets the percentage of relative humidity.
'''
return jsonify(sensehat.humidity)
@app.route('/sensehat/pressure', methods=['GET'])
def pressure():
'''
Gets the pressure in Millibars.
'''
return jsonify(sensehat.pressure)
#-#
#-# IMU Sensor
#-#
@app.route('/sensehat/imu/config', methods=['GET'])
def imu_config():
'''
Gets the status for the accelerometer, gyroscope and magnetometer sensors.
'''
result = {
'accelerometer_enabled': sensehat._accel_enabled,
'compass_enabled': sensehat._compass_enabled,
'gyroscope_enabled': sensehat._gyro_enabled
}
return jsonify(result)
@app.route('/sensehat/imu/config', methods=['PUT'])
def imu_config_update():
'''
Enables and disables the accelerometer, gyroscope and/or magnetometer.
'''
sensehat.set_imu_config(
request.json.get('compass_enabled', sensehat._compass_enabled),
request.json.get('gyroscope_enabled', sensehat._gyro_enabled),
request.json.get('accelerometer_enabled', sensehat._accel_enabled)
)
return 'OK'
@app.route('/sensehat/accelerometer', methods=['GET'])
def accelerometer():
'''
Calls IMU config to disable the magnetometer and gyroscope then
gets the current orientation from the accelerometer only.
'''
return jsonify(sensehat.accelerometer)
@app.route('/sensehat/accelerometer/raw', methods=['GET'])
def accelerometer_raw():
'''
Gets the raw x, y and z axis accelerometer data.
'''
return jsonify(sensehat.accelerometer_raw)
@app.route('/sensehat/gyroscope', methods=['GET'])
def gyroscope():
'''
Calls IMU config to disable the magnetometer and accelerometer then
gets the current orientation from the gyroscope only.
'''
return jsonify(sensehat.gyroscope)
@app.route('/sensehat/gyroscope/raw', methods=['GET'])
def gyroscope_raw():
'''
Gets the raw x, y and z axis gyroscope data.
'''
return jsonify(sensehat.gyroscope_raw)
@app.route('/sensehat/compass', methods=['GET'])
def compass():
'''
Calls IMU config to disable the gyroscope and accelerometer then
gets the direction of North from the magnetometer in degrees.
'''
return jsonify(sensehat.compass)
@app.route('/sensehat/compass/raw', methods=['GET'])
def compass_raw():
'''
Gets the raw x, y and z axis magnetometer data.
'''
return jsonify(sensehat.compass_raw)
#-#
#-# MAIN
#-#
if __name__ == '__main__':
app.run(host='0.0.0.0')