-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptstatus.py
More file actions
245 lines (225 loc) · 6.3 KB
/
Copy pathptstatus.py
File metadata and controls
245 lines (225 loc) · 6.3 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
#!/usr/bin/env python3
import ctypes
import sys
import contextlib
import bluetooth
import ptcbp
POWER = {
0: 'Battery full',
1: 'Battery half',
2: 'Battery low',
3: 'Battery critical',
4: 'AC',
}
MODELS = {
0x38: 'QL-800',
0x39: 'QL-810W',
0x41: 'QL-820NWB',
0x66: 'PT-E550W',
0x68: 'PT-P750W',
0x6f: 'PT-P900W',
0x70: 'PT-P950NW',
0x72: 'PT-P300BT',
}
ERR_FLAGS = {
0: 'Replace media',
1: 'Expansion buffer full',
2: 'Communication error',
3: 'Communication buffer full',
4: 'Cover opened',
5: 'Overheat/Cancelled on printer side',
6: 'Feed error',
7: 'General system error',
8: 'Media not loaded',
9: 'End of media (Page too long)',
10: 'Cutter jammed',
11: 'Low battery',
12: 'Printer in use',
13: 'Printer not powered',
14: 'Overvoltage',
15: 'Fan error',
}
TAPE_TYPE = {
0x00: 'Not loaded',
0x01: 'Laminated (TZexxx)',
0x03: 'Non-laminated (TZeNxxx)',
0x11: 'Heat shrink tube (HSexxx)',
0x4a: 'Continuous tape',
0x4b: 'Die-cut labels',
0xff: 'Unsupported',
}
PHASES = {
0x000000: 'Ready',
0x000001: 'Feed',
0x010000: 'Printing',
0x010014: 'Cover open while receiving',
}
TAPE_BGCOLORS = {
0x00: 'None',
0x01: 'White',
0x02: 'Other',
0x03: 'Clear',
0x04: 'Red',
0x05: 'Blue',
0x06: 'Yellow',
0x07: 'Green',
0x08: 'Black',
0x09: 'Clear (White text)',
0x20: 'Matte white',
0x21: 'Matte clear',
0x22: 'Matte silver',
0x23: 'Satin gold',
0x24: 'Satin silver',
0x30: 'Blue (D)',
0x31: 'Red (D)',
0x40: 'Fluorescent orange',
0x41: 'Fluorescent yellow',
0x50: 'Berry pink (S)',
0x51: 'Light gray (S)',
0x52: 'Lime green (S)',
0x60: 'Yellow (F)',
0x61: 'Pink (F)',
0x62: 'Blue (F)',
0x70: 'White (Heat shrink tube)',
0x90: 'White (Flex ID)',
0x91: 'Yellow (Flex ID)',
0xf0: 'Printing head cleaner',
0xf1: 'Stencil',
0xff: 'Unsupported',
}
TAPE_FGCOLORS = {
0x00: 'None',
0x01: 'White',
0x02: 'Other',
0x04: 'Red',
0x05: 'Blue',
0x08: 'Black',
0x0a: 'Gold',
0x62: 'Blue (F)',
0xf0: 'Printing head cleaner',
0xf1: 'Stencil',
0xff: 'Unsupported',
}
PRINT_FLAGS = {
6: 'Auto cut',
7: 'Hardware mirroring',
}
STATUS_TYPE = {
0x00: "Reply to status request",
0x01: "Printing completed",
0x02: "Error occured",
0x03: "IF mode finished",
0x04: "Power off",
0x05: "Notification",
0x06: "Phase change",
}
NOTIFICATIONS = {
0x00: 'N/A',
0x01: 'Cover open',
0x02: 'Cover close',
}
class StatusRegister(ctypes.BigEndianStructure):
_fields_ = (
('magic', ctypes.c_char * 4),
('model', ctypes.c_uint8),
('country', ctypes.c_uint8),
('_err2', ctypes.c_uint8),
('_power', ctypes.c_uint8),
('err', ctypes.c_uint16),
('tape_width', ctypes.c_uint8),
('tape_type', ctypes.c_uint8),
('colors', ctypes.c_uint8),
('fonts', ctypes.c_uint8),
('_sbz0', ctypes.c_uint8),
('mode', ctypes.c_uint8),
('density', ctypes.c_uint8),
('tape_length', ctypes.c_uint8),
('status_type', ctypes.c_uint8),
('phase_type', ctypes.c_uint8),
('phase', ctypes.c_uint16),
('notification', ctypes.c_uint8),
('expansion_area', ctypes.c_uint8),
('tape_bgcolor', ctypes.c_uint8),
('tape_fgcolor', ctypes.c_uint8),
('hw_settings', ctypes.c_uint32),
('_sbz1', ctypes.c_uint8 * 2),
)
describe_code = lambda code, table: f'{table.get(code, "Unknown")} (0x{code:02x})'
def describe_flag(flagset, descset):
flags = []
ctr = 0
if flagset == 0:
return 'None'
while flagset != 0:
flag = flagset & 1
if flag:
flags.append(descset.get(ctr, 'bit{}'.format(ctr)))
ctr += 1
flagset >>= 1
return ', '.join(flags)
def print_status(stat, verbose=False):
# 0:4
if bytes(stat.magic) != b'\x80\x20B0':
raise RuntimeError('Invalid magic')
# 4
print(f'Model: {describe_code(stat.model, MODELS)}')
# 5:8
if (verbose):
print(f'Country: 0x{stat.country:02x}')
print(f'Extended error: 0x{stat._err2:02x}')
print(f'Power: {describe_code(stat._power, POWER)}')
# 8:12
print(f'Errors: {describe_flag(stat.err, ERR_FLAGS)}')
print(f'Tape width: {stat.tape_width}mm')
print(f'Tape type: {describe_code(stat.tape_type, TAPE_TYPE)}')
# 12:15
if (verbose):
pass
# 15
print(f'Print flags: {describe_flag(stat.mode, PRINT_FLAGS)}')
# 16
if (verbose):
pass
# 17
# This would be uglier if written as an f string, so just leave as-is
print('Fixed label length: {}'.format('{}mm'.format(stat.tape_length) if stat.tape_length != 0 else 'N/A'))
# 18
print(f'Status: {describe_code(stat.status_type, STATUS_TYPE)}')
# 19:22
print(f'Phase: {describe_code(stat.phase_type << 16 | stat.phase, PHASES)}')
# 22
print(f'Notification: {describe_code(stat.notification, NOTIFICATIONS)}')
# 23
if (verbose):
print(f'Expansion size: 0x{stat.expansion_area:02x}')
# 24:26
print(f'Tape background: {describe_code(stat.tape_bgcolor, TAPE_BGCOLORS)}')
print(f'Tape foreground: {describe_code(stat.tape_fgcolor, TAPE_FGCOLORS)}')
# 26
if (verbose):
print(f'Hardware settings: 0x{stat.hw_settings:08x}')
def unpack_status(bytes_):
if len(bytes_) != 32:
raise ValueError('Status must be exactly 32 bytes long.')
status = StatusRegister()
ctypes.memmove(ctypes.addressof(status), bytes_, ctypes.sizeof(status))
return status
if __name__ == '__main__':
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} <addr> [ch]')
exit(1)
addr = sys.argv[1]
if len(sys.argv) < 3:
ch = 1
else:
ch = int(sys.argv[2])
with contextlib.closing(bluetooth.BluetoothSocket(bluetooth.RFCOMM)) as sock:
sock.connect((addr, ch))
sock.send(b'\x00'*64)
sock.send(ptcbp.serialize_control('reset'))
sock.send(ptcbp.serialize_control('get_status'))
resp = StatusRegister()
buf = sock.recv(32)
ctypes.memmove(ctypes.addressof(resp), buf, ctypes.sizeof(resp))
print(buf)
print_status(resp, verbose=True)