Skip to content

Commit

Permalink
Respect user defined settings from config file (Fixes #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwohlbruck committed May 1, 2022
1 parent eba0cf0 commit cd51316
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 86 deletions.
175 changes: 116 additions & 59 deletions micro/app/commander.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,139 @@
from app.config import reset_config
from app.config import reset_config, add_config, load_config
from machine import reset
from app.led import set_color, turn_off, set, get_color_gradient, rgb_to_hue, hex_to_rgb, polylinear_gradient, rgb_to_hex
from app.led import set_color, turn_off, set, get_color_gradient, rgb_to_hue, hex_to_rgb, polylinear_gradient, rgb_to_hex, kelvin_to_rgb
from app.server import start_server

reading_light_on = False
BRIGHTNESS = 1
NIGHT_MODE = True
MINIMUM_LIGHT_LEVEL = 0.5
READING_LIGHT_COLOR_TEMPERATURE = 6000

server = None
reading_light_on = False

def on_message(name, data):
# TODO: Move this switch to a dict outside
if name == 'GROUP_STATE_CHANGED':
pulse_received(data)

if name == 'FACTORY_RESET':
factory_reset()

# TODO: Move this switch to a dict outside
if name == 'GROUP_STATE_CHANGED':
pulse_received(data)

if name == 'FACTORY_RESET':
factory_reset()

if name == 'REQUEST_CONFIG':
server.send_config()

if name == 'UPDATE_CONFIG':
update_config(data)

def start_commander():
global server
server = start_server()
server.subscribe(on_message)
global BRIGHTNESS
global NIGHT_MODE
global MINIMUM_LIGHT_LEVEL
global READING_LIGHT_COLOR_TEMPERATURE

config = load_config()
if config:
BRIGHTNESS = config.get('brightness', BRIGHTNESS)
NIGHT_MODE = config.get('nightMode', NIGHT_MODE)
MINIMUM_LIGHT_LEVEL = config.get('minimumLightLevel', MINIMUM_LIGHT_LEVEL)
READING_LIGHT_COLOR_TEMPERATURE = config.get('readingLightColorTemperature', READING_LIGHT_COLOR_TEMPERATURE)

global server
server = start_server()
server.subscribe(on_message)


def activate(color):
if (server):
server.send_lamp_command(rgb_to_hex(*color), True)
if (server):
server.send_lamp_command(rgb_to_hex(*color), True)

def deactivate(color=(0,0,0)):
if (server):
server.send_lamp_command(rgb_to_hex(*color), False)
if (server):
server.send_lamp_command(rgb_to_hex(*color), False)

def turn_on_reading_light():
print('on')
global reading_light_on
reading_light_on = True
set_color((255,255,255))
global READING_LIGHT_COLOR_TEMPERATURE
print('on')
global reading_light_on
reading_light_on = True
print(READING_LIGHT_COLOR_TEMPERATURE)
print(kelvin_to_rgb(READING_LIGHT_COLOR_TEMPERATURE))
set_color(kelvin_to_rgb(READING_LIGHT_COLOR_TEMPERATURE))

def turn_off_reading_light():
print('off')
global reading_light_on
reading_light_on = False
turn_off()
print('off')
global reading_light_on
reading_light_on = False
turn_off()

def toggle_reading_light():
global reading_light_on
if reading_light_on:
turn_off_reading_light()
else:
turn_on_reading_light()
global reading_light_on
if reading_light_on:
turn_off_reading_light()
else:
turn_on_reading_light()

def factory_reset():
reset_config()
reset()
reset_config()
reset()

def state_from_color_list(colors, brightness=None):
if len(colors) == 1:
colors = get_color_gradient(rgb_to_hue(*hex_to_rgb(colors[0])))
else:
colors = [hex_to_rgb(c) for c in colors]

return polylinear_gradient(None, colors, brightness=brightness)


if len(colors) == 1:
colors = get_color_gradient(rgb_to_hue(*hex_to_rgb(colors[0])))
else:
colors = [hex_to_rgb(c) for c in colors]

return polylinear_gradient(None, colors, brightness=brightness)

def pulse_received(data):
state = data.get('state', None)
active = state.get('active', None)

if state == None and active == None:
return

colors = state.get('colors')
if active:
# effect = start_effect('rotate', colors=state.get('colors'))
state = state_from_color_list(colors)
set(state)

else:
# stop_effect(effect)
global reading_light_on
if reading_light_on:
turn_on_reading_light(colors)
else:
state = state_from_color_list(colors, .03)
set(state)
state = data.get('state', None)
active = state.get('active', None)

if state == None and active == None:
return

colors = state.get('colors')
if active:
# effect = start_effect('rotate', colors=state.get('colors'))
state = state_from_color_list(colors)
set(state)

else:
# stop_effect(effect)
global reading_light_on
if reading_light_on:
turn_on_reading_light(colors)
else:
state = state_from_color_list(colors, .03)
set(state)

def update_config(config):
global BRIGHTNESS
global NIGHT_MODE
global MINIMUM_LIGHT_LEVEL
global READING_LIGHT_COLOR_TEMPERATURE

brightness = config.get('brightness', None)
night_mode = config.get('nightMode', None)
minimum_light_level = config.get('minimumLightLevel', None)
reading_light_color_temperature = config.get('readingLightColorTemperature', None)

if brightness is not None:
add_config('brightness', brightness)
BRIGHTNESS = brightness

if night_mode is not None:
add_config('nightMode', night_mode)
NIGHT_MODE = night_mode

if minimum_light_level is not None:
add_config('minimumLightLevel', minimum_light_level)
MINIMUM_LIGHT_LEVEL = minimum_light_level

if reading_light_color_temperature is not None:
add_config('readingLightColorTemperature', reading_light_color_temperature)
READING_LIGHT_COLOR_TEMPERATURE = reading_light_color_temperature

if reading_light_on:
turn_on_reading_light()
26 changes: 25 additions & 1 deletion micro/app/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from neopixel import NeoPixel
from time import sleep_ms, ticks_us, ticks_ms, ticks_diff
from _thread import start_new_thread
from math import floor, radians, cos, pi
from math import floor, radians, cos, pi, log
from random import random
from gc import collect

Expand Down Expand Up @@ -287,6 +287,30 @@ def hsl_to_rgb(h, s, l):
else:
r, g, b = c, 0, x
return tuple(int(255 * (r + m)) for r in (r, g, b))

# Convert color temp in Kelvin to rgb
# TODO: The colors that this outputs look like shit. Fix it later.
def kelvin_to_rgb(kelvin):
temp = kelvin / 100
r = 0
g = 0
b = 0
if temp <= 66:
r = 255
g = temp
g = 99.4708025861 * log(g) - 161.1195681661
if temp <= 19:
b = 0
else:
b = temp - 10
b = 138.5177312231 * log(b) - 305.0447927307
else:
r = temp - 60
r = 329.698727446 * pow(r, -0.1332047592)
g = temp - 60
g = 288.1221695283 * pow(g, -0.0755148492)
b = 255
return (r, g, b)


# TODO: make n default None
Expand Down
27 changes: 1 addition & 26 deletions micro/app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
from time import sleep_ms
import app.uwebsockets.client as wsclient
from app.config import get_device_id, get_config_item, load_config, add_config
from app.config import get_device_id, get_config_item, load_config
from app.wifi import disconnect_wifi

MAX_RECONNECT_ATTEMPTS = 5
Expand Down Expand Up @@ -81,12 +81,6 @@ def on_message(self, message):

print('Got ' + name)

if name == 'REQUEST_CONFIG':
self.send_config()

if name == 'UPDATE_CONFIG':
self.update_config(data)

for callback in self.callbacks:
callback(name, data)

Expand All @@ -113,25 +107,6 @@ def send_config(self):
'data': config
})

def update_config(self, config):
brightness = config.get('brightness', None)
night_mode = config.get('nightMode', None)
minimum_light_level = config.get('minimumLightLevel', None)
reading_light_color_temperature = config.get('readingLightColorTemperature', None)

if brightness is not None:
add_config('brightness', brightness)

if night_mode is not None:
add_config('nightMode', night_mode)

if minimum_light_level is not None:
add_config('minimumLightLevel', minimum_light_level)

if reading_light_color_temperature is not None:
add_config('readingLightColorTemperature', reading_light_color_temperature)


def start_server():
try:
lamp_id = get_config_item('lampId')
Expand Down

0 comments on commit cd51316

Please sign in to comment.