2016-12-23 00:33:23 +00:00
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
import socket
|
|
|
|
import binascii
|
|
|
|
import io
|
|
|
|
import time
|
|
|
|
import re
|
2017-01-17 22:28:21 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
|
|
|
|
class FlipdotSender(object):
|
|
|
|
'''
|
|
|
|
classdocs
|
|
|
|
'''
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
C_BLACK = 0
|
|
|
|
C_WHITE = 255
|
|
|
|
|
2017-02-16 23:40:42 +00:00
|
|
|
global threadrunning
|
|
|
|
threadrunning=False
|
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
lastimgmap = []
|
|
|
|
|
2019-06-30 12:48:00 +00:00
|
|
|
def __init__(self, udphost, udpport, img_size=(80,16), font_size=9, font_size_scroll=12,
|
|
|
|
font_offset1=(0,0), font_offset2=(0,8),
|
2016-12-23 01:39:36 +00:00
|
|
|
#font_family='/usr/share/fonts/gnu-free/FreeMono.ttf',
|
2017-08-13 17:43:26 +00:00
|
|
|
font_family='/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',
|
2019-06-30 12:48:00 +00:00
|
|
|
#font_family='/usr/share/fonts/truetype/freefont/FreeMono.ttf',
|
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
chars_per_line=11):
|
|
|
|
'''
|
|
|
|
Constructor
|
|
|
|
'''
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
self._udphost = udphost
|
|
|
|
if not type(udpport) is int or udpport > 65536:
|
|
|
|
raise TypeError('port has to be int and > 65536 !!')
|
|
|
|
self._udpport = udpport
|
|
|
|
self._img_size = img_size
|
|
|
|
self._font_size = font_size
|
2016-12-23 01:39:36 +00:00
|
|
|
self._font_size_scroll = font_size_scroll
|
2016-12-23 00:33:23 +00:00
|
|
|
self._font_offset1 = font_offset1
|
|
|
|
self._font_offset2 = font_offset2
|
|
|
|
self._font_family = font_family
|
|
|
|
self._chars_per_line = chars_per_line
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 23:40:42 +00:00
|
|
|
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
|
|
|
def stopAnimation(self):
|
|
|
|
global threadrunning
|
|
|
|
threadrunning=False #tried to stop a running animation
|
2019-06-30 12:48:00 +00:00
|
|
|
|
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
def _list2byte(self, l):
|
|
|
|
byte = 0
|
|
|
|
i = 0
|
|
|
|
for i in range(8):
|
|
|
|
byte += 2**(7-i) if l[i] else 0
|
|
|
|
return byte
|
2019-06-30 12:48:00 +00:00
|
|
|
|
|
|
|
def _array2packet(self, a):
|
2016-12-23 00:33:23 +00:00
|
|
|
return [self._list2byte(a[i*8:i*8+8]) for i in range(int(len(a)/8))]
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
'''
|
|
|
|
def _send(self, image): #old function, backup
|
2016-12-23 00:33:23 +00:00
|
|
|
imgmap = []
|
|
|
|
for pixel in image.getdata():
|
|
|
|
r, g, b, a = pixel
|
|
|
|
if r == FlipdotSender.C_WHITE:
|
|
|
|
imgmap.append(1)
|
|
|
|
else:
|
|
|
|
imgmap.append(0)
|
2017-02-16 21:30:01 +00:00
|
|
|
|
|
|
|
lastimgmap=imgmap
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
packet = self._array2packet(imgmap)
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
self._sock.sendto(bytes(packet), (self._udphost, self._udpport))'''
|
|
|
|
|
|
|
|
def _send(self, image,fadespeed=0): #changes slowly 'fadespeed'-pixels at a time
|
2017-02-16 23:40:42 +00:00
|
|
|
global threadrunning
|
2019-06-30 12:48:00 +00:00
|
|
|
#if fadespeed=0 -> change instant.
|
2017-02-16 21:30:01 +00:00
|
|
|
#time to change= 1280/25*0.2
|
|
|
|
imgmap = []
|
|
|
|
for pixel in image.getdata():
|
|
|
|
r, g, b, a = pixel
|
|
|
|
if r == FlipdotSender.C_WHITE:
|
|
|
|
imgmap.append(1)
|
|
|
|
else:
|
|
|
|
imgmap.append(0)
|
|
|
|
|
|
|
|
imgmaptmp=FlipdotSender.lastimgmap
|
|
|
|
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
if fadespeed>0:
|
2017-02-16 23:40:42 +00:00
|
|
|
threadrunning=True
|
2017-02-16 21:30:01 +00:00
|
|
|
#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):
|
2017-02-16 23:40:42 +00:00
|
|
|
if threadrunning==False:
|
|
|
|
break #stop this for
|
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
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)
|
2017-02-18 18:56:48 +00:00
|
|
|
if threadrunning==True: #if animation hasnt been cancelled
|
2017-02-16 23:40:42 +00:00
|
|
|
self.sendPacket(imgmap) #send packet and save last-imagemap
|
|
|
|
threadrunning=False
|
|
|
|
else:
|
|
|
|
self.sendPacket(imgmap) #send packet and save last-imagemap
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
|
|
|
|
def sendPacket(self, imgmap):
|
|
|
|
packet = self._array2packet(imgmap)
|
2016-12-23 00:33:23 +00:00
|
|
|
self._sock.sendto(bytes(packet), (self._udphost, self._udpport))
|
2017-02-16 21:30:01 +00:00
|
|
|
|
|
|
|
FlipdotSender.lastimgmap=imgmap
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-01-17 22:28:21 +00:00
|
|
|
def send_bytes(self, img):
|
|
|
|
imgmap = []
|
|
|
|
for pixel in img:
|
|
|
|
if pixel == "1":
|
|
|
|
imgmap.append(1)
|
|
|
|
else:
|
|
|
|
imgmap.append(0)
|
|
|
|
|
|
|
|
if len(img) < 1280:
|
|
|
|
imgmap = np.hstack((imgmap, np.zeros(1280-len(img), dtype=int)))
|
|
|
|
|
|
|
|
packet = self._array2packet(imgmap)
|
|
|
|
|
|
|
|
self._sock.sendto(bytes(packet), (self._udphost, self._udpport))
|
|
|
|
|
2019-06-30 12:48:00 +00:00
|
|
|
def send_binimage(self, data,fadespeed=0): #works like send_bytes, but enables fadeanimation
|
|
|
|
image = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)
|
|
|
|
draw = ImageDraw.Draw(image)
|
|
|
|
draw.fontmode = "1"
|
|
|
|
|
|
|
|
for i,d in enumerate(data):
|
|
|
|
if d=="1":
|
|
|
|
draw.point((i%self._img_size[0],int(i/self._img_size[0])), fill=FlipdotSender.C_WHITE)
|
|
|
|
|
|
|
|
self._send(image,fadespeed)
|
|
|
|
|
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
def send_text(self, text,fadespeed=0):
|
2016-12-23 00:33:23 +00:00
|
|
|
image = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)
|
|
|
|
draw = ImageDraw.Draw(image)
|
|
|
|
draw.fontmode = "1" # No AA
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
font = ImageFont.truetype(self._font_family, self._font_size)
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
cut = self._chars_per_line
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
splitted_text = text.split("|")
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
draw.text(self._font_offset1, splitted_text[0], font=font, fill=FlipdotSender.C_WHITE)
|
2016-12-23 01:39:36 +00:00
|
|
|
if len(splitted_text)>1:
|
2016-12-23 00:33:23 +00:00
|
|
|
draw.text(self._font_offset2, splitted_text[1], font=font, fill=FlipdotSender.C_WHITE)
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
self._send(image,fadespeed)
|
|
|
|
|
|
|
|
def send_textFull(self, text,fadespeed=0):
|
|
|
|
image = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)
|
|
|
|
draw = ImageDraw.Draw(image)
|
|
|
|
draw.fontmode = "1" # No AA
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
l=1000 #init very high
|
|
|
|
splitpoint=40 #very high
|
|
|
|
currentfontsize=12+1 #init with max font size
|
|
|
|
font = ImageFont.truetype(self._font_family, currentfontsize)
|
|
|
|
|
|
|
|
while(l>80): #while text too long and font not too small
|
2019-06-30 12:48:00 +00:00
|
|
|
if currentfontsize>8:
|
2017-02-16 21:30:01 +00:00
|
|
|
currentfontsize=currentfontsize-1 #reduce size and try if fits
|
|
|
|
else: #if fontsize too small, try cutting sentence (2 lines)
|
|
|
|
splitpoint=splitpoint-1
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
font = ImageFont.truetype(self._font_family, currentfontsize)
|
|
|
|
l=draw.textsize(text[0:splitpoint], font=font)[0]
|
|
|
|
print("Textlength="+str(l)+" split="+str(splitpoint))
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
if splitpoint==40: #not splitted
|
|
|
|
draw.text((0,int((16-currentfontsize)/2) ), text, font=font, fill=FlipdotSender.C_WHITE)
|
|
|
|
else:
|
|
|
|
draw.text((0,-1), text[0:splitpoint], font=font, fill=FlipdotSender.C_WHITE)
|
|
|
|
draw.text((0,-1+currentfontsize), text[splitpoint:], font=font, fill=FlipdotSender.C_WHITE)
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 21:30:01 +00:00
|
|
|
self._send(image,fadespeed)
|
|
|
|
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
def send_marquee(self, str, speed=3):
|
2017-02-16 23:40:42 +00:00
|
|
|
global threadrunning
|
|
|
|
threadrunning=True
|
2016-12-23 00:33:23 +00:00
|
|
|
offset = self._img_size[0]
|
2016-12-23 01:39:36 +00:00
|
|
|
font = ImageFont.truetype(self._font_family, self._font_size_scroll)
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2017-02-16 23:40:42 +00:00
|
|
|
while offset >= -font.getsize(str)[0]-speed and threadrunning==True:
|
2016-12-23 00:33:23 +00:00
|
|
|
image = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)
|
|
|
|
draw = ImageDraw.Draw(image)
|
|
|
|
draw.fontmode = "1" # No AA
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
draw.text((offset,0), str, font=font, fill=FlipdotSender.C_WHITE)
|
|
|
|
self._send(image)
|
|
|
|
offset -= speed
|
|
|
|
time.sleep(0.15)
|
2017-02-16 23:40:42 +00:00
|
|
|
threadrunning=False
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
def send_img(self, img):
|
|
|
|
background = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)
|
|
|
|
stream = io.BytesIO(img)
|
|
|
|
image = Image.open(stream)
|
|
|
|
image = image.convert(mode='RGBA')
|
|
|
|
image = image.resize(self._img_size)
|
|
|
|
image.save('/tmp/send2.jpeg', 'JPEG')
|
|
|
|
background.paste(image, box=(0,0), mask=None)
|
|
|
|
background.save('/tmp/send.jpeg', 'JPEG')
|
2019-06-30 12:48:00 +00:00
|
|
|
|
2016-12-23 00:33:23 +00:00
|
|
|
self._send(background)
|