Merge branch 'master' into 'main'
Master See merge request lucien/code-lua-iot!2
This commit is contained in:
commit
07bcae8f43
6 changed files with 234 additions and 7 deletions
|
@ -1,7 +1,10 @@
|
||||||
-- charge le fichier principal
|
-- charge le fichier principal
|
||||||
dofile("main.lua")
|
dofile("main.lua")
|
||||||
|
dofile("neopixel.lua")
|
||||||
|
dofile("encoder.lua")
|
||||||
|
|
||||||
-- affiche la température du CPU
|
-- affiche la température du CPU
|
||||||
printTemp()
|
printTemp()
|
||||||
|
|
||||||
|
-- affiche le résultat du dé sur le neopixel
|
||||||
|
-- dice()
|
||||||
|
|
41
encoder.lua
Normal file
41
encoder.lua
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
-- é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
|
2
main.lua
2
main.lua
|
@ -1,5 +1,7 @@
|
||||||
-- charge les fonctions du fichier out.lua
|
-- charge les fonctions du fichier out.lua
|
||||||
dofile("out.lua")
|
dofile("out.lua")
|
||||||
|
dofile("neopixel.lua")
|
||||||
|
dofile("encoder.lua")
|
||||||
|
|
||||||
-- fonction permettant d'afficher la température du CPU
|
-- fonction permettant d'afficher la température du CPU
|
||||||
function printTemp()
|
function printTemp()
|
||||||
|
|
93
neopixel.lua
Normal file
93
neopixel.lua
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
-- écrit le 12/10/2024 par Lukian Leizour
|
||||||
|
|
||||||
|
function feux()
|
||||||
|
neo = neopixel.attach(neopixel.WS2812B, pio.GPIO18, 8)
|
||||||
|
|
||||||
|
while true do
|
||||||
|
neo:setPixel(7, 255, 0, 0)
|
||||||
|
neo:update()
|
||||||
|
|
||||||
|
tmr.delayms(3000)
|
||||||
|
|
||||||
|
neo:setPixel(7, 0, 0, 0)
|
||||||
|
neo:setPixel(5, 0, 255, 0)
|
||||||
|
neo:update()
|
||||||
|
|
||||||
|
tmr.delayms(3000)
|
||||||
|
|
||||||
|
neo:setPixel(5, 0, 0, 0)
|
||||||
|
neo:setPixel(6, 255, 255, 0)
|
||||||
|
neo:update()
|
||||||
|
|
||||||
|
tmr.delayms(1000)
|
||||||
|
|
||||||
|
neo:setPixel(6, 0, 0, 0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function wheelRGB(pos)
|
||||||
|
pos = 255 - pos
|
||||||
|
if (pos < 85) then
|
||||||
|
return 255 - pos * 3, 0, pos * 3
|
||||||
|
elseif (pos < 170) then
|
||||||
|
pos = pos - 85
|
||||||
|
return 0, pos * 3, 255 - pos * 3
|
||||||
|
else
|
||||||
|
pos = pos - 170
|
||||||
|
return pos * 3, 255 - pos * 3, 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function arc()
|
||||||
|
neo = neopixel.attach(neopixel.WS2812B, pio.GPIO18, 8)
|
||||||
|
for i=0,7 do
|
||||||
|
r, g, b = wheelRGB((255*i) // 8)
|
||||||
|
neo:setPixel(i, r//10, g//10, b//10)
|
||||||
|
end
|
||||||
|
neo:update()
|
||||||
|
end
|
||||||
|
|
||||||
|
function arc_move()
|
||||||
|
neo = neopixel.attach(neopixel.WS2812B, pio.GPIO18, 8)
|
||||||
|
pixel = 0
|
||||||
|
direction = 0
|
||||||
|
|
||||||
|
while true do
|
||||||
|
r, g, b = wheelRGB((255*pixel) // 8)
|
||||||
|
neo:setPixel(pixel, r, g, b)
|
||||||
|
neo:update()
|
||||||
|
tmr.delayms(100)
|
||||||
|
neo:setPixel(pixel, r//10, g//10, b//10)
|
||||||
|
if (direction == 0) then
|
||||||
|
if (pixel == 7) then
|
||||||
|
direction = 1; pixel = 6
|
||||||
|
else
|
||||||
|
pixel = pixel + 1
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if (pixel == 0) then
|
||||||
|
direction = 0; pixel = 1
|
||||||
|
else
|
||||||
|
pixel = pixel - 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function fete()
|
||||||
|
neo = neopixel.attach(neopixel.WS2812B, pio.GPIO18, 8)
|
||||||
|
while true do
|
||||||
|
r, g, b = wheelRGB(math.random(0, 255))
|
||||||
|
neo:setPixel(math.random(0, 7), r, g, b)
|
||||||
|
neo:update()
|
||||||
|
tmr.delayms(50)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function dice() -- on note que le dé renvoie toujours la même valeur étant donné qu'il est appelé au démarage et que math.random() a comme seed l'uptime de l'ESP32
|
||||||
|
for i=0,math.random(0, 7) do
|
||||||
|
r, g, b = wheelRGB((255*i) // 8)
|
||||||
|
neo:setPixel(i, r, g, b)
|
||||||
|
neo:update()
|
||||||
|
end
|
||||||
|
end
|
65
out.lua
65
out.lua
|
@ -23,16 +23,69 @@ function blink (pin, n, delay) -- pin : la patte de sortie; n : int, nombre d'it
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function blink_pwm(pin, n, delay)
|
-- fonction permettant de faire clignoter la led sur la pin 18 4 fois brièvement
|
||||||
print(1 / (delay * 2 / 1000), 0.5)
|
function blink_pwm1()
|
||||||
device = pwm.attach(pin, math.floor(1 / (delay * 2 / 1000)), 0.5)
|
device = pwm.attach(pio.GPIO18, 1, 0.2) -- 1Hz et 20% de signal "haut"
|
||||||
|
device:start() -- départ
|
||||||
|
tmr.delayms(4000) -- on attend que la led ait finit de clignoter
|
||||||
|
device:stop() -- on arrête le signal
|
||||||
|
device:detach() -- on se détache de la pin
|
||||||
|
end
|
||||||
|
|
||||||
|
-- fonction permettant de faire clignoter la led sur la pin 18 4 fois longuement
|
||||||
|
function blink_pwm2()
|
||||||
|
device = pwm.attach(pio.GPIO18, 1, 0.8) -- 1Hz et 80% de signal "haut"
|
||||||
|
device:start() -- départ
|
||||||
|
tmr.delayms(4000) -- on attend que la led ait finit de clignoter
|
||||||
|
device:stop() -- on arrête le signal
|
||||||
|
device:detach() -- on se détache de la pin
|
||||||
|
end
|
||||||
|
|
||||||
|
-- fonction permettant d'allumer et étteindre progressivement une led sur la pin 18
|
||||||
|
function smooth()
|
||||||
|
device = pwm.attach(pio.GPIO18, 200, 0)
|
||||||
device:start()
|
device:start()
|
||||||
tmr.delayms(n * delay)
|
i = 0
|
||||||
|
while i <= 1 do
|
||||||
|
device:setduty(i)
|
||||||
|
tmr.delayms(200)
|
||||||
|
i = i + 0.05
|
||||||
|
end
|
||||||
|
i = 1.
|
||||||
|
while i >= 0 do
|
||||||
|
device:setduty(i)
|
||||||
|
tmr.delayms(200)
|
||||||
|
i = i - 0.05
|
||||||
|
end
|
||||||
device:stop()
|
device:stop()
|
||||||
end
|
end
|
||||||
|
|
||||||
function test(n, delay)
|
function rgb(r, g, b)
|
||||||
blink_pwm(pio.GPIO18, n, delay)
|
red = pwm.attach(pio.GPIO21, 2000, r / 256); red:start()
|
||||||
|
green = pwm.attach(pio.GPIO19, 2000, g / 256); green:start()
|
||||||
|
blue = pwm.attach(pio.GPIO18, 2000, b / 256); blue:start()
|
||||||
|
|
||||||
|
tmr.delayms(5000)
|
||||||
|
|
||||||
|
red:stop(); red:detach()
|
||||||
|
green:stop(); green:detach()
|
||||||
|
blue:stop(); blue:detach()
|
||||||
|
end
|
||||||
|
|
||||||
|
function allrgb()
|
||||||
|
red = pwm.attach(pio.GPIO21, 2000, 1.); red:start()
|
||||||
|
green = pwm.attach(pio.GPIO19, 2000, 0); green:start()
|
||||||
|
blue = pwm.attach(pio.GPIO18, 2000, 0); blue:start()
|
||||||
|
|
||||||
|
for i=0,255,5 do green:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
for i=255,0,-5 do red:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
for i=0,255,5 do blue:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
for i=255,0,-5 do green:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
for i=0,255,5 do red:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
|
||||||
|
red:stop(); red:detach()
|
||||||
|
green:stop(); green:detach()
|
||||||
|
blue:stop(); blue:detach()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- fonction permettant d'allumer un buzzer pendant un temps donné
|
-- fonction permettant d'allumer un buzzer pendant un temps donné
|
||||||
|
|
35
pwm.lua
Normal file
35
pwm.lua
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
-- écrit le 12/10/2024 par Lukian Leizour
|
||||||
|
|
||||||
|
function rgb(r, g, b)
|
||||||
|
-- on attache et démarre les pins
|
||||||
|
red = pwm.attach(pio.GPIO21, 2000, r / 256); red:start()
|
||||||
|
green = pwm.attach(pio.GPIO19, 2000, g / 256); green:start()
|
||||||
|
blue = pwm.attach(pio.GPIO18, 2000, b / 256); blue:start()
|
||||||
|
|
||||||
|
-- on attend 5 secondes
|
||||||
|
tmr.delayms(5000)
|
||||||
|
|
||||||
|
-- on stoppe et détache les pins
|
||||||
|
red:stop(); red:detach()
|
||||||
|
green:stop(); green:detach()
|
||||||
|
blue:stop(); blue:detach()
|
||||||
|
end
|
||||||
|
|
||||||
|
function allrgb()
|
||||||
|
-- on attache et démarre les pins
|
||||||
|
red = pwm.attach(pio.GPIO21, 2000, 1.); red:start()
|
||||||
|
green = pwm.attach(pio.GPIO19, 2000, 0); green:start()
|
||||||
|
blue = pwm.attach(pio.GPIO18, 2000, 0); blue:start()
|
||||||
|
|
||||||
|
-- on boucle pour afficher toutes les couleurs RGB
|
||||||
|
for i=0,255,5 do green:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
for i=255,0,-5 do red:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
for i=0,255,5 do blue:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
for i=255,0,-5 do green:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
for i=0,255,5 do red:setduty(i / 256); tmr.delayms(50) end
|
||||||
|
|
||||||
|
-- on stoppe et détache les pins
|
||||||
|
red:stop(); red:detach()
|
||||||
|
green:stop(); green:detach()
|
||||||
|
blue:stop(); blue:detach()
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue