mqtt-to-flipdot/FlipdotSender.py

126 lines
4.0 KiB
Python
Raw Normal View History

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
'''
C_BLACK = 0
C_WHITE = 255
2016-12-23 01:39:36 +00:00
def __init__(self, udphost, udpport, img_size=(80,16), font_size=8, font_size_scroll=12,
font_offset1=(0,-1), font_offset2=(0,7),
#font_family='/usr/share/fonts/gnu-free/FreeMono.ttf',
font_family='/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf',
2016-12-23 00:33:23 +00:00
#font_family='/usr/share/fonts/truetype/freefont/FreeMono.ttf',
2016-12-23 01:39:36 +00:00
2016-12-23 00:33:23 +00:00
chars_per_line=11):
'''
Constructor
'''
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
self._sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
def _list2byte(self, l):
byte = 0
i = 0
for i in range(8):
byte += 2**(7-i) if l[i] else 0
return byte
def _array2packet(self, a):
return [self._list2byte(a[i*8:i*8+8]) for i in range(int(len(a)/8))]
def _send(self, image):
imgmap = []
for pixel in image.getdata():
r, g, b, a = pixel
if r == FlipdotSender.C_WHITE:
imgmap.append(1)
else:
imgmap.append(0)
packet = self._array2packet(imgmap)
self._sock.sendto(bytes(packet), (self._udphost, self._udpport))
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))
2016-12-23 00:33:23 +00:00
def send_text(self, text):
image = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)
draw = ImageDraw.Draw(image)
draw.fontmode = "1" # No AA
font = ImageFont.truetype(self._font_family, self._font_size)
cut = self._chars_per_line
splitted_text = text.split("|")
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)
self._send(image)
def send_marquee(self, str, speed=3):
offset = self._img_size[0]
2016-12-23 01:39:36 +00:00
font = ImageFont.truetype(self._font_family, self._font_size_scroll)
2016-12-23 00:33:23 +00:00
while offset >= -font.getsize(str)[0]-speed:
image = Image.new("RGBA", self._img_size, FlipdotSender.C_BLACK)
draw = ImageDraw.Draw(image)
draw.fontmode = "1" # No AA
draw.text((offset,0), str, font=font, fill=FlipdotSender.C_WHITE)
self._send(image)
offset -= speed
time.sleep(0.15)
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')
self._send(background)