2026-03-21 01:22:32 +01:00
|
|
|
from microdot import Microdot, send_file
|
2026-03-25 08:37:07 +01:00
|
|
|
import machine, neopixel, time
|
|
|
|
|
import uasyncio as asyncio
|
2026-03-21 01:22:32 +01:00
|
|
|
|
|
|
|
|
app = Microdot()
|
|
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
# =========================
|
|
|
|
|
# NeoPixel setup
|
|
|
|
|
# =========================
|
|
|
|
|
nbPixels = 12 * 2
|
|
|
|
|
pinPixelStrip = 16
|
2026-03-21 01:22:32 +01:00
|
|
|
neoPixelStrip = neopixel.NeoPixel(machine.Pin(pinPixelStrip), nbPixels)
|
|
|
|
|
|
|
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
# =========================
|
|
|
|
|
# LED CONTROLLER
|
|
|
|
|
# =========================
|
|
|
|
|
class LedController:
|
|
|
|
|
def __init__(self, np):
|
|
|
|
|
self._np = np
|
|
|
|
|
|
|
|
|
|
# async control
|
|
|
|
|
self._flag = asyncio.ThreadSafeFlag()
|
|
|
|
|
self._cmd = None
|
|
|
|
|
self._task = None
|
|
|
|
|
|
|
|
|
|
# state
|
|
|
|
|
self.states = {
|
|
|
|
|
'BUSY': (255, 0, 0),
|
|
|
|
|
'AVAILABLE': (0, 255, 0),
|
|
|
|
|
'AWAY': (246, 190, 0),
|
|
|
|
|
'OFF': (0, 0, 0),
|
|
|
|
|
'ON': (255, 255, 255)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.color = self.states['OFF']
|
|
|
|
|
self.status = 'off'
|
|
|
|
|
self.previousStatus = 'off'
|
|
|
|
|
self.brightness = 0.1
|
|
|
|
|
|
|
|
|
|
self.blinking = False
|
|
|
|
|
self.blinkFrequency = 0
|
|
|
|
|
self.blinkDuration = 0
|
|
|
|
|
self.blinkStart = 0
|
|
|
|
|
|
|
|
|
|
# ---------- internal ----------
|
|
|
|
|
def _apply_brightness(self, color):
|
|
|
|
|
r, g, b = color
|
|
|
|
|
return (
|
|
|
|
|
int(r * self.brightness),
|
|
|
|
|
int(g * self.brightness),
|
|
|
|
|
int(b * self.brightness),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _write(self, color):
|
|
|
|
|
self._np.fill(self._apply_brightness(color))
|
|
|
|
|
self._np.write()
|
|
|
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
|
while True:
|
|
|
|
|
await self._flag.wait()
|
|
|
|
|
cmd = self._cmd
|
|
|
|
|
|
|
|
|
|
# Cancel any ongoing task before starting a new one
|
|
|
|
|
if self._task:
|
|
|
|
|
self._task.cancel()
|
|
|
|
|
try:
|
|
|
|
|
await self._task
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# Process the new command
|
|
|
|
|
t = cmd["type"]
|
|
|
|
|
|
|
|
|
|
if t == "blink":
|
|
|
|
|
self._task = asyncio.create_task(self._blink(cmd))
|
|
|
|
|
elif t == "solid":
|
|
|
|
|
self._task = asyncio.create_task(self._solid(cmd))
|
|
|
|
|
elif t == "off":
|
|
|
|
|
self._task = asyncio.create_task(self._off())
|
|
|
|
|
|
|
|
|
|
async def _solid(self, cmd):
|
|
|
|
|
self.color = cmd["color"]
|
|
|
|
|
self.status = cmd.get("status", "colored")
|
|
|
|
|
self._write(self.color)
|
|
|
|
|
|
|
|
|
|
async def _off(self):
|
|
|
|
|
self.color = (0, 0, 0)
|
|
|
|
|
self.status = "off"
|
|
|
|
|
self._write(self.color)
|
|
|
|
|
|
|
|
|
|
async def _blink(self, cmd):
|
|
|
|
|
try:
|
|
|
|
|
self.blinking = True
|
|
|
|
|
|
|
|
|
|
self.color = cmd["color"]
|
|
|
|
|
self.previousStatus = cmd["previousStatus"]
|
|
|
|
|
self.blinkFrequency = cmd["frequency"]
|
|
|
|
|
self.blinkDuration = cmd["duration"]
|
|
|
|
|
self.blinkStart = time.ticks_ms()
|
|
|
|
|
|
|
|
|
|
freq = max(0.1, self.blinkFrequency)
|
|
|
|
|
half_ms = int(500 / freq)
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
if self.blinkDuration != 0:
|
|
|
|
|
elapsed = time.ticks_diff(time.ticks_ms(), self.blinkStart)
|
|
|
|
|
if elapsed > self.blinkDuration * 1000:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
self._write(self.color)
|
|
|
|
|
await asyncio.sleep_ms(half_ms)
|
|
|
|
|
|
|
|
|
|
self._write((0, 0, 0))
|
|
|
|
|
await asyncio.sleep_ms(half_ms)
|
|
|
|
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
self.blinking = False
|
|
|
|
|
self.status = self.previousStatus
|
|
|
|
|
self._write(self.color)
|
|
|
|
|
|
|
|
|
|
async def send(self, cmd):
|
|
|
|
|
self._cmd = cmd
|
|
|
|
|
self._flag.set()
|
|
|
|
|
|
|
|
|
|
# ---------- public API ----------
|
|
|
|
|
async def set_color(self, color, brightness=None):
|
|
|
|
|
if brightness is not None:
|
|
|
|
|
self.brightness = brightness
|
|
|
|
|
|
|
|
|
|
await self.send({
|
|
|
|
|
"type": "solid",
|
|
|
|
|
"color": color
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
async def set_status(self, status):
|
|
|
|
|
status = status.upper()
|
|
|
|
|
color = self.states.get(status)
|
|
|
|
|
|
|
|
|
|
if color is None:
|
|
|
|
|
raise ValueError("invalid status")
|
|
|
|
|
|
|
|
|
|
await self.send({
|
|
|
|
|
"type": "solid",
|
|
|
|
|
"color": color,
|
|
|
|
|
"status": status.lower()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
async def blink(self, frequency, duration):
|
|
|
|
|
self.previousStatus = self.status
|
|
|
|
|
|
|
|
|
|
await self.send({
|
|
|
|
|
"type": "blink",
|
|
|
|
|
"color": self.color,
|
|
|
|
|
"frequency": frequency,
|
|
|
|
|
"duration": duration,
|
|
|
|
|
"previousStatus": self.previousStatus
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
|
await self.send({"type": "off"})
|
|
|
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
|
if self.blinking and self.blinkDuration > 0:
|
|
|
|
|
elapsed = time.ticks_diff(time.ticks_ms(), self.blinkStart) / 1000
|
|
|
|
|
remains = max(0.0, self.blinkDuration - elapsed)
|
|
|
|
|
else:
|
|
|
|
|
remains = 0.0
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"status": self.status,
|
|
|
|
|
"color": self.color,
|
|
|
|
|
"brightness": self.brightness,
|
|
|
|
|
"isblinking": self.blinking,
|
|
|
|
|
"frequency": self.blinkFrequency,
|
|
|
|
|
"duration": self.blinkDuration,
|
|
|
|
|
"remains": remains
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# =========================
|
|
|
|
|
# INIT
|
|
|
|
|
# =========================
|
|
|
|
|
led = LedController(neoPixelStrip)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# =========================
|
|
|
|
|
# ROUTES
|
|
|
|
|
# =========================
|
2026-03-21 01:22:32 +01:00
|
|
|
|
|
|
|
|
@app.get('/static/<path:path>')
|
|
|
|
|
async def staticRoutes(request, path):
|
|
|
|
|
if '..' in path:
|
2026-03-25 08:37:07 +01:00
|
|
|
return {'error': '404 Not found'}, 404
|
2026-03-21 01:22:32 +01:00
|
|
|
return send_file('static/' + path)
|
|
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
@app.get('/')
|
|
|
|
|
async def getIndex(request):
|
|
|
|
|
return send_file('static/index.html')
|
|
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
@app.get('/api/brightness')
|
|
|
|
|
async def getBrightness(request):
|
2026-03-25 08:37:07 +01:00
|
|
|
return {'brightness': led.brightness}
|
|
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
|
|
|
|
|
@app.post('/api/brightness')
|
|
|
|
|
async def setBrightness(request):
|
|
|
|
|
brightness = request.json.get("brightness")
|
2026-03-25 08:37:07 +01:00
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
if brightness is None:
|
|
|
|
|
return {'error': 'missing brightness parameter'}, 400
|
2026-03-25 08:37:07 +01:00
|
|
|
|
|
|
|
|
if not isinstance(brightness, (int, float)):
|
|
|
|
|
return {'error': 'wrong brightness type'}, 400
|
|
|
|
|
|
|
|
|
|
if brightness < 0 or brightness > 1:
|
|
|
|
|
return {'error': 'brightness out of bound'}, 400
|
|
|
|
|
|
|
|
|
|
current_status = led.status
|
|
|
|
|
current_color = led.color
|
|
|
|
|
|
|
|
|
|
await led.set_color(current_color, brightness)
|
|
|
|
|
|
|
|
|
|
led.status = current_status
|
|
|
|
|
|
|
|
|
|
return {'brightness': led.brightness}
|
|
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
|
|
|
|
|
@app.post('/api/color')
|
|
|
|
|
async def setColor(request):
|
|
|
|
|
r = request.json.get("r")
|
|
|
|
|
g = request.json.get("g")
|
|
|
|
|
b = request.json.get("b")
|
|
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
if r is None or g is None or b is None:
|
2026-03-21 01:22:32 +01:00
|
|
|
return {'error': 'missing color'}, 400
|
2026-03-25 08:37:07 +01:00
|
|
|
|
|
|
|
|
if not all(isinstance(v, int) for v in (r, g, b)):
|
|
|
|
|
return {'error': 'wrong color type'}, 400
|
|
|
|
|
|
|
|
|
|
if not (0 <= r <= 255 and 0 <= g <= 255 and 0 <= b <= 255):
|
|
|
|
|
return {'error': 'color out of bound'}, 400
|
|
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
brightness = request.json.get("brightness")
|
|
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
await led.set_color((r, g, b), brightness)
|
|
|
|
|
|
|
|
|
|
return {'status': led.status}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get('/api/color')
|
|
|
|
|
async def getColor(request):
|
|
|
|
|
r, g, b = led.color
|
|
|
|
|
return {'r': r, 'g': g, 'b': b, 'brightness': led.brightness}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get('/api/status')
|
|
|
|
|
async def getStatus(request):
|
|
|
|
|
return {'status': led.status}
|
|
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
|
|
|
|
|
@app.route('/api/status/<status>', methods=['GET', 'POST'])
|
|
|
|
|
async def setStatus(request, status):
|
2026-03-25 08:37:07 +01:00
|
|
|
try:
|
|
|
|
|
await led.set_status(status)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return {'error': 'unknown status'}, 404
|
|
|
|
|
|
|
|
|
|
return {'status': led.status}
|
2026-03-21 05:59:37 +01:00
|
|
|
|
2026-03-21 06:23:17 +01:00
|
|
|
|
2026-03-21 05:59:37 +01:00
|
|
|
@app.post('/api/blink')
|
|
|
|
|
async def setBlink(request):
|
2026-03-25 08:37:07 +01:00
|
|
|
freq = request.json.get('frequency')
|
2026-03-21 05:59:37 +01:00
|
|
|
duration = request.json.get('duration')
|
|
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
if freq is None or duration is None:
|
|
|
|
|
return {'error': 'missing frequency or duration'}, 400
|
2026-03-21 05:59:37 +01:00
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
if not isinstance(freq, (int, float)) or freq <= 0:
|
|
|
|
|
return {'error': 'invalid frequency'}, 400
|
2026-03-21 05:59:37 +01:00
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
if not isinstance(duration, (int, float)) or duration < 0:
|
|
|
|
|
return {'error': 'invalid duration'}, 400
|
2026-03-21 05:59:37 +01:00
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
await led.blink(freq, duration)
|
2026-03-21 05:59:37 +01:00
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
return {'status': 'blinking', 'frequency': freq, 'duration': duration}
|
2026-03-21 05:59:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get('/api/blink')
|
|
|
|
|
async def getBlink(request):
|
2026-03-25 08:37:07 +01:00
|
|
|
state = led.get_state()
|
2026-03-21 05:59:37 +01:00
|
|
|
return {
|
2026-03-25 08:37:07 +01:00
|
|
|
'isblinking': state["isblinking"],
|
|
|
|
|
'frequency': state["frequency"],
|
|
|
|
|
'duration': state["duration"],
|
|
|
|
|
'remains': state["remains"]
|
2026-03-21 05:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
@app.post('/api/blink/stop')
|
|
|
|
|
async def blinkStop(request):
|
|
|
|
|
await led.stop()
|
|
|
|
|
await led.set_status(led.previousStatus)
|
|
|
|
|
return {'status': led.status}
|
|
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
|
|
|
|
|
@app.get('/api/debug')
|
|
|
|
|
async def getDebugInfo(request):
|
2026-03-25 08:37:07 +01:00
|
|
|
r, g, b = led.color
|
|
|
|
|
dr, dg, db = led._np[0]
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'status': led.status,
|
|
|
|
|
'brightness': led.brightness,
|
|
|
|
|
'color': {'r': r, 'g': g, 'b': b},
|
|
|
|
|
'dimColor': {'r': dr, 'g': dg, 'b': db},
|
|
|
|
|
'blinking': led.blinking,
|
|
|
|
|
'blinkFrequency': led.blinkFrequency,
|
|
|
|
|
'blinkDuration': led.blinkDuration
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
|
|
|
|
|
@app.post('/api/mutedeck-webhook')
|
|
|
|
|
async def mutedeckWebhook(request):
|
2026-03-25 08:37:07 +01:00
|
|
|
|
2026-03-21 01:22:32 +01:00
|
|
|
if request.json.get('control') != 'system':
|
|
|
|
|
if request.json.get('call') == 'active':
|
2026-03-25 08:37:07 +01:00
|
|
|
isMuted = request.json.get('mute') == 'active'
|
|
|
|
|
|
|
|
|
|
if isMuted:
|
|
|
|
|
await led.set_status('away')
|
2026-03-21 01:22:32 +01:00
|
|
|
else:
|
2026-03-25 08:37:07 +01:00
|
|
|
await led.set_status('busy')
|
2026-03-21 01:22:32 +01:00
|
|
|
else:
|
2026-03-25 08:37:07 +01:00
|
|
|
await led.set_status('available')
|
2026-03-21 01:22:32 +01:00
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
return {'status': led.status}
|
2026-03-21 01:22:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post('/shutdown')
|
|
|
|
|
async def shutdown(request):
|
|
|
|
|
request.app.shutdown()
|
|
|
|
|
return 'The server is shutting down...'
|
|
|
|
|
|
2026-03-25 08:37:07 +01:00
|
|
|
|
|
|
|
|
# =========================
|
|
|
|
|
# START
|
|
|
|
|
# =========================
|
|
|
|
|
async def main():
|
|
|
|
|
asyncio.create_task(led.run())
|
|
|
|
|
|
|
|
|
|
# startup animation
|
|
|
|
|
await led.set_status('busy')
|
|
|
|
|
await asyncio.sleep_ms(200)
|
|
|
|
|
await led.set_status('away')
|
|
|
|
|
await asyncio.sleep_ms(300)
|
|
|
|
|
await led.set_status('available')
|
|
|
|
|
await asyncio.sleep_ms(500)
|
|
|
|
|
await led.set_status('off')
|
|
|
|
|
|
|
|
|
|
await app.start_server(port=80)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.run(main())
|