155 lines
5.4 KiB
Python
155 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
import time
|
|
import paho.mqtt.client as mqtt
|
|
from FlipdotSender import FlipdotSender
|
|
import time
|
|
from hangman import Hangman
|
|
from threading import Thread
|
|
|
|
global mode
|
|
mode="standby"
|
|
global timeout
|
|
timeout=0
|
|
global flipthread
|
|
flipthread=None
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
print("Connected with result code " + str(rc))
|
|
client.subscribe("raum2/flipdot/#") #subscribe to every subtopic
|
|
#client.subscribe("raum2/flipdot/text")
|
|
#client.subscribe("raum2/flipdot/scroll")
|
|
#client.subscribe("raum2/flipdot/image")
|
|
|
|
|
|
def on_message(client, userdata, msg):
|
|
print(msg.topic + " " + str(msg.payload.decode("utf-8")))
|
|
global mode
|
|
global timeout
|
|
global flipthread
|
|
|
|
if mode=="standby" or mode=="inuse":
|
|
|
|
if msg.topic == "raum2/flipdot/scroll/set":
|
|
mode="inuse"
|
|
updateTimeout()
|
|
payload = msg.payload.decode("utf-8")
|
|
|
|
if len(payload)>0 and (payload[0]).isdigit():
|
|
speed = int(payload[0])
|
|
text = payload[1:]
|
|
else:
|
|
speed = 3
|
|
text = payload
|
|
|
|
#flipdot.send_marquee(text, speed)
|
|
if flipthread is not None and flipthread.isAlive():
|
|
flipdot.stopAnimation()
|
|
while flipthread.isAlive():
|
|
flipdot.stopAnimation() #try to stop animation
|
|
time.sleep(0.1)
|
|
flipthread.join() #wait for thread to finish
|
|
|
|
flipthread=Thread(target=flipdot.send_marquee, args=(text,speed))
|
|
flipthread.start()
|
|
|
|
if mode!="inuse" and msg.topic == "raum2/flipdot/text/set": #used to display information regulary (time etc). only available if flipdot not in use
|
|
payload = msg.payload.decode("utf-8")
|
|
|
|
if flipthread is not None and flipthread.isAlive():
|
|
flipdot.stopAnimation()
|
|
while flipthread.isAlive():
|
|
flipdot.stopAnimation() #try to stop animation
|
|
time.sleep(0.1)
|
|
flipthread.join()
|
|
|
|
if len(payload)>0 and payload[0]=='~':
|
|
payload=payload[1:] #remove first char
|
|
#flipdot.send_text(payload,25) #send_text with animation
|
|
flipthread=Thread(target=flipdot.send_text, args=(payload,64))
|
|
else:
|
|
#flipdot.send_text(payload) #without animation
|
|
flipthread=Thread(target=flipdot.send_text, args=(payload,))
|
|
flipthread.start()
|
|
|
|
|
|
if msg.topic == "raum2/flipdot/textFull/set": #scale/break text automatically
|
|
mode="inuse"
|
|
updateTimeout()
|
|
payload = msg.payload.decode("utf-8")
|
|
if len(payload)>0 and payload[0]=='~':
|
|
payload=payload[1:]
|
|
flipdot.send_textFull(payload,50)
|
|
flipdot.send_textFull(payload)
|
|
|
|
if msg.topic == "raum2/flipdot/image/set":
|
|
mode="inuse"
|
|
updateTimeout()
|
|
payload = msg.payload.decode("utf-8")
|
|
print(payload)
|
|
flipdot.send_bytes(payload)
|
|
|
|
|
|
|
|
if msg.topic == "raum2/flipdot/hangman/set":
|
|
payload = msg.payload.decode("utf-8")
|
|
|
|
if payload=="#start":
|
|
hangman.setup()
|
|
updateTimeout()
|
|
client.publish("raum2/flipdot/hangman","started")
|
|
mode="hangman"
|
|
|
|
elif (mode=="hangman") and (len(payload)>0): #try entered character
|
|
updateTimeout()
|
|
trychar=payload[-1]
|
|
gamestatus=hangman.step(trychar)
|
|
client.publish("raum2/flipdot/hangman","char="+trychar.upper())
|
|
if gamestatus!=1: #game has ended
|
|
mode="standby"
|
|
if gamestatus==2: #gameover
|
|
client.publish("raum2/flipdot/hangman","gameover")
|
|
elif gamestatus==3: #won
|
|
client.publish("raum2/flipdot/hangman","won")
|
|
elif gamestatus==0: #ended in another way
|
|
client.publish("raum2/flipdot/hangman","ended")
|
|
|
|
|
|
if payload=="#stop":
|
|
mode="standby"
|
|
client.publish("raum2/flipdot/hangman","ended")
|
|
|
|
def updateTimeout():
|
|
global timeout
|
|
timeout=int(round(time.time() * 1000))
|
|
|
|
|
|
#flipdot = FlipdotSender("2001:67c:275c:a9::c", 2323)
|
|
flipdot = FlipdotSender("localhost", 2323)
|
|
#hangman= Hangman("2001:67c:275c:a9::c", 2323,flipdot)
|
|
hangman= Hangman("localhost", 2323,flipdot)
|
|
|
|
|
|
|
|
client = mqtt.Client()
|
|
client.on_connect = on_connect
|
|
client.on_message = on_message
|
|
client.connect("raum.ctdo.de", 1883, 60)
|
|
client.loop_start()
|
|
|
|
|
|
while True:
|
|
millis = int(round(time.time() * 1000))
|
|
if (timeout!=0) and (mode=="hangman"):
|
|
if millis-120000 > timeout: #timeout
|
|
print("Game Timeout")
|
|
mode="standby"
|
|
timeout=0
|
|
if (timeout!=0) and (mode=="inuse"): #inuse: flipdot was manually controlled, do not update time,pwr etc for a while
|
|
if millis-10000 > timeout: #timeout
|
|
mode="standby"
|
|
timeout=0
|
|
|
|
time.sleep(2)
|
|
|
|
|