diff --git a/ESP32/main.py b/ESP32/main.py index 1433e87..e6a418e 100644 --- a/ESP32/main.py +++ b/ESP32/main.py @@ -1,5 +1,6 @@ from microdot import Microdot, send_file import machine, sys, neopixel, time +import asyncio app = Microdot() @@ -8,46 +9,65 @@ nbPixels = 12*2 pinPixelStrip = 16 # ESP32 D1 Mini neoPixelStrip = neopixel.NeoPixel(machine.Pin(pinPixelStrip), nbPixels) -# Define status colors -statusColors = { - 'BUSY': (255,0,0), # red - 'AVAILABLE': (0,255,0), # green - 'AWAY': (246,190,0), # cyan - 'OFF': (0, 0, 0), # off - 'ON': (255, 255, 255) # white - } +# Define BusyLight status +statusDef = { + 'off': {'name': 'off', 'code': 0, 'color': (0, 0, 0)}, # OFF + 'on': {'name': 'on', 'code': 1, 'color': (255, 255, 255)}, # White + 'busy': {'name': 'busy', 'code': 2, 'color': (255, 0, 0)}, # Red + 'available': {'name': 'available', 'code': 3, 'color': (0, 255, 0)}, # Green + 'away': {'name': 'away', 'code': 4, 'color': (246, 190, 0)}, # Yellow + 'blinking': {'name': 'blinking', 'code': 5}, # No defined color + 'colored': {'name': 'colored', 'code': 6} # No defined color +} # Store BusyLight default global status -blColor = statusColors.get('OFF') -blStatus = 'off' -blPreviousStatus='off' +blColor = statusDef.get('off').get('color') +blPreviousColor = statusDef.get('off').get('color') +blStatus = statusDef.get('off') +blPreviousStatus = statusDef.get('off') blBrightness = 0.1 # Adjust the brightness (0.0 - 1.0) +blBlinkingTask = None -def __setColor(color): +def __setDimmedColor(color): r, g , b = color r = int(r * blBrightness) g = int(g * blBrightness) b = int(b * blBrightness) return (r, g, b) -def __setBusyLightColor(color, brightness): +def __setBusyLightColorAndBrigthness(color, brightness): global blBrightness blBrightness = brightness + global blColor blColor = color - neoPixelStrip.fill(__setColor(color)) + dimmedColor = __setDimmedColor(color) + + neoPixelStrip.fill(dimmedColor) neoPixelStrip.write() +def __setBusyLightColored(color, brightness): + __setBusyLightColorAndBrigthness(color, brightness) global blStatus - blStatus = 'colored' + blStatus = statusDef.get('colored') -def __setBusyLightStatus(status): - status = status.upper() - color = statusColors.get(status) - __setBusyLightColor(color, blBrightness) +def __setBusyLightStatus(status = statusDef.get('off')): + if status == statusDef.get('colored'): + lColor = blColor + else: + lColor = status.get('color') + __setBusyLightColorAndBrigthness(lColor, blBrightness) global blStatus - blStatus = status.lower() + blStatus = status + +def __setBusyLightBlinking(time_ms=500): + return 0 + +def __setBusyLightStill(): + return 0 + + # Microdot APP routes @@ -80,17 +100,9 @@ async def setBrightness(request): else: return {'error': 'wrong brigthness type (float)'}, 400 - # Save blStatus - global blStatus - status = blStatus - # Apply new brightness to current color - color = blColor - __setBusyLightColor(color, brightness) - - # Restore global status - blStatus = status - + __setBusyLightColorAndBrigthness(blColor, brightness) + global blBrightness blBrightness = brightness @@ -121,32 +133,71 @@ async def setColor(request): brightness = request.json.get("brightness") - if not brightness is None: + if not bool(brightness is None): if type(brightness) is float \ or type(brightness) is int: if brightness < 0 or brightness > 1: return {'error': 'brightness out of bound (0.0 - 1.0)'}, 400 else: return {'error': 'wrong brightness type (float)'}, 400 - __setBusyLightColor(color, brightness) + __setBusyLightColored(color, brightness) - __setBusyLightColor(color, blBrightness) + __setBusyLightColored(color, blBrightness) return {'status': blStatus} +@app.post('/api/blink') +async def setBlink(request): + time_ms = request.json.get("time_ms") + blinking = request.json.get("blinking") + + if not bool(time_ms is None): + if type(time_ms) is int: + if time_ms < 100 or time_ms > 5000: + return {'error': 'parameter "time_ms" out of bound (100 - 5000)'}, 400 + else: + return {'error': 'wrong parameter "time_ms" type (int)'}, 400 + + if bool(blinking is None): + return {'error': 'missing blinking parameter'}, 400 + else: + if type(blinking) is bool: + if blinking: + if time_ms is None: + __setBusyLightBlinking() + else: + __setBusyLightBlinking(time_ms) + else: + __setBusyLightStill() + else: + return {'error': 'wrong blinking type (bool)'}, 400 + + return {'status': 'to be implemented'} + + @app.route('/api/status/', methods=['GET', 'POST']) -async def setStatus(request, status): +async def setStatus(request, status): lStatus = status.lower() if lStatus == 'on': - __setBusyLightStatus('ON') + status = statusDef.get('on') + __setBusyLightStatus(status) + elif lStatus == 'off': - __setBusyLightStatus('OFF') + status = statusDef.get('off') + __setBusyLightStatus(status) + elif lStatus == 'available': - __setBusyLightStatus('AVAILABLE') + status = statusDef.get('available') + __setBusyLightStatus(status) + elif lStatus == 'away': - __setBusyLightStatus('AWAY') + status = statusDef.get('away') + __setBusyLightStatus(status) + elif lStatus == 'busy': - __setBusyLightStatus('BUSY') + status = statusDef.get('busy') + __setBusyLightStatus(status) + else: return {'error': 'unknown /api/status/' + lStatus + ' route'}, 404 @@ -183,11 +234,11 @@ async def mutedeckWebhook(request): isVideoOn = False if isMuted: - __setBusyLightStatus('away') + __setBusyLightStatus(statusDef.get('away')) else: - __setBusyLightStatus('busy') + __setBusyLightStatus(statusDef.get('busy')) else: - __setBusyLightStatus('available') + __setBusyLightStatus(statusDef.get('available')) return {'status': blStatus} @@ -199,18 +250,17 @@ async def shutdown(request): # Startup effect def startUpSeq(): - print('Start seq begins') - __setBusyLightStatus('OFF') + print('Start seq is started') + __setBusyLightStatus(statusDef.get('off')) time.sleep_ms(100) - __setBusyLightStatus('BUSY') + __setBusyLightStatus(statusDef.get('busy')) time.sleep_ms(200) - __setBusyLightStatus('AWAY') + __setBusyLightStatus(statusDef.get('away')) time.sleep_ms(300) - __setBusyLightStatus('AVAILABLE') + __setBusyLightStatus(statusDef.get('available')) time.sleep_ms(500) - __setBusyLightStatus('OFF') + __setBusyLightStatus(statusDef.get('off')) print('Start seq is ended') - __setBusyLightColor(statusColors.get('OFF'), 0.1) startUpSeq()