added updated rgb code

This commit is contained in:
Johannes Erwerle 2025-04-16 19:56:08 +02:00
parent bd97c8ba96
commit bea1a50bf1

View file

@ -6,37 +6,13 @@ from time import sleep
machine.freq(240000000) # set the CPU frequency to 240 MHz
PIXELPIN = Pin(20, Pin.OUT)
NUM_PIXELS = 16
NUM_PIXELS = 40
ADC0 = ADC(Pin(26))
ADC1 = ADC(Pin(27))
ADC2 = ADC(Pin(28))
def hsv_to_rgb(hsv) -> tuple[float, float, float]:
H, S, V = hsv
h_i = int(H * 6)
f = H * 6 - h_i
p = V * (1 - S)
q = V * (1 - S * f)
t = V * (1 - S * (1 - f))
if h_i == 0 or h_i == 6:
return (V, t, p)
if h_i == 1:
return (q, V, p)
if h_i == 2:
return (p, V, t)
if h_i == 3:
return (p, q, V)
if h_i == 4:
return (t, p, V)
# implicit case 5
return (V, p, q)
def read_adc_float(adc: ADC) -> float:
value = adc.read_u16()
return value / 2**16
@ -79,32 +55,38 @@ def denoise(value: float, factor: float = 0.05) -> float:
return max(0.0, min(1.0, (value - factor) / (1 - 2 * factor)))
def gate(initial: float, new: float, gate: float) -> float:
"""
needs a minimum amount of change (given as gate) between the initial and the new value for the output to change.
"""
if abs(initial - new) > gate:
return new
return initial
np = NeoPixel(PIXELPIN, NUM_PIXELS, bpp=4)
ww = 0.0
hue = 0.0
value = 0.0
r = 0.0
g = 0.0
b = 0.0
dampening_factor = 0.25
while True:
ww = (1.0 - dampening_factor) * ww + dampening_factor * read_adc_float(ADC0)
hue = (1.0 - dampening_factor) * hue + dampening_factor * read_adc_float(ADC1)
value = (1.0 - dampening_factor) * value + dampening_factor * read_adc_float(ADC2)
hsv = (denoise(hue), 1.0, denoise(value))
(r, g, b) = hsv_to_rgb(hsv)
r = dampening_factor * gate(r, denoise(1-read_adc_float(ADC0)), 4.0/512) + (1-dampening_factor) * r
g = dampening_factor * gate(g, denoise(1-read_adc_float(ADC1)), 4.0/512) + (1-dampening_factor) * g
b = dampening_factor * gate(b, denoise(1-read_adc_float(ADC2)), 4.0/512) + (1-dampening_factor) * b
for i in range(0, NUM_PIXELS):
np[i] = (
float_to_u8(r),
float_to_u8(g),
float_to_u8(b),
float_to_u8(denoise(ww)),
float_to_u8(0.0),
)
np.write()
print(float_to_u8(ww), float_to_u8(hue), float_to_u8(value))
print(float_to_u8(r), float_to_u8(g), float_to_u8(b))
sleep(0.01)