Skip to content

Commit 52a4c72

Browse files
committed
first flipdot command
1 parent 6518e34 commit 52a4c72

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

flipdot/gen_string.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 = 3
12+
INVERT=False
13+
14+
IMG_SIZE = (120, 16)
15+
FONT_SIZE = 11
16+
FONT_OFFSET= (1, 2)
17+
18+
C_BLACK = (0, 0, 0)
19+
C_WHITE = (255, 255, 255)
20+
21+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
22+
23+
def list2byte(l):
24+
byte = 0
25+
i = 0
26+
for i in range(8):
27+
byte += 2**(7-i) if l[i] else 0
28+
return byte
29+
30+
def array2packet(a):
31+
return str(bytearray([list2byte(a[i*8:i*8+8]) for i in range(len(a)/8)]))
32+
33+
def str2array(s):
34+
image = Image.new("RGBA", IMG_SIZE, C_BLACK)
35+
draw = ImageDraw.Draw(image)
36+
draw.fontmode = "1" # No AA
37+
#font = ImageFont.load_default()
38+
font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", FONT_SIZE)
39+
40+
draw.text(FONT_OFFSET, s, font=font, fill=C_WHITE)
41+
42+
imgmap = []
43+
for pixel in image.getdata():
44+
r, g, b, a = pixel
45+
if r == 255:
46+
imgmap.append(0 if INVERT else 1)
47+
else:
48+
imgmap.append(1 if INVERT else 0)
49+
return imgmap
50+
51+
while True:
52+
text = sys.stdin.readline().decode('utf-8')
53+
if text == "":
54+
break
55+
sock.sendto(array2packet(str2array(text.strip())), (UDPHOST, UDPPORT))
56+
time.sleep(1.0/FPS)
57+

flipdot/scroll_text.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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)

print.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
if [ $(echo $4 | wc -c) -gt 17 ]; then
3+
echo $4 | flipdot/scroll_text.py
4+
else
5+
echo $4 | flipdot/gen_string.py
6+
fi
7+
if [ "$?" == "0" ]; then
8+
echo "$1, flipping done"
9+
else
10+
echo "$1, something flipped out."
11+
fi

0 commit comments

Comments
 (0)