概要
電子工作コンテスト、見つけたので、やってみる。
最終決定版プログラム
import array
import time
import rp2
from machine import ADC, Pin
vr_color = ADC(Pin(26))
vr_bright = ADC(Pin(27))
V_REF = 3.3
@rp2.asm_pio(sideset_init = rp2.PIO.OUT_LOW, autopull = True, pull_thresh = 24, out_shiftdir = rp2.PIO.SHIFT_LEFT)
def ws2812():
label("loop")
out(x, 1)
nop().side(1)
jmp(not_x, "tugi")
nop()
nop()
nop().side(0)
label("tugi")
nop().side(0)
nop()
jmp("loop")
sm = rp2.StateMachine(0, ws2812, freq = 5_000_000, sideset_base = Pin(17))
sm.active(1)
def main():
global sm
NUM_LEDS = 7
ar = array.array("I", [0 for _ in range(NUM_LEDS)])
last_print_time = time.ticks_ms()
print("start (Dual VR Mode - No Animation)")
while True:
raw_color = vr_color.read_u16()
raw_bright = vr_bright.read_u16()
mix = raw_color / 65535.0
brightness = raw_bright / 65535.0
current_time = time.ticks_ms()
if time.ticks_diff(current_time, last_print_time) >= 1000:
print(f"VR1(Color): {mix:.2f} | VR2(Bright): {brightness:.2f}")
last_print_time = current_time
pos = int(mix * 299)
if pos < 100:
g_base = 100 - pos
b_base = pos
r_base = 0
elif pos < 200:
pos_sub = pos - 100
g_base = 0
b_base = 100 - pos_sub
r_base = pos_sub
else:
pos_sub = pos - 200
g_base = pos_sub
b_base = 0
r_base = 100 - pos_sub
r = int(r_base * brightness)
g = int(g_base * brightness)
b = int(b_base * brightness)
r >>= 1
g >>= 1
b >>= 1
led_value = (r << 16) | (g << 8) | b
for j in range(NUM_LEDS):
ar[j] = led_value
sm.put(ar, 8)
time.sleep_us(100)
time.sleep_ms(30)
if __name__ == "__main__":
main()
以上。