added better color selection and brightness comtrol
Signed-off-by: Ebbe Baß <ebbe.bass>main
parent
cc67b2b657
commit
c549c200ca
112
code.py
112
code.py
|
@ -3,67 +3,89 @@ import rotaryio
|
||||||
import neopixel
|
import neopixel
|
||||||
import time
|
import time
|
||||||
import digitalio
|
import digitalio
|
||||||
|
import microcontroller
|
||||||
|
|
||||||
num_pixels = 10
|
num_pixels = 10
|
||||||
|
|
||||||
pixels = neopixel.NeoPixel(board.GP5, num_pixels)
|
pixels = neopixel.NeoPixel(board.GP5, num_pixels)
|
||||||
|
|
||||||
encoder = rotaryio.IncrementalEncoder(board.GP19, board.GP18)
|
encoder = rotaryio.IncrementalEncoder(board.GP19, board.GP18)
|
||||||
|
|
||||||
button = digitalio.DigitalInOut(board.GP17)
|
button = digitalio.DigitalInOut(board.GP17)
|
||||||
button.direction = digitalio.Direction.INPUT
|
button.direction = digitalio.Direction.INPUT
|
||||||
button.pull = digitalio.Pull.UP
|
button.pull = digitalio.Pull.UP
|
||||||
|
|
||||||
button_state = None # Initialize button state to None
|
# Predefined colors
|
||||||
|
colors = [
|
||||||
|
(255, 0, 0), # Red
|
||||||
|
(0, 255, 0), # Green
|
||||||
|
(0, 0, 255), # Blue
|
||||||
|
(255, 255, 255), # White
|
||||||
|
(200, 255, 200), # Cold white
|
||||||
|
(255, 20, 100), # Warm white
|
||||||
|
]
|
||||||
|
|
||||||
lamp_state = True # Initialize lamp state to True (on)
|
# Brightness levels (6 steps, including 0 for off)
|
||||||
|
brightness_levels = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
|
||||||
|
|
||||||
cold_white = (200, 255, 200)
|
# Check and initialize non-volatile memory
|
||||||
white = (255, 255, 255)
|
if len(microcontroller.nvm) < 5:
|
||||||
warm_white = (255, 20, 100)
|
microcontroller.nvm = bytearray(5) # Initialize with 5 bytes
|
||||||
|
microcontroller.nvm[0:4] = b"0.80" # Default brightness
|
||||||
|
microcontroller.nvm[4:5] = (0).to_bytes(1, "little") # Default color index
|
||||||
|
|
||||||
def interpolate_color(encoder_pos):
|
# Load settings from non-volatile memory
|
||||||
if encoder_pos <= -25:
|
try:
|
||||||
return cold_white
|
brightness_index = int(float(microcontroller.nvm[0:4].decode()) * 5)
|
||||||
elif encoder_pos >= 25:
|
color_index = int.from_bytes(microcontroller.nvm[4:5], "little")
|
||||||
return warm_white
|
if color_index >= len(colors):
|
||||||
else:
|
color_index = 0 # Reset to default if out of range
|
||||||
ratio = (encoder_pos + 25) / 50.0
|
except ValueError:
|
||||||
inverted_ratio = 1.0 - ratio
|
brightness_index = 4 # Default brightness (0.8)
|
||||||
interpolated_color = tuple(int(cold_white[i] * inverted_ratio + warm_white[i] * ratio) for i in range(3))
|
color_index = 0 # Default color index (red)
|
||||||
return interpolated_color
|
|
||||||
|
|
||||||
fading_in_progress = False # Flag to indicate if fading animation is in progress
|
|
||||||
|
|
||||||
|
pixels.brightness = brightness_levels[brightness_index]
|
||||||
|
current_color = colors[color_index]
|
||||||
|
pixels.fill(current_color)
|
||||||
|
|
||||||
|
button_state = None
|
||||||
|
last_encoder_pos = encoder.position
|
||||||
|
|
||||||
|
def fade_colors(start_color, end_color, steps=50, delay=0.02):
|
||||||
|
"""Smoothly fade between two colors."""
|
||||||
|
for step in range(steps):
|
||||||
|
ratio = step / steps
|
||||||
|
interpolated_color = tuple(
|
||||||
|
int(start_color[i] * (1 - ratio) + end_color[i] * ratio) for i in range(3)
|
||||||
|
)
|
||||||
|
pixels.fill(interpolated_color)
|
||||||
|
time.sleep(delay)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
# Read encoder position for brightness control
|
||||||
enc_pos = encoder.position
|
enc_pos = encoder.position
|
||||||
enc_pos = min(max(enc_pos, -25), 25)
|
if enc_pos != last_encoder_pos:
|
||||||
print(enc_pos)
|
step_change = enc_pos - last_encoder_pos
|
||||||
|
last_encoder_pos = enc_pos
|
||||||
# Process button press
|
brightness_index = min(max(brightness_index + step_change, 0), len(brightness_levels) - 1)
|
||||||
|
pixels.brightness = brightness_levels[brightness_index]
|
||||||
|
print(f"Brightness: {brightness_levels[brightness_index]:.2f}")
|
||||||
|
|
||||||
|
# Handle button press to cycle colors
|
||||||
if not button.value and button_state is None:
|
if not button.value and button_state is None:
|
||||||
button_state = "pressed"
|
button_state = "pressed"
|
||||||
print("pressed")
|
|
||||||
elif button.value and button_state == "pressed":
|
elif button.value and button_state == "pressed":
|
||||||
lamp_state = not lamp_state # Toggle lamp state
|
new_color_index = (color_index + 1) % len(colors)
|
||||||
button_state = None # Reset button state
|
fade_colors(colors[color_index], colors[new_color_index])
|
||||||
fading_in_progress = True # Set flag to indicate fading animation is in progress
|
color_index = new_color_index
|
||||||
|
current_color = colors[color_index] # Update current_color
|
||||||
# Perform fading animation if lamp state changed
|
pixels.fill(current_color) # Apply the new color
|
||||||
if fading_in_progress:
|
print(f"Selected color: {current_color}")
|
||||||
if lamp_state: # Fade in
|
button_state = None
|
||||||
for brightness in range(255):
|
|
||||||
pixels.brightness = brightness / 255.0
|
# Set the pixels to the current color (if not transitioning)
|
||||||
time.sleep(0.2/255)
|
if brightness_levels[brightness_index] > 0.0: # Only fill if brightness > 0
|
||||||
pixels.brightness = 1.0 # Ensure full brightness is set
|
pixels.fill(current_color)
|
||||||
else: # Fade out
|
|
||||||
for brightness in range(255, -1, -1):
|
# Save settings to non-volatile memory
|
||||||
pixels.brightness = brightness / 255.0
|
brightness_value = brightness_levels[brightness_index]
|
||||||
time.sleep(0.2/255)
|
microcontroller.nvm[0:4] = f"{brightness_value:.2f}".encode()
|
||||||
pixels.brightness = 0.0 # Ensure brightness is completely off
|
microcontroller.nvm[4:5] = color_index.to_bytes(1, "little")
|
||||||
fading_in_progress = False # Reset flag once fading animation is complete
|
|
||||||
|
|
||||||
# Calculate and set current color
|
|
||||||
current_color = interpolate_color(enc_pos)
|
|
||||||
pixels.fill(current_color)
|
|
||||||
|
|
Loading…
Reference in New Issue