forked from eliasweingaertner/peripage-A6-bluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppa6-print.py
executable file
·170 lines (159 loc) · 6.07 KB
/
ppa6-print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from gattlib import GATTRequester
from PIL import Image, ImageOps, ImageFont, ImageDraw
import time
import crc8
import sys
import argparse
parser = argparse.ArgumentParser(description="Print an text to a Peripage A6 BLE")
parser.add_argument("BTMAC",help="BT MAC address of the Peripage A6")
parser.add_argument("-t", "--text",type=str, help="Text to be printed")
args = parser.parse_args()
if not args.text:
print("ERROR: Please specfiy text with -t or --text argument")
sys.exit(1)
# ------------------------------------------------------------------------------
# imgFromString : Convert string to binary image
# ------------------------------------------------------------------------------
def imgFromString(s):
# Font choice
font = ImageFont.truetype("liberation-mono/LiberationMono-Regular.ttf", 50)
# Get size of text
size = font.getsize_multiline(s)
# Fix height and width
size_x = 384 if size[0] <= 384 else size[0]
size_y = size[1]
# Create image
img = Image.new("RGB", size=(size_x, size_y), color="white")
# Draw text in image
draw = ImageDraw.Draw(img)
draw.text((0, 0), s, (0, 0, 0), font=font)
# Convert RGB image to binary image
img = ImageOps.invert(img.convert('L'))
img = img.convert('1')
# Flip image to print
#img = ImageOps.flip(img)
#img = ImageOps.mirror(img)
# Save image to file
#img.save('img.png')
return img
# ------------------------------------------------------------------------------
# binFromImg : Convert binary image to array
# ------------------------------------------------------------------------------
def binFromImg(img):
binImg=[]
for line in range (0,img.size[1]):
binImg.append(''.join(format(byte, '08b') for byte in img.tobytes()[int(line*(img.size[0]/8)):int((line*(img.size[0]/8))+img.size[0]/8)]))
return binImg
# ------------------------------------------------------------------------------
# dataCrc : Calcul hex CRC-8
# ------------------------------------------------------------------------------
def dataCrc(data):
hash = crc8.crc8()
hash.update(bytes.fromhex(data))
return str(hash.hexdigest())
# ------------------------------------------------------------------------------
# binCount : Convert binary image to array
# ------------------------------------------------------------------------------
def binCount (binImg):
trame=[]
i=0
#read Image line by line
for line in binImg:
nb_zero=0
nb_one=0
trame.append('')
# Read line char by char
for char in line:
# Bit '0' process
if char == '0':
# Bit '1' before
if nb_one!=0:
# Format '1' number to hex + 128 (First bit to print black)
trame[i]+='{:02x}'.format(128+nb_one)
nb_one=0
# Max number is 127 (First bit color + 127 max number = '0x7f')
if nb_zero>126:
trame[i]+='{:02x}'.format(nb_zero)
nb_zero=0
nb_zero += 1
# Bit '1' process
if char == '1':
# Bit '0' before
if nb_zero!=0:
# Format '0' number to hex
trame[i]+='{:02x}'.format(nb_zero)
nb_zero=0
# Max number is 127 (First bit color + 127 max number = '0xff')
if nb_one>126:
trame[i]+='{:02x}'.format(128+nb_one)
nb_one=0
nb_one += 1
# End of trame. If '1' or '0' before process
if nb_zero!=0:
trame[i]+='{:02x}'.format(nb_zero)
elif nb_one!=0:
trame[i]+='{:02x}'.format(128+nb_one)
i+=1
return trame
# ------------------------------------------------------------------------------
# bleConnect : Connect to printer mac
# ------------------------------------------------------------------------------
def bleConnect(mac):
host = mac
req = GATTRequester(host, False)
req.connect(True)
# Some config trame
req.write_by_handle(0x09, bytes([1, 0]))
time.sleep(0.02)
req.write_by_handle(0x000e, bytes([1, 0]))
time.sleep(0.02)
req.write_by_handle(0x0011, bytes([2, 0]))
time.sleep(0.02)
req.exchange_mtu(83)
time.sleep(0.02)
req.write_cmd(0x0006, bytes([18, 81, 120, 168, 0, 1, 0, 0, 0, 255, 18, 81, 120, 163, 0, 1, 0, 0, 0, 255]))
time.sleep(0.02)
req.write_cmd(0x0006, bytes([18, 81, 120, 187, 0, 1, 0, 1, 7, 255]))
time.sleep(0.02)
req.write_cmd(0x0006, bytes([18, 81, 120, 163, 0, 1, 0, 0, 0, 255]))
time.sleep(0.2)
return req
# ------------------------------------------------------------------------------
# printData : Print text
# ------------------------------------------------------------------------------
def printText(text,req):
data = binCount(binFromImg(imgFromString(text)))
for dat in data:
# Header of trame
head = "5178bf00"
# Format BT trame
trame=head + '{:02x}'.format(len(bytes.fromhex(dat)),'x') + "00" + dat + dataCrc(dat) + "ff"
print(trame)
i = len(trame)
# Pull 40 bytes trames
while i > 0:
if i > 40:
req.write_cmd(0x06, bytes.fromhex(trame[len(trame)-i:len(trame)-i+40]))
i -= 40
else:
req.write_cmd(0x06, bytes.fromhex(trame[len(trame)-i:len(trame)]))
i -= 40
time.sleep(0.01)
# 90 dp moving forward paper
forwardPaper(90)
return 0
# ------------------------------------------------------------------------------
# forwardPaper : Moving forward
# ------------------------------------------------------------------------------
def forwardPaper(dp):
head = "5178a100"
data = '{:02x}'.format(dp) + '00'
# Format BT trame
trame=head + '{:02x}'.format(len(bytes.fromhex(data)),'x') + "00" + data + dataCrc(data) + "ff"
req.write_cmd(0x06, bytes.fromhex(trame))
time.sleep(0.01)
#Start
host = args.BTMAC
if args.text:
req = bleConnect(host)
printText(args.text,req)