112 lines
2.8 KiB
Python
112 lines
2.8 KiB
Python
import machine
|
||
import time
|
||
import neopixel
|
||
|
||
# ----------------------------
|
||
# NeoPixel SETUP
|
||
# ----------------------------
|
||
|
||
# Strip on GP17 (3 LEDs)
|
||
STRIP_PIN = 17
|
||
STRIP_LEDS = 3
|
||
strip = neopixel.NeoPixel(machine.Pin(STRIP_PIN), STRIP_LEDS)
|
||
|
||
# Single Pixel on GP28 (1 LED)
|
||
SINGLE_PIN = 28
|
||
single = neopixel.NeoPixel(machine.Pin(SINGLE_PIN), 1)
|
||
|
||
# ----------------------------
|
||
# Buttons on GP20, GP21, GP22
|
||
# ----------------------------
|
||
button_pins = [20, 21, 22]
|
||
buttons = [machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_UP) for pin in button_pins]
|
||
|
||
# ----------------------------
|
||
# 12-color palette
|
||
# ----------------------------
|
||
colors = [
|
||
(0, 0, 0), # Off
|
||
(255, 0, 0), # Red
|
||
(0, 255, 0), # Green
|
||
(0, 0, 255), # Blue
|
||
(255, 255, 0), # Yellow
|
||
(0, 255, 255), # Cyan
|
||
(255, 0, 255), # Magenta
|
||
(255, 128, 0), # Orange
|
||
(128, 0, 255), # Purple
|
||
(255, 20, 147), # Pink
|
||
(128, 255, 0), # Lime
|
||
(255, 255, 255), # White
|
||
]
|
||
|
||
# State for strip pixels (0–2)
|
||
color_index = [0, 0, 0]
|
||
brightness = [5, 5, 5]
|
||
last_state = [1, 1, 1]
|
||
press_time = [0, 0, 0]
|
||
|
||
# State for single pixel (pixel #3)
|
||
single_color_index = 1
|
||
single_brightness = 5
|
||
|
||
# ----------------------------
|
||
# Helpers
|
||
# ----------------------------
|
||
def apply_brightness(color, level):
|
||
r, g, b = color
|
||
factor = level / 100
|
||
return (int(r * factor), int(g * factor), int(b * factor))
|
||
|
||
def update_strip():
|
||
for i in range(STRIP_LEDS):
|
||
strip[i] = apply_brightness(colors[color_index[i]], brightness[i])
|
||
strip.write()
|
||
|
||
def update_single():
|
||
single[0] = apply_brightness(colors[single_color_index], single_brightness)
|
||
single.write()
|
||
|
||
# Initial setup
|
||
update_strip()
|
||
update_single()
|
||
|
||
# ----------------------------
|
||
# Main loop
|
||
# ----------------------------
|
||
while True:
|
||
now = time.ticks_ms()
|
||
|
||
# Handle buttons for pixels 0,1,2
|
||
for i in range(3):
|
||
state = buttons[i].value()
|
||
|
||
# Button pressed
|
||
if last_state[i] == 1 and state == 0:
|
||
press_time[i] = now
|
||
|
||
# Button released
|
||
if last_state[i] == 0 and state == 1:
|
||
duration = time.ticks_diff(now, press_time[i])
|
||
|
||
if duration > 500:
|
||
# LONG PRESS → brightness
|
||
brightness[i] += 10
|
||
if brightness[i] > 100:
|
||
brightness[i] = 10
|
||
else:
|
||
# SHORT PRESS → color change
|
||
color_index[i] = (color_index[i] + 1) % len(colors)
|
||
|
||
update_strip()
|
||
time.sleep_ms(200) # debounce
|
||
|
||
last_state[i] = state
|
||
|
||
# (Optional) Example automatic color change for pixel 3
|
||
# single_color_index = (single_color_index + 1) % len(colors)
|
||
# update_single()
|
||
# time.sleep(0.5)
|
||
|
||
time.sleep_ms(10)
|
||
|