Compare commits
1 commit
main
...
igox/blink
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0f1d9b79c |
1 changed files with 99 additions and 49 deletions
142
ESP32/main.py
142
ESP32/main.py
|
|
@ -1,5 +1,6 @@
|
||||||
from microdot import Microdot, send_file
|
from microdot import Microdot, send_file
|
||||||
import machine, sys, neopixel, time
|
import machine, sys, neopixel, time
|
||||||
|
import asyncio
|
||||||
|
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
|
|
||||||
|
|
@ -8,46 +9,65 @@ nbPixels = 12*2
|
||||||
pinPixelStrip = 16 # ESP32 D1 Mini
|
pinPixelStrip = 16 # ESP32 D1 Mini
|
||||||
neoPixelStrip = neopixel.NeoPixel(machine.Pin(pinPixelStrip), nbPixels)
|
neoPixelStrip = neopixel.NeoPixel(machine.Pin(pinPixelStrip), nbPixels)
|
||||||
|
|
||||||
# Define status colors
|
# Define BusyLight status
|
||||||
statusColors = {
|
statusDef = {
|
||||||
'BUSY': (255,0,0), # red
|
'off': {'name': 'off', 'code': 0, 'color': (0, 0, 0)}, # OFF
|
||||||
'AVAILABLE': (0,255,0), # green
|
'on': {'name': 'on', 'code': 1, 'color': (255, 255, 255)}, # White
|
||||||
'AWAY': (246,190,0), # cyan
|
'busy': {'name': 'busy', 'code': 2, 'color': (255, 0, 0)}, # Red
|
||||||
'OFF': (0, 0, 0), # off
|
'available': {'name': 'available', 'code': 3, 'color': (0, 255, 0)}, # Green
|
||||||
'ON': (255, 255, 255) # white
|
'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
|
# Store BusyLight default global status
|
||||||
blColor = statusColors.get('OFF')
|
blColor = statusDef.get('off').get('color')
|
||||||
blStatus = 'off'
|
blPreviousColor = statusDef.get('off').get('color')
|
||||||
blPreviousStatus='off'
|
blStatus = statusDef.get('off')
|
||||||
|
blPreviousStatus = statusDef.get('off')
|
||||||
blBrightness = 0.1 # Adjust the brightness (0.0 - 1.0)
|
blBrightness = 0.1 # Adjust the brightness (0.0 - 1.0)
|
||||||
|
blBlinkingTask = None
|
||||||
|
|
||||||
def __setColor(color):
|
def __setDimmedColor(color):
|
||||||
r, g , b = color
|
r, g , b = color
|
||||||
r = int(r * blBrightness)
|
r = int(r * blBrightness)
|
||||||
g = int(g * blBrightness)
|
g = int(g * blBrightness)
|
||||||
b = int(b * blBrightness)
|
b = int(b * blBrightness)
|
||||||
return (r, g, b)
|
return (r, g, b)
|
||||||
|
|
||||||
def __setBusyLightColor(color, brightness):
|
def __setBusyLightColorAndBrigthness(color, brightness):
|
||||||
global blBrightness
|
global blBrightness
|
||||||
blBrightness = brightness
|
blBrightness = brightness
|
||||||
|
|
||||||
global blColor
|
global blColor
|
||||||
blColor = color
|
blColor = color
|
||||||
neoPixelStrip.fill(__setColor(color))
|
dimmedColor = __setDimmedColor(color)
|
||||||
|
|
||||||
|
neoPixelStrip.fill(dimmedColor)
|
||||||
neoPixelStrip.write()
|
neoPixelStrip.write()
|
||||||
|
|
||||||
|
def __setBusyLightColored(color, brightness):
|
||||||
|
__setBusyLightColorAndBrigthness(color, brightness)
|
||||||
global blStatus
|
global blStatus
|
||||||
blStatus = 'colored'
|
blStatus = statusDef.get('colored')
|
||||||
|
|
||||||
def __setBusyLightStatus(status):
|
def __setBusyLightStatus(status = statusDef.get('off')):
|
||||||
status = status.upper()
|
if status == statusDef.get('colored'):
|
||||||
color = statusColors.get(status)
|
lColor = blColor
|
||||||
__setBusyLightColor(color, blBrightness)
|
else:
|
||||||
|
lColor = status.get('color')
|
||||||
|
__setBusyLightColorAndBrigthness(lColor, blBrightness)
|
||||||
|
|
||||||
global blStatus
|
global blStatus
|
||||||
blStatus = status.lower()
|
blStatus = status
|
||||||
|
|
||||||
|
def __setBusyLightBlinking(time_ms=500):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def __setBusyLightStill():
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Microdot APP routes
|
# Microdot APP routes
|
||||||
|
|
||||||
|
|
@ -80,16 +100,8 @@ async def setBrightness(request):
|
||||||
else:
|
else:
|
||||||
return {'error': 'wrong brigthness type (float)'}, 400
|
return {'error': 'wrong brigthness type (float)'}, 400
|
||||||
|
|
||||||
# Save blStatus
|
|
||||||
global blStatus
|
|
||||||
status = blStatus
|
|
||||||
|
|
||||||
# Apply new brightness to current color
|
# Apply new brightness to current color
|
||||||
color = blColor
|
__setBusyLightColorAndBrigthness(blColor, brightness)
|
||||||
__setBusyLightColor(color, brightness)
|
|
||||||
|
|
||||||
# Restore global status
|
|
||||||
blStatus = status
|
|
||||||
|
|
||||||
global blBrightness
|
global blBrightness
|
||||||
blBrightness = brightness
|
blBrightness = brightness
|
||||||
|
|
@ -121,32 +133,71 @@ async def setColor(request):
|
||||||
|
|
||||||
brightness = request.json.get("brightness")
|
brightness = request.json.get("brightness")
|
||||||
|
|
||||||
if not brightness is None:
|
if not bool(brightness is None):
|
||||||
if type(brightness) is float \
|
if type(brightness) is float \
|
||||||
or type(brightness) is int:
|
or type(brightness) is int:
|
||||||
if brightness < 0 or brightness > 1:
|
if brightness < 0 or brightness > 1:
|
||||||
return {'error': 'brightness out of bound (0.0 - 1.0)'}, 400
|
return {'error': 'brightness out of bound (0.0 - 1.0)'}, 400
|
||||||
else:
|
else:
|
||||||
return {'error': 'wrong brightness type (float)'}, 400
|
return {'error': 'wrong brightness type (float)'}, 400
|
||||||
__setBusyLightColor(color, brightness)
|
__setBusyLightColored(color, brightness)
|
||||||
|
|
||||||
__setBusyLightColor(color, blBrightness)
|
__setBusyLightColored(color, blBrightness)
|
||||||
|
|
||||||
return {'status': blStatus}
|
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/<status>', methods=['GET', 'POST'])
|
@app.route('/api/status/<status>', methods=['GET', 'POST'])
|
||||||
async def setStatus(request, status):
|
async def setStatus(request, status):
|
||||||
lStatus = status.lower()
|
lStatus = status.lower()
|
||||||
if lStatus == 'on':
|
if lStatus == 'on':
|
||||||
__setBusyLightStatus('ON')
|
status = statusDef.get('on')
|
||||||
|
__setBusyLightStatus(status)
|
||||||
|
|
||||||
elif lStatus == 'off':
|
elif lStatus == 'off':
|
||||||
__setBusyLightStatus('OFF')
|
status = statusDef.get('off')
|
||||||
|
__setBusyLightStatus(status)
|
||||||
|
|
||||||
elif lStatus == 'available':
|
elif lStatus == 'available':
|
||||||
__setBusyLightStatus('AVAILABLE')
|
status = statusDef.get('available')
|
||||||
|
__setBusyLightStatus(status)
|
||||||
|
|
||||||
elif lStatus == 'away':
|
elif lStatus == 'away':
|
||||||
__setBusyLightStatus('AWAY')
|
status = statusDef.get('away')
|
||||||
|
__setBusyLightStatus(status)
|
||||||
|
|
||||||
elif lStatus == 'busy':
|
elif lStatus == 'busy':
|
||||||
__setBusyLightStatus('BUSY')
|
status = statusDef.get('busy')
|
||||||
|
__setBusyLightStatus(status)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return {'error': 'unknown /api/status/' + lStatus + ' route'}, 404
|
return {'error': 'unknown /api/status/' + lStatus + ' route'}, 404
|
||||||
|
|
||||||
|
|
@ -183,11 +234,11 @@ async def mutedeckWebhook(request):
|
||||||
isVideoOn = False
|
isVideoOn = False
|
||||||
|
|
||||||
if isMuted:
|
if isMuted:
|
||||||
__setBusyLightStatus('away')
|
__setBusyLightStatus(statusDef.get('away'))
|
||||||
else:
|
else:
|
||||||
__setBusyLightStatus('busy')
|
__setBusyLightStatus(statusDef.get('busy'))
|
||||||
else:
|
else:
|
||||||
__setBusyLightStatus('available')
|
__setBusyLightStatus(statusDef.get('available'))
|
||||||
|
|
||||||
return {'status': blStatus}
|
return {'status': blStatus}
|
||||||
|
|
||||||
|
|
@ -199,18 +250,17 @@ async def shutdown(request):
|
||||||
|
|
||||||
# Startup effect
|
# Startup effect
|
||||||
def startUpSeq():
|
def startUpSeq():
|
||||||
print('Start seq begins')
|
print('Start seq is started')
|
||||||
__setBusyLightStatus('OFF')
|
__setBusyLightStatus(statusDef.get('off'))
|
||||||
time.sleep_ms(100)
|
time.sleep_ms(100)
|
||||||
__setBusyLightStatus('BUSY')
|
__setBusyLightStatus(statusDef.get('busy'))
|
||||||
time.sleep_ms(200)
|
time.sleep_ms(200)
|
||||||
__setBusyLightStatus('AWAY')
|
__setBusyLightStatus(statusDef.get('away'))
|
||||||
time.sleep_ms(300)
|
time.sleep_ms(300)
|
||||||
__setBusyLightStatus('AVAILABLE')
|
__setBusyLightStatus(statusDef.get('available'))
|
||||||
time.sleep_ms(500)
|
time.sleep_ms(500)
|
||||||
__setBusyLightStatus('OFF')
|
__setBusyLightStatus(statusDef.get('off'))
|
||||||
print('Start seq is ended')
|
print('Start seq is ended')
|
||||||
__setBusyLightColor(statusColors.get('OFF'), 0.1)
|
|
||||||
|
|
||||||
startUpSeq()
|
startUpSeq()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue