threads for marquee and send text animation implemented. working! hopefully

This commit is contained in:
interfisch 2017-02-17 00:40:42 +01:00
parent 174ac4c363
commit c43fe55663
2 changed files with 51 additions and 11 deletions

View File

@ -15,6 +15,9 @@ class FlipdotSender(object):
C_BLACK = 0
C_WHITE = 255
global threadrunning
threadrunning=False
lastimgmap = []
def __init__(self, udphost, udpport, img_size=(80,16), font_size=8, font_size_scroll=12,
@ -40,7 +43,11 @@ class FlipdotSender(object):
self._font_family = font_family
self._chars_per_line = chars_per_line
self._sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def stopAnimation(self):
global threadrunning
threadrunning=False #tried to stop a running animation
def _list2byte(self, l):
@ -70,6 +77,7 @@ class FlipdotSender(object):
self._sock.sendto(bytes(packet), (self._udphost, self._udpport))'''
def _send(self, image,fadespeed=0): #changes slowly 'fadespeed'-pixels at a time
global threadrunning
#if fadespeed=0 -> change instant.
#time to change= 1280/25*0.2
imgmap = []
@ -82,19 +90,27 @@ class FlipdotSender(object):
imgmaptmp=FlipdotSender.lastimgmap
if fadespeed>0:
threadrunning=True
#diff=np.sum(np.array(imgmaptmp) != np.array(imgmap)) #different pixels
pixelchangeind=np.arange(self._img_size[0]*self._img_size[1])
np.random.shuffle(pixelchangeind)
for _i,ind in enumerate(pixelchangeind):
if threadrunning==False:
break #stop this for
if ind<len(imgmaptmp): #imgmaptmp is not empty (normally at first run)
imgmaptmp[ind]=imgmap[ind]
if _i%fadespeed==0:
packet = self._array2packet(imgmaptmp)
self._sock.sendto(bytes(packet), (self._udphost, self._udpport))
time.sleep(0.2)
self.sendPacket(imgmap) #send packet and save last-imagemap
if threadrunning==True: #if animation wasnt cancelled
self.sendPacket(imgmap) #send packet and save last-imagemap
threadrunning=False
else:
self.sendPacket(imgmap) #send packet and save last-imagemap
def sendPacket(self, imgmap):
packet = self._array2packet(imgmap)
@ -166,10 +182,12 @@ class FlipdotSender(object):
def send_marquee(self, str, speed=3):
global threadrunning
threadrunning=True
offset = self._img_size[0]
font = ImageFont.truetype(self._font_family, self._font_size_scroll)
while offset >= -font.getsize(str)[0]-speed:
while offset >= -font.getsize(str)[0]-speed and threadrunning==True:
image = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)
draw = ImageDraw.Draw(image)
draw.fontmode = "1" # No AA
@ -178,6 +196,7 @@ class FlipdotSender(object):
self._send(image)
offset -= speed
time.sleep(0.15)
threadrunning=False
def send_img(self, img):
background = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)

View File

@ -4,15 +4,18 @@ 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 gametimeout
gametimeout=0
global flipthread
flipthread=None
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("raum2/flipdot/#")
client.subscribe("raum2/flipdot/#") #subscribe to every subtopic
#client.subscribe("raum2/flipdot/text")
#client.subscribe("raum2/flipdot/scroll")
#client.subscribe("raum2/flipdot/image")
@ -22,6 +25,7 @@ def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload.decode("utf-8")))
global mode
global gametimeout
global flipthread
if mode=="standby":
gametimeout=0
@ -36,14 +40,29 @@ def on_message(client, userdata, msg):
speed = 3
text = payload
flipdot.send_marquee(text, speed)
#flipdot.send_marquee(text, speed)
if flipthread is not None:
flipdot.stopAnimation()
flipthread.join() #wait for thread to finish
flipthread=Thread(target=flipdot.send_marquee, args=(text,speed))
flipthread.start()
if msg.topic == "raum2/flipdot/text/set":
payload = msg.payload.decode("utf-8")
if flipthread is not None:
flipdot.stopAnimation()
flipthread.join()
if len(payload)>0 and payload[0]=='~':
payload=payload[1:] #remove first char
flipdot.send_text(payload,25) #send_text with animation
flipdot.send_text(payload) #without animation
#flipdot.send_text(payload,25) #send_text with animation
flipthread=Thread(target=flipdot.send_text, args=(payload,25))
else:
#flipdot.send_text(payload) #without animation
flipthread=Thread(target=flipdot.send_text, args=(payload,))
flipthread.start()
if msg.topic == "raum2/flipdot/textFull/set":
payload = msg.payload.decode("utf-8")
@ -89,8 +108,10 @@ def on_message(client, userdata, msg):
flipdot = FlipdotSender("2001:67c:275c:a9::c", 2323)
hangman= Hangman("2001:67c:275c:a9::c", 2323,flipdot)
#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)