Created
March 19, 2017 18:21
-
-
Save raspberrycoulis/8132c4cc52d3961fd2e617b2c3cd98be to your computer and use it in GitHub Desktop.
Mote pHAT API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from colorsys import hsv_to_rgb, rgb_to_hsv | |
import motephat | |
from flask import Flask, jsonify, make_response | |
app = Flask(__name__) | |
motephat.configure_channel(1, 16, False) | |
motephat.configure_channel(2, 16, False) | |
motephat.configure_channel(3, 16, False) | |
motephat.configure_channel(4, 16, False) | |
colour = 'FFFFFF' | |
status = 0 | |
def hex_to_rgb(value): | |
value = value.lstrip('#') | |
length = len(value) | |
return tuple(int(value[i:i + length / 3], 16) for i in range(0, length, length / 3)) | |
def mote_on(c): | |
r, g, b = hex_to_rgb(c) | |
for channel in range(4): | |
for pixel in range(16): | |
motephat.set_pixel(channel + 1, pixel, r, g, b) | |
motephat.show() | |
return True | |
def mote_off(): | |
motephat.clear() | |
motephat.show() | |
return True | |
def get_status(): | |
global status | |
for channel in range(4): | |
for pixel in range(16): | |
if motephat.get_pixel(channel + 1, pixel) != (0, 0, 0): | |
status = 1 | |
return status | |
@app.route('/mote/api/v1.0/<string:st>', methods=['GET']) | |
def set_status(st): | |
global status, colour | |
if st == 'on': | |
status = 1 | |
mote_on(colour) | |
elif st == 'off': | |
status = 0 | |
mote_off() | |
elif st == 'status': | |
status = get_status() | |
return jsonify({'status': status, 'colour': colour}) | |
@app.route('/mote/api/v1.0/set', methods=['GET']) | |
def get_colour(): | |
global colour | |
return jsonify({'status': status, 'colour': colour}) | |
@app.route('/mote/api/v1.0/set/<string:c>', methods=['GET']) | |
def set_colour(c): | |
global status, colour | |
colour = c | |
if status != 0: | |
mote_on(colour) | |
status = 1 | |
return jsonify({'status': status, 'colour': colour}) | |
@app.errorhandler(404) | |
def not_found(error): | |
return make_response(jsonify({'error': 'Not found'}), 404) | |
if __name__ == '__main__': | |
mote_off() | |
app.run(host='0.0.0.0', debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment