From 52c504769941fbcfe191c723d3adf7a59f7fb608 Mon Sep 17 00:00:00 2001 From: Lukian Date: Sat, 12 Oct 2024 21:53:58 +0200 Subject: [PATCH] commit --- autorun.lua | 5 ++- encoder.lua | 41 +++++++++++++++++++++++ main.lua | 2 ++ neopixel.lua | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ out.lua | 34 +++++++++++++------ pwm.lua | 35 ++++++++++++++++++++ 6 files changed, 199 insertions(+), 11 deletions(-) create mode 100644 encoder.lua create mode 100644 neopixel.lua create mode 100644 pwm.lua diff --git a/autorun.lua b/autorun.lua index bd6fb78..7eb3cfe 100644 --- a/autorun.lua +++ b/autorun.lua @@ -1,7 +1,10 @@ -- charge le fichier principal dofile("main.lua") +dofile("neopixel.lua") +dofile("encoder.lua") -- affiche la température du CPU printTemp() - +-- affiche le résultat du dé sur le neopixel +-- dice() diff --git a/encoder.lua b/encoder.lua new file mode 100644 index 0000000..e8f27cb --- /dev/null +++ b/encoder.lua @@ -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 diff --git a/main.lua b/main.lua index 97e19b5..a56b80e 100755 --- a/main.lua +++ b/main.lua @@ -1,5 +1,7 @@ -- charge les fonctions du fichier out.lua dofile("out.lua") +dofile("neopixel.lua") +dofile("encoder.lua") -- fonction permettant d'afficher la température du CPU function printTemp() diff --git a/neopixel.lua b/neopixel.lua new file mode 100644 index 0000000..7c4480d --- /dev/null +++ b/neopixel.lua @@ -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 diff --git a/out.lua b/out.lua index 05a5ea5..0f0ed78 100644 --- a/out.lua +++ b/out.lua @@ -57,21 +57,35 @@ function smooth() tmr.delayms(200) i = i - 0.05 end - device:stop()² + device:stop() +end - red:start() - green:start() - blue:start() +function rgb(r, g, b) + 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() - green:stop() - blue:stop() + red:stop(); red:detach() + green:stop(); green:detach() + blue:stop(); blue:detach() +end - red:detach() - green:detach() - blue:detach() +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 -- fonction permettant d'allumer un buzzer pendant un temps donné diff --git a/pwm.lua b/pwm.lua new file mode 100644 index 0000000..935c9dd --- /dev/null +++ b/pwm.lua @@ -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 \ No newline at end of file