From af9db046cf5162d5043288c79b29bd2c74bd6027 Mon Sep 17 00:00:00 2001 From: Lukian Date: Thu, 23 Jan 2025 15:50:19 +0100 Subject: [PATCH] added two states for the player --- letter_display.lua | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/letter_display.lua b/letter_display.lua index 441ccd6..c3874f4 100644 --- a/letter_display.lua +++ b/letter_display.lua @@ -1,6 +1,5 @@ client = mqtt.client("Player", "mqtt.leizour.fr", 8883, true) client:connect("student", "O99Rq8$F12NXzhL5caya") - -- CLK (clock) : D15 -- DT (data) : D2 -- SW (Bouton): D4 @@ -8,23 +7,41 @@ client:connect("student", "O99Rq8$F12NXzhL5caya") -- counter = nombre de cran tourner -- button = bouton poussoir +playing = false mot = "" last_clicked = 0 function action(dir, counter, button) cls() lettre = string.char((counter % 26)+65) if button==1 then - if os.time() - last_clicked > 1 then - mot = mot..lettre - last_clicked = os.time() - else - client:publish("pendu/mot", mot, mqtt.QOS0) - mot = "" + if playing then + client:publish("pendu/lettre", lettre, mqtt.QOS0) + else + if os.time() - last_clicked > 1 then + mot = mot..lettre + last_clicked = os.time() + else + client:publish("pendu/mot", mot, mqtt.QOS0) + mot = "" + end end end console("Choix lettre : "..lettre) - console("Mot : "..mot) + if not playing then + console("Mot : "..mot) + end end +function handle_message(len, message, topic_len, topic_name) + if topic_name == "pendu/gamestate" then + if message == "PLAYING" then + playing = true + elseif message == "WAITING" then + playing = false + end + end +end + +client:subscribe("pendu/gamestate", mqtt.QOS0, handle_message) enc = encoder.attach(pio.GPIO15, pio.GPIO2, pio.GPIO4, action)