Refactoring
- Animation in it's own class - JS to sync state across browsers
This commit is contained in:
parent
f4f5458209
commit
efb7c20af5
6 changed files with 352 additions and 160 deletions
56
foobaz.py
56
foobaz.py
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
import argparse
|
||||
import sys
|
||||
from typing import Tuple
|
||||
|
||||
from bottle import route, run, static_file, view
|
||||
from bottle import post, request, route, run, static_file, view
|
||||
|
||||
from dmx import DMX, Bar252, StairvilleLedPar56, Steady, RotatingRainbow, REDSpot18RGB, Chase, FadeTo
|
||||
from animation import Off
|
||||
from dmx import DMX, Bar252, StairvilleLedPar56, REDSpot18RGB
|
||||
|
||||
|
||||
@route('/')
|
||||
|
|
@ -17,23 +19,28 @@ def index():
|
|||
def static(path):
|
||||
return static_file(path, root='static')
|
||||
|
||||
@route('/api/state/<state>')
|
||||
def update(state):
|
||||
if state == "off":
|
||||
dmx.setAnimation(None)
|
||||
elif state == "white":
|
||||
dmx.setAnimation(Steady(255, 255, 255))
|
||||
elif state == "red":
|
||||
dmx.setAnimation(Steady(255, 0, 0))
|
||||
elif state == "blue":
|
||||
dmx.setAnimation(Steady(0, 0, 255))
|
||||
elif state == "rainbow":
|
||||
dmx.setAnimation(RotatingRainbow())
|
||||
elif state == "chase-blue":
|
||||
dmx.setAnimation(Chase(0, 0, 255))
|
||||
dmx.start()
|
||||
return {'result': 'ok'}
|
||||
@route('/api/state')
|
||||
def state():
|
||||
return {
|
||||
'animation': dmx.animation,
|
||||
'color': rgb_to_hex(dmx.color)
|
||||
}
|
||||
|
||||
@post('/api/state')
|
||||
def update():
|
||||
json = request.json
|
||||
print(json)
|
||||
dmx.animation = json["animation"]
|
||||
dmx.color = hex_to_rgb(json["color"])
|
||||
return state()
|
||||
|
||||
|
||||
def hex_to_rgb(hex: str) -> tuple[int, ...]:
|
||||
return tuple(int(hex[i:i+2], 16) for i in (1, 3, 5))
|
||||
|
||||
|
||||
def rgb_to_hex(color: Tuple[int, int, int]) -> str:
|
||||
return f"#{color[0]:02X}{color[1]:02X}{color[2]:02X}"
|
||||
|
||||
def main(args):
|
||||
global dmx
|
||||
|
|
@ -47,17 +54,17 @@ def main(args):
|
|||
|
||||
print(f"Starting DMX via Art-Net to {artnet}", file=sys.stderr)
|
||||
# dmx = DMX("10.31.242.35")
|
||||
dmx = DMX(artnet, maxchan=64)
|
||||
dmx = DMX(artnet, maxchan=128)
|
||||
|
||||
if args.room == "shop":
|
||||
dmx.rgbs = [
|
||||
dmx._rgbs = [
|
||||
REDSpot18RGB(dmx, 1),
|
||||
REDSpot18RGB(dmx, 5),
|
||||
REDSpot18RGB(dmx, 9),
|
||||
REDSpot18RGB(dmx, 13),
|
||||
]
|
||||
elif args.room == "big":
|
||||
dmx.rgbs = [
|
||||
dmx._rgbs = [
|
||||
StairvilleLedPar56(dmx, 1),
|
||||
StairvilleLedPar56(dmx, 8),
|
||||
StairvilleLedPar56(dmx, 15),
|
||||
|
|
@ -70,10 +77,11 @@ def main(args):
|
|||
else:
|
||||
print(f"Unknown room {args.room}", file=sys.stderr)
|
||||
sys.exit(64)
|
||||
dmx.setAnimation(FadeTo(128, 128, 128))
|
||||
dmx.start()
|
||||
dmx.animation = Off()
|
||||
dmx.color = (128, 128, 128)
|
||||
dmx.animation = "fade"
|
||||
|
||||
run(host='0.0.0.0', port=8080, reloader=False)
|
||||
run(host='0.0.0.0', port=8080, reloader=False, debug=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv[1:])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue