-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightness.py
36 lines (26 loc) · 932 Bytes
/
brightness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import uasyncio as asyncio
from cosmic import CosmicUnicorn
TRANSITION = 1.0 / 72.0
cu = CosmicUnicorn()
def map_range(x):
min_input = 10
max_input = 130
min_output = 0.1
max_output = 1
return (x - min_input) * (max_output - min_output) / (max_input - min_input) + min_output
def calculate_brightness(prev_brightness_val):
current_lsv = cu.light()
current_brightness_val = map_range(current_lsv)
brightness_diff = current_brightness_val - prev_brightness_val
brightness_val = prev_brightness_val + (brightness_diff * TRANSITION)
if brightness_val > 1:
brightness_val = 1
elif brightness_val < 0.1:
brightness_val = 0.1
return brightness_val
async def brightness_task(device):
brightness_val = 0.1
while True:
brightness_val = calculate_brightness(brightness_val)
device.set_brightness(brightness_val)
await asyncio.sleep(0.1)