1
1
# Protocol information from Thermal_Printer Arduino library
2
2
# https://github.com/bitbank2/Thermal_Printer/
3
- import collections
4
- import time
5
3
from adafruit_ble .uuid import StandardUUID
6
4
from adafruit_ble .services import Service
7
5
from adafruit_ble .characteristics .stream import StreamIn
8
6
9
- PrinterConfig = collections .namedtuple (
10
- "PrinterConfig" , ("width" , "service" , "characteristic" )
11
- )
12
-
13
7
# Switch the printing mode to bitmap
14
8
printimage = b"Qx\xbe \x00 \x01 \x00 \x00 \x00 \xff "
15
9
# Switch the printing mode to text
19
13
20
14
# this table helps compute the checksum of transmitted data
21
15
# it is crc-8-ccitt
22
- checksumtable = b"\x00 \x07 \x0e \t \x1c \x1b \x12 \x15 8?61$#*-pw~ylkbeHOFATSZ]\xe0 \xe7 \xee \xe9 \xfc \xfb \xf2 \xf5 \xd8 \xdf \xd6 \xd1 \xc4 \xc3 \xca \xcd \x90 \x97 \x9e \x99 \x8c \x8b \x82 \x85 \xa8 \xaf \xa6 \xa1 \xb4 \xb3 \xba \xbd \xc7 \xc0 \xc9 \xce \xdb \xdc \xd5 \xd2 \xff \xf8 \xf1 \xf6 \xe3 \xe4 \xed \xea \xb7 \xb0 \xb9 \xbe \xab \xac \xa5 \xa2 \x8f \x88 \x81 \x86 \x93 \x94 \x9d \x9a ' ).;<52\x1f \x18 \x11 \x16 \x03 \x04 \r \n WPY^KLEBohafst}z\x89 \x8e \x87 \x80 \x95 \x92 \x9b \x9c \xb1 \xb6 \xbf \xb8 \xad \xaa \xa3 \xa4 \xf9 \xfe \xf7 \xf0 \xe5 \xe2 \xeb \xec \xc1 \xc6 \xcf \xc8 \xdd \xda \xd3 \xd4 ing`ur{|QV_XMJCD\x19 \x1e \x17 \x10 \x05 \x02 \x0b \x0c !&/(=:34NI@GRU\\ [vqx\x7f jmdc>907\" %,+\x06 \x01 \x08 \x0f \x1a \x1d \x14 \x13 \xae \xa9 \xa0 \xa7 \xb2 \xb5 \xbc \xbb \x96 \x91 \x98 \x9f \x8a \x8d \x84 \x83 \xde \xd9 \xd0 \xd7 \xc2 \xc5 \xcc \xcb \xe6 \xe1 \xe8 \xef \xfa \xfd \xf4 \xf3 "
16
+ checksumtable = (
17
+ b"\x00 \x07 \x0e \t \x1c \x1b \x12 \x15 8?61$#*-"
18
+ b"pw~ylkbeHOFATSZ]"
19
+ b"\xe0 \xe7 \xee \xe9 \xfc \xfb \xf2 \xf5 \xd8 \xdf \xd6 \xd1 \xc4 \xc3 \xca \xcd "
20
+ b"\x90 \x97 \x9e \x99 \x8c \x8b \x82 \x85 \xa8 \xaf \xa6 \xa1 \xb4 \xb3 \xba \xbd "
21
+ b"\xc7 \xc0 \xc9 \xce \xdb \xdc \xd5 \xd2 \xff \xf8 \xf1 \xf6 \xe3 \xe4 \xed \xea "
22
+ b"\xb7 \xb0 \xb9 \xbe \xab \xac \xa5 \xa2 \x8f \x88 \x81 \x86 \x93 \x94 \x9d \x9a "
23
+ b"' ).;<52\x1f \x18 \x11 \x16 \x03 \x04 \r \n "
24
+ b"WPY^KLEBohafst}z"
25
+ b"\x89 \x8e \x87 \x80 \x95 \x92 \x9b \x9c \xb1 \xb6 \xbf \xb8 \xad \xaa \xa3 \xa4 "
26
+ b"\xf9 \xfe \xf7 \xf0 \xe5 \xe2 \xeb \xec \xc1 \xc6 \xcf \xc8 \xdd \xda \xd3 \xd4 "
27
+ b"ing`ur{|QV_XMJCD"
28
+ b"\x19 \x1e \x17 \x10 \x05 \x02 \x0b \x0c !&/(=:34"
29
+ b"NI@GRU\\ [vqx\x7f jmdc"
30
+ b'>907"%,+\x06 \x01 \x08 \x0f \x1a \x1d \x14 \x13 '
31
+ b"\xae \xa9 \xa0 \xa7 \xb2 \xb5 \xbc \xbb \x96 \x91 \x98 \x9f \x8a \x8d \x84 \x83 "
32
+ b"\xde \xd9 \xd0 \xd7 \xc2 \xc5 \xcc \xcb \xe6 \xe1 \xe8 \xef \xfa \xfd \xf4 \xf3 "
33
+ )
23
34
24
35
# mirrortable[i] is the bit reversed version of the byte i
25
- mirrortable = b"\x00 \x80 @\xc0 \xa0 `\xe0 \x10 \x90 P\xd0 0\xb0 p\xf0 \x08 \x88 H\xc8 (\xa8 h\xe8 \x18 \x98 X\xd8 8\xb8 x\xf8 \x04 \x84 D\xc4 $\xa4 d\xe4 \x14 \x94 T\xd4 4\xb4 t\xf4 \x0c \x8c L\xcc ,\xac l\xec \x1c \x9c \\ \xdc <\xbc |\xfc \x02 \x82 B\xc2 \" \xa2 b\xe2 \x12 \x92 R\xd2 2\xb2 r\xf2 \n \x8a J\xca *\xaa j\xea \x1a \x9a Z\xda :\xba z\xfa \x06 \x86 F\xc6 &\xa6 f\xe6 \x16 \x96 V\xd6 6\xb6 v\xf6 \x0e \x8e N\xce .\xae n\xee \x1e \x9e ^\xde >\xbe ~\xfe \x01 \x81 A\xc1 !\xa1 a\xe1 \x11 \x91 Q\xd1 1\xb1 q\xf1 \t \x89 I\xc9 )\xa9 i\xe9 \x19 \x99 Y\xd9 9\xb9 y\xf9 \x05 \x85 E\xc5 %\xa5 e\xe5 \x15 \x95 U\xd5 5\xb5 u\xf5 \r \x8d M\xcd -\xad m\xed \x1d \x9d ]\xdd =\xbd }\xfd \x03 \x83 C\xc3 #\xa3 c\xe3 \x13 \x93 S\xd3 3\xb3 s\xf3 \x0b \x8b K\xcb +\xab k\xeb \x1b \x9b [\xdb ;\xbb {\xfb \x07 \x87 G\xc7 '\xa7 g\xe7 \x17 \x97 W\xd7 7\xb7 w\xf7 \x0f \x8f O\xcf /\xaf o\xef \x1f \x9f _\xdf ?\xbf \x7f \xff "
36
+ mirrortable = (
37
+ b"\x00 \x80 @\xc0 \xa0 `\xe0 \x10 \x90 P\xd0 0\xb0 p\xf0 "
38
+ b"\x08 \x88 H\xc8 (\xa8 h\xe8 \x18 \x98 X\xd8 8\xb8 x\xf8 "
39
+ b"\x04 \x84 D\xc4 $\xa4 d\xe4 \x14 \x94 T\xd4 4\xb4 t\xf4 "
40
+ b"\x0c \x8c L\xcc ,\xac l\xec \x1c \x9c \\ \xdc <\xbc |\xfc "
41
+ b'\x02 \x82 B\xc2 "\xa2 b\xe2 \x12 \x92 R\xd2 2\xb2 r\xf2 '
42
+ b"\n \x8a J\xca *\xaa j\xea \x1a \x9a Z\xda :\xba z\xfa "
43
+ b"\x06 \x86 F\xc6 &\xa6 f\xe6 \x16 \x96 V\xd6 6\xb6 v\xf6 "
44
+ b"\x0e \x8e N\xce .\xae n\xee \x1e \x9e ^\xde >\xbe ~\xfe "
45
+ b"\x01 \x81 A\xc1 !\xa1 a\xe1 \x11 \x91 Q\xd1 1\xb1 q\xf1 "
46
+ b"\t \x89 I\xc9 )\xa9 i\xe9 \x19 \x99 Y\xd9 9\xb9 y\xf9 "
47
+ b"\x05 \x85 E\xc5 %\xa5 e\xe5 \x15 \x95 U\xd5 5\xb5 u\xf5 "
48
+ b"\r \x8d M\xcd -\xad m\xed \x1d \x9d ]\xdd =\xbd }\xfd "
49
+ b"\x03 \x83 C\xc3 #\xa3 c\xe3 \x13 \x93 S\xd3 3\xb3 s\xf3 "
50
+ b"\x0b \x8b K\xcb +\xab k\xeb \x1b \x9b [\xdb ;\xbb {\xfb "
51
+ b"\x07 \x87 G\xc7 '\xa7 g\xe7 \x17 \x97 W\xd7 7\xb7 w\xf7 "
52
+ b"\x0f \x8f O\xcf /\xaf o\xef \x1f \x9f _\xdf ?\xbf \x7f \xff "
53
+ )
26
54
27
55
28
56
def checksum (data , start , count ):
@@ -32,24 +60,17 @@ def checksum(data, start, count):
32
60
return cs
33
61
34
62
35
- PRINTER_CAT = PrinterConfig (width = 384 , service = 0xA3E0 , characteristic = 0xAE01 )
36
-
37
63
MODE_TEXT = "MODE_TEXT"
38
64
MODE_BITMAP = "MODE_BITMAP"
39
65
40
- printers = {"GB02" : PRINTER_CAT , "GB01" : PRINTER_CAT }
41
-
42
66
class CatPrinter (Service ):
43
67
44
68
uuid = StandardUUID (0xAE30 )
45
69
46
- _tx = StreamIn (uuid = StandardUUID (0xAE01 ),
47
- timeout = 1.0 ,
48
- buffer_size = 256 ,
49
- )
70
+ _tx = StreamIn (uuid = StandardUUID (0xAE01 ), timeout = 1.0 , buffer_size = 256 )
50
71
51
72
def _write_data (self , buf ):
52
- result = self ._tx .write (buf )
73
+ self ._tx .write (buf )
53
74
54
75
@property
55
76
def bitmap_width (self ):
@@ -59,7 +80,6 @@ def __init__(self, service=None):
59
80
super ().__init__ (service = service )
60
81
self ._mode = None
61
82
62
-
63
83
@property
64
84
def mode (self ):
65
85
return self ._mode
@@ -80,14 +100,12 @@ def mode(self, value):
80
100
81
101
def feed_lines (self , lines ):
82
102
buf = bytearray (paperfeed )
83
- buf [6 ] = lines & 0xff
103
+ buf [6 ] = lines & 0xFF
84
104
buf [7 ] = lines >> 8
85
105
buf [8 ] = checksum (buf , 6 , 2 )
86
106
self ._write_data (buf )
87
107
88
108
def _print_common (self , text , reverse_bits = True ):
89
-
90
- offset = 0
91
109
data = memoryview (text )
92
110
while data :
93
111
sz = min (112 , len (data ))
0 commit comments