code-lua-iot/encoder.lua
2024-10-12 21:53:58 +02:00

41 lines
1.1 KiB
Lua

-- écrit par Lukian Leizour le 12/10/2024
function print_pos()
enc = encoder.attach(pio.GPIO18, pio.GPIO19, pio.GPIO21, function(dir, counter, button)
print(dir, counter, button)
end)
end
function scroll()
neo = neopixel.attach(neopixel.WS2812B, pio.GPIO22, 8)
pos = 0
enc = encoder.attach(pio.GPIO18, pio.GPIO19, pio.GPIO21, function(dir, counter, button)
neo:setPixel(pos, 0, 0, 0)
if (dir == 1) then pos = (pos + 1) % 8
elseif (dir == -1) then pos = (pos - 1) % 8 end
r, g, b = wheelRGB(math.random(0, 255))
neo:setPixel(pos, r//10, g//10, b//10)
neo:update()
end)
end
function led_on_off()
on = false
enc = encoder.attach(pio.GPIO18, pio.GPIO19, pio.GPIO21, function(dir, counter, button)
if button == 1 then on = not on end
if on then pinon(pio.GPIO2) else pinoff(pio.GPIO2) end
end)
end
function led_speed()
device = pwm.attach(pio.GPIO2, 1, 0.5)
device:start()
pos = 1
enc = encoder.attach(pio.GPIO18, pio.GPIO19, pio.GPIO21, function(dir, counter, button)
if (dir == 1) then pos = pos + 1
elseif (dir == -1 and pos > 1) then pos = (pos - 1) end
device:setfreq(pos)
end)
end