projet-iot/player/letter_display.lua

55 lines
1.5 KiB
Lua

dofile("screen.lua")
attachscreen(18, 19, 0x3C)
dofile("mqtt_config.lua")
client = mqtt.client(client_name, "mqtt.leizour.fr", 8883, true)
client:connect("student", password)
client:publish("pendu/gamestate", "query", mqtt.QOS0)
-- CLK (clock) : D15
-- DT (data) : D2
-- SW (Bouton): D4
-- dir = direction de tournage de l'encodeur
-- counter = nombre de crans tournés
-- 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 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)
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
mot = ""
end
end
end
client:subscribe("pendu/gamestate", mqtt.QOS0, handle_message)
enc = encoder.attach(pio.GPIO15, pio.GPIO2, pio.GPIO4, action)