Created
April 1, 2020 18:11
-
-
Save alexellis/deb5577b078022888274af27b3a0c70c to your computer and use it in GitHub Desktop.
unicorn_server.py
This file contains 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 flask import Flask | |
app = Flask(__name__) | |
import unicornhat as UH | |
import time | |
import os | |
import signal | |
import sys | |
def clear_uh(): | |
UH.clear() | |
UH.show() | |
def sigterm_handler(_signo, _stack_frame): | |
clear_uh() | |
sys.exit(0) | |
max_pixels = 8 | |
signal.signal(signal.SIGTERM, sigterm_handler) | |
def on(column, r, g, b): | |
x = column | |
for y in range(0, max_pixels): | |
UH.set_pixel(x, y, r, g, b) | |
UH.show() | |
sleep_for = 0.25 | |
@app.route('/red') | |
def blink_red(): | |
for i in range(0,8): | |
on(i, 255, 0, 0) | |
time.sleep(sleep_for) | |
clear_uh() | |
return 'Blinking red!' | |
@app.route('/') | |
def blink(): | |
for i in range(0,8): | |
on(i, 0, 0, 255) | |
time.sleep(sleep_for) | |
clear_uh() | |
return 'Blinking!' | |
if __name__ == '__main__': | |
app.run(host="0.0.0.0", port=5000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment