|
| 1 | +#!/usr/bin/env python2 |
| 2 | + |
| 3 | +import socket, time |
| 4 | +from PIL import Image, ImageFont, ImageDraw |
| 5 | +import sys |
| 6 | +import fileinput |
| 7 | + |
| 8 | +UDPHOST="sr-flipdot.local" |
| 9 | +UDPPORT=2323 |
| 10 | + |
| 11 | +FPS = 50 |
| 12 | +STEPS = 2 |
| 13 | +INVERT=False |
| 14 | + |
| 15 | +DISPLAY_SIZE = (120, 16) |
| 16 | +FONT_SIZE = 10 |
| 17 | +FONT_OFFSET= (1, 0) |
| 18 | + |
| 19 | +C_BLACK = (0, 0, 0) |
| 20 | +C_WHITE = (255, 255, 255) |
| 21 | + |
| 22 | +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 23 | + |
| 24 | +def list2byte(l): |
| 25 | + byte = 0 |
| 26 | + i = 0 |
| 27 | + for i in range(8): |
| 28 | + byte += 2**(7-i) if l[i] else 0 |
| 29 | + return byte |
| 30 | + |
| 31 | +def array2packet(a): |
| 32 | + return str(bytearray([list2byte(a[i*8:i*8+8]) for i in range(len(a)/8)])) |
| 33 | + |
| 34 | +def str2array(s): |
| 35 | + #font = ImageFont.load_default() |
| 36 | + font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", FONT_SIZE) |
| 37 | + |
| 38 | + img_width = font.getsize(s)[0] |
| 39 | + image = Image.new("RGBA", (img_width,DISPLAY_SIZE[1]), C_BLACK) |
| 40 | + draw = ImageDraw.Draw(image) |
| 41 | + draw.fontmode = "1" # no AA |
| 42 | + |
| 43 | + draw.text(FONT_OFFSET, s, font=font, fill=C_WHITE) |
| 44 | + |
| 45 | + imgmap = [] |
| 46 | + for pixel in image.getdata(): |
| 47 | + r, g, b, a = pixel |
| 48 | + if r == 255: |
| 49 | + imgmap.append(0 if INVERT else 1) |
| 50 | + else: |
| 51 | + imgmap.append(1 if INVERT else 0) |
| 52 | + return imgmap |
| 53 | + |
| 54 | + |
| 55 | +def scroll_text(imgmap): |
| 56 | + |
| 57 | + display_width = DISPLAY_SIZE[0] |
| 58 | + display_heigth = DISPLAY_SIZE[1] |
| 59 | + |
| 60 | + imgmap_width = len(imgmap) / display_heigth |
| 61 | + scrollimg_width = imgmap_width + 2*display_width |
| 62 | + |
| 63 | + scroll_imgmap = [1 if INVERT else 0] * (scrollimg_width * display_heigth) |
| 64 | + |
| 65 | + # expand imgmap |
| 66 | + for row in range(display_heigth): |
| 67 | + for col in range(imgmap_width): |
| 68 | + scroll_imgmap[display_width-STEPS + row*scrollimg_width + col] = imgmap[col+row*imgmap_width] |
| 69 | + |
| 70 | + |
| 71 | + # scroll img |
| 72 | + curr_frame = [0] * display_width * display_heigth |
| 73 | + |
| 74 | + for offset in range(0, scrollimg_width-display_width, STEPS): |
| 75 | + for row in range(display_heigth): |
| 76 | + for col in range(display_width): |
| 77 | + curr_frame[col+row*display_width] = scroll_imgmap[col+row*scrollimg_width + offset] |
| 78 | + |
| 79 | + sock.sendto(array2packet(curr_frame), (UDPHOST, UDPPORT)) |
| 80 | + #time.sleep(1.0/FPS) |
| 81 | + |
| 82 | +try: |
| 83 | + while True: |
| 84 | + text = sys.stdin.readline().decode('utf-8') |
| 85 | + if text == "": |
| 86 | + break |
| 87 | + scroll_text(str2array(text.strip())) |
| 88 | +except KeyboardInterrupt: |
| 89 | + print("") |
| 90 | + sys.exit(0) |
0 commit comments