Skip to content

Commit 3e53a7b

Browse files
authored
Merge pull request EarthstormSoftware#3 from dglaude/split_blinkt
Add Blinkt! and Rainbow HAT support
2 parents 7c44b8c + 935bfc0 commit 3e53a7b

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

blinktapi.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from colorsys import hsv_to_rgb, rgb_to_hsv
2+
from flask import Flask, jsonify, make_response
3+
import blinkt
4+
5+
app = Flask(__name__)
6+
7+
colour = 'FFFFFF'
8+
status = 0
9+
10+
def hex_to_rgb(value):
11+
value = value.lstrip('#')
12+
length = len(value)
13+
return tuple(int(value[i:i + length // 3], 16) for i in range(0, length, length // 3))
14+
15+
def blinkt_on(c):
16+
global status
17+
r, g, b = hex_to_rgb(c)
18+
blinkt.set_all(r, g, b)
19+
blinkt.show()
20+
status=1
21+
return True
22+
23+
def blinkt_off():
24+
global status
25+
blinkt.clear()
26+
blinkt.show()
27+
status=0
28+
return True
29+
30+
def get_status():
31+
global status
32+
return status
33+
34+
@app.route('/mote/api/v1.0/<string:st>', methods=['GET'])
35+
def set_status(st):
36+
global status, colour
37+
if st == 'on':
38+
status = 1
39+
blinkt_on(colour)
40+
elif st == 'off':
41+
status = 0
42+
blinkt_off()
43+
elif st == 'status':
44+
status = get_status()
45+
return jsonify({'status': status, 'colour': colour})
46+
47+
@app.route('/mote/api/v1.0/set', methods=['GET'])
48+
def get_colour():
49+
global colour
50+
return jsonify({'status': status, 'colour': colour})
51+
52+
@app.route('/mote/api/v1.0/set/<string:c>', methods=['GET'])
53+
def set_colour(c):
54+
global status, colour
55+
colour = c
56+
if status != 0:
57+
blinkt_on(colour)
58+
status = 1
59+
return jsonify({'status': status, 'colour': colour})
60+
61+
@app.errorhandler(404)
62+
def not_found(error):
63+
return make_response(jsonify({'error': 'Not found'}), 404)
64+
65+
if __name__ == '__main__':
66+
blinkt_off()
67+
app.run(host='0.0.0.0', debug=True)

rainbowapi.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from colorsys import hsv_to_rgb, rgb_to_hsv
2+
from flask import Flask, jsonify, make_response
3+
from rainbowhat import rainbow as blinkt
4+
5+
app = Flask(__name__)
6+
7+
8+
colour = 'FFFFFF'
9+
status = 0
10+
11+
def hex_to_rgb(value):
12+
value = value.lstrip('#')
13+
length = len(value)
14+
return tuple(int(value[i:i + length // 3], 16) for i in range(0, length, length // 3))
15+
16+
def blinkt_on(c):
17+
global status
18+
r, g, b = hex_to_rgb(c)
19+
blinkt.set_all(r, g, b)
20+
blinkt.show()
21+
status=1
22+
return True
23+
24+
def blinkt_off():
25+
global status
26+
blinkt.clear()
27+
blinkt.show()
28+
status=0
29+
return True
30+
31+
def get_status():
32+
global status
33+
return status
34+
35+
@app.route('/mote/api/v1.0/<string:st>', methods=['GET'])
36+
def set_status(st):
37+
global status, colour
38+
if st == 'on':
39+
status = 1
40+
blinkt_on(colour)
41+
elif st == 'off':
42+
status = 0
43+
blinkt_off()
44+
elif st == 'status':
45+
status = get_status()
46+
return jsonify({'status': status, 'colour': colour})
47+
48+
@app.route('/mote/api/v1.0/set', methods=['GET'])
49+
def get_colour():
50+
global colour
51+
return jsonify({'status': status, 'colour': colour})
52+
53+
@app.route('/mote/api/v1.0/set/<string:c>', methods=['GET'])
54+
def set_colour(c):
55+
global status, colour
56+
colour = c
57+
if status != 0:
58+
blinkt_on(colour)
59+
status = 1
60+
return jsonify({'status': status, 'colour': colour})
61+
62+
@app.errorhandler(404)
63+
def not_found(error):
64+
return make_response(jsonify({'error': 'Not found'}), 404)
65+
66+
if __name__ == '__main__':
67+
blinkt_off()
68+
app.run(host='0.0.0.0', debug=True)

0 commit comments

Comments
 (0)