This repository was archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppa6ctl.py
More file actions
404 lines (276 loc) · 8.4 KB
/
ppa6ctl.py
File metadata and controls
404 lines (276 loc) · 8.4 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
'''
name: ppa6ctl
description: Python module to control PeriPage A6 printer
author: Kevin Mader, Alexander Weigl
website: https://www.elektronikundco.de
git: https://github.com/linglingltd/peripage-a6-control
Credits to Elias Weingaertner
for reverse engineering the protocol
https://github.com/eliasweingaertner/peripage-A6-bluetooth
Tested with PeriPage A6, 203dpi
https://peripagepocketprinter.com/collections/pocket-printers/products/pocket-printer?variant=32420578394163
Horizontal resolution: 384px
Turns itself off after 20min of no bluetooth communication
'''
import os
import re
import time
import qrcode
import threading
import bluetooth
from io import BytesIO
from cairosvg import svg2png
from PIL import Image, ImageOps, ImageEnhance
_address = None # format: 00:00:00:00:00:00
_sock = None
_lastError = ""
_printing = False
_keepAliveThread = None
# Connection management and basic control
def search(name = "PeriPage"):
'''Searches nearby bluetooth devices for a PeriPage printer and returns its MAC address'''
devices = bluetooth.discover_devices(lookup_names = True)
for address, devname in devices:
if devname.find(name) != -1:
return address
return False
def connect(address, keepalive = False):
'''Connects to a given address using Bluetooth and starts a keepalive thread if wanted'''
global _address, _sock, _keepAliveThread
# check if address is a valid MAC address
if not re.fullmatch(r"(([0-9a-f]){2}:){5}([0-9a-f]){2}", address, re.IGNORECASE):
return error("Invalid MAC address, format: 00:00:00:00:00:00")
_address = address
_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
try:
_sock.connect((_address, 1))
except:
return error("BluetoothSocket connection failed")
reset()
if keepalive:
_keepAliveThread = threading.Thread(target=_keepaliveFunc)
_keepAliveThread.start()
return True
def connected():
'''Returns the connection status'''
global _address, _sock
try:
_sock.getpeername()
return True
except:
return False
def disconnect():
'''Close the connection to the Bluetooth printer'''
global _sock
if not _sock:
return error("Printer not connected")
_sock.close()
_sock = None
return True
def _keepaliveFunc():
'''Periodically gets the device name of the printer to prevent printer shutdown'''
global _keepAliveThread, _printing
count = 60
while True:
# Only force communication if nothing is printed now
if not _printing:
if not connected():
break
if count == 0:
count = 60
if not getDeviceName():
break
if count > 0:
count = count - 1
time.sleep(1)
_keepAliveThread = None
def reset():
global _sock
if not _sock:
return error("Printer not connected")
_sock.send(bytes.fromhex("10ff50f1"))
data = _sock.recv(32)
_sock.send(bytes.fromhex("000000000000000000000000"))
return data
def reset2():
global _sock
if not _sock:
return error("Printer not connected")
sock.send(bytes.fromhex("10ff100002"))
data = sock.recv(32)
return data
def getDeviceName():
'''Returns the printers device name'''
global _sock
if not _sock:
return error("Printer not connected")
_sock.send(bytes.fromhex("10ff3011"))
data = _sock.recv(32)
try:
decoded = data.decode("ascii")
return decoded
except:
return False
def getFWDPI():
'''Returns the printers firmware and dpi'''
global _sock
if not _sock:
return error("Printer not connected")
_sock.send(bytes.fromhex("10ff20f1"))
data = _sock.recv(32)
try:
decoded = data.decode("ascii")
return decoded
except:
return False
def getSerial():
'''Returns the printers serial number'''
global _sock
if not _sock:
return error("Printer not connected")
_sock.send(bytes.fromhex("10ff20f2"))
data = _sock.recv(32)
try:
decoded = data.decode("ascii")
return decoded
except:
return False
# Error functions
def getLastError():
'''Returns the last error message and clears the buffer'''
global _lastError
errorMessage = _lastError
_lastError = ""
return errorMessage
def error(message):
'''Writes an error message to the buffer'''
global _lastError
_lastError = message
return False
# Automatic printing
def image(filename, brightness = 0, contrast = 0):
'''Prints an image'''
if not printStart():
return False
if not printImage(filename, brightness, contrast):
return False
return printStop()
def QR(data):
'''Prints QR code'''
if not printStart():
return False
if not _printImage(qrcode.make(data)):
return False
return printStop()
def text(text):
'''Prints some text'''
if not printStart():
return False
if not printLn(text):
return False
return printStop()
def feed():
'''Feeds some paper'''
if not printStart():
return False
return printStop()
# Manual printing
def printStart():
'''Starts manual printing mode'''
global _sock, _printing
if not _sock:
return error("Printer not connected")
_printing = True
_sock.send(bytes.fromhex("10fffe01"))
return True
def printStop():
'''Stops manual printing mode and feed paper for a clean cut'''
global _sock, _printing
if not _sock:
return error("Printer not connected")
_sock.send(bytes.fromhex("1b4a4010fffe45"))
# Get answer code, \xAA seems to be printed successfully
data = _sock.recv(32)
_printing = False
return True
def printString(text):
'''Prints a string in manual printing mode'''
global _sock
if not _sock:
return error("Printer not connected")
# Remove non ASCII characters
outputString = re.sub(r'[^\x00-\x7F]+','?', text) # maybe compile the regex?
line = bytes(outputString, "ascii")
_sock.send(line)
return True
def printNewline():
'''Prints a newline in manual printing mode'''
return printString("\n")
def printLn(text):
'''Prints a line in manual printing mode'''
if not printString(text):
return False
return printNewline()
def printFeed(lines, blank = True):
'''Feeds paper on pixel basis, white or black in manual printing mode'''
if lines > 65535:
return error("Too much lines, maximum: 65535")
global _sock
if not _sock:
return error("Printer not connected")
height_bytes = (lines).to_bytes(2, byteorder = "little")
_sock.send(bytes.fromhex("1d7630003000") + height_bytes)
line = [(0 if blank else 255) for i in range(0, 48)]
for i in range(0, lines):
_sock.send(bytes(line))
time.sleep(0.02)
return True
def printQR(data):
'''Prints QR code in manual printing mode'''
return _printImage(qrcode.make(data))
def printImage(filename, brightness = 0, contrast = 0):
'''Prints an image file in manual printing mode'''
file_name, file_extension = os.path.splitext(filename)
img = None
if file_extension == '.svg':
png_data = svg2png(file_obj=open(filename, "rb"), background_color = "#FFFFFF", output_width = 384)
img = Image.open(BytesIO(png_data))
else:
img = Image.open(filename)
return _printImage(img, brightness, contrast)
def _printImage(img, brightness = 0, contrast = 0):
'''Prints an image object in manual printing mode'''
global _sock
if not _sock:
return error("Printer not connected")
# make image greyscale
img = img.convert("L")
img_width = img.size[0]
img_height = img.size[1]
# brightness / contrast
if brightness:
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(brightness)
if contrast:
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(contrast)
img = ImageOps.invert(img)
new_width = 384 #Peripage A6 image width
scale = new_width / float(img_width)
new_height = int(img_height * scale)
if new_height > 65535:
return error("Image height too big, maximum supported: 65535")
img = img.resize((384, new_height), Image.ANTIALIAS)
# make image b/w
img = img.convert("1")
height_bytes = new_height.to_bytes(2, byteorder = "little")
_sock.send(bytes.fromhex("1d7630003000") + height_bytes)
# send image to printer, line by line
image_bytes = img.tobytes()
# a chunk is one line, 48byte * 8 = 384bit
chunksize = 48
for i in range(0, len(image_bytes), chunksize):
chunk = image_bytes[i:i + chunksize]
_sock.send(chunk)
time.sleep(0.02)
return True