-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlabel_maker.py
316 lines (230 loc) · 8.68 KB
/
label_maker.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
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
from enum import IntEnum, IntFlag
import sys
import contextlib
import bluetooth
from label_rasterizer import encode_png, rasterize
STATUS_OFFSET_ERROR_INFORMATION_1 = 8
STATUS_OFFSET_ERROR_INFORMATION_2 = 9
STATUS_OFFSET_MEDIA_WIDTH = 10
STATUS_OFFSET_MEDIA_TYPE = 11
STATUS_OFFSET_MODE = 15
STATUS_OFFSET_MEDIA_LENGTH = 17
STATUS_OFFSET_STATUS_TYPE = 18
STATUS_OFFSET_PHASE_TYPE = 19
STATUS_OFFSET_PHASE_NUMBER= 20
STATUS_OFFSET_NOTIFICATION_NUMBER = 22
STATUS_OFFSET_TAPE_COLOR_INFORMATION = 24
STATUS_OFFSET_TEXT_COLOR_INFORMATION = 25
STATUS_OFFSET_HARDWARE_SETTINGS = 26
class ErrorInformation1(IntFlag):
NO_MEDIA = 0x01
CUTTER_JAM = 0x04
WEAK_BATTERIES = 0x08
HIGH_VOLTAGE_ADAPTER = 0x40
class ErrorInformation2(IntFlag):
WRONG_MEDIA = 0x01
COVER_OPEN = 0x10
OVERHEATING = 0x20
class MediaType(IntEnum):
NO_MEDIA = 0x00
LAMINATED_TAPE = 0x01
NON_LAMINATED_TAPE = 0x03
HEAT_SHRINK_TUBE = 0x11
INCOMPATIBLE_TAPE = 0xFF
class Mode(IntFlag):
AUTO_CUT = 0x40
MIRROR_PRITING = 0x80
class StatusType(IntEnum):
REPLY_TO_STATUS_REQUEST = 0x00
PRINTING_COMPLETED = 0x01
ERROR_OCCURRED = 0x02
TURNED_OFF = 0x04
NOTIFICATION = 0x05
PHASE_CHANGE = 0x06
class PhaseType(IntEnum):
EDITING_STATE = 0x00
PRINTING_STATE = 0x01
class PhaseNumberEditingState(IntEnum):
EDITING_STATE = 0x0000
FEED = 0x0001
class PhaseNumberPrintingState(IntEnum):
PRINTING = 0x0000
COVER_OPEN_WHILE_RECEIVING = 0x0014
class NotificationNumber(IntEnum):
NOT_AVAILABLE = 0x00
COVER_OPEN = 0x01
COVER_CLOSED = 0x02
class TapeColor(IntEnum):
WHITE = 0x01
OTHER = 0x02
CLEAR = 0x03
RED = 0x04
BLUE = 0x05
YELLOW = 0x06
GREEN = 0x07
BLACK = 0x08
CLEAR_WHITE_TEXT = 0x09
MATTE_WHITE = 0x20
MATTE_CLEAR = 0x21
MATTE_SILVER = 0x22
SATIN_GOLD = 0x23
SATIN_SILVER = 0x24
BLUE_D = 0x30
RED_D = 0x31
FLUORESCENT_ORANGE = 0x40
FLUORESCENT_YELLOW = 0x41
BERRY_PINK_S = 0x50
LIGHT_GRAY_S = 0x51
LIME_GREEN_S = 0x52
YELLOW_F = 0x60
PINK_F = 0x61
BLUE_F= 0x62
WHITE_HEAT_SHRINK_TUBE = 0x70
WHITE_FLEX_ID = 0x90
YELLOW_FLEX_ID = 0x91
CLEANING = 0xF0
STENCIL = 0xF1
INCOMPATIBLE = 0xFF
class TextColor(IntEnum):
WHITE = 0x01
OTHER = 0x02
RED = 0x04
BLUE = 0x05
BLACK = 0x08
GOLD = 0x0A
BLUE_F = 0x62
CLEANING = 0xF0
STENCIL = 0xF1
INCOMPATIBLE = 0XFF
@contextlib.contextmanager
def bt_socket_manager(*args, **kwargs):
socket = bluetooth.BluetoothSocket(*args, **kwargs)
yield socket
socket.close()
def make_label(image_path, bt_address, bt_channel):
data = encode_png(image_path)
with bt_socket_manager(bluetooth.RFCOMM) as socket:
socket.connect((bt_address, bt_channel))
send_invalidate(socket)
send_initialize(socket)
send_status_information_request(socket)
status_information = receive_status_information_response(socket)
handle_status_information(status_information)
send_switch_dynamic_command_mode(socket)
send_switch_automatic_status_notification_mode(socket)
send_print_information_command(socket, data)
send_various_mode_settings(socket)
send_advanced_mode_settings(socket)
send_specify_margin_amount(socket)
send_select_compression_mode(socket)
send_raster_data(socket, data)
send_print_command_with_feeding(socket)
while True:
status_information = receive_status_information_response(socket)
handle_status_information(status_information)
def send_invalidate(socket):
# send 100 null bytes
socket.send(b"\x00" * 100)
def send_initialize(socket):
# send [1B 40]
socket.send(b"\x1B\x40")
def send_switch_dynamic_command_mode(socket):
# set dynamic command mode to "raster mode" [1B 69 61 {01}]
socket.send(b"\x1B\x69\x61\x01")
def send_switch_automatic_status_notification_mode(socket):
# set automatic status notification mode to "notify" [1B 69 21 {00}]
socket.send(b"\x1B\x69\x21\x00")
def send_print_information_command(socket, data):
# print to 24mm tape [1B 69 7A {84 00 18 00 <data length 4 bytes> 00 00}]
socket.send(b"\x1B\x69\x7A\x84\x00\x18\x00")
socket.send((len(data) >> 4).to_bytes(4, 'little'))
socket.send(b"\x00\x00")
def send_various_mode_settings(socket):
# set to auto-cut, no mirror printing [1B 69 4D {40}]
socket.send(b"\x1B\x69\x4D")
socket.send(Mode.AUTO_CUT.to_bytes(1, "big"))
def send_advanced_mode_settings(socket):
# set print chaining off [1B 69 4B {08}]
socket.send(b"\x1B\x69\x4B\x08")
def send_specify_margin_amount(socket):
# set margin (feed) amount to 0 [1B 69 64 {00 00}]
socket.send(b"\x1B\x69\x64\x00\x00")
def send_select_compression_mode(socket):
# set to TIFF compression [4D {02}]
socket.send(b"\x4D\x02")
def send_raster_data(socket, data):
# send all raster data lines
for line in rasterize(data):
socket.send(bytes(line))
def send_print_command_with_feeding(socket):
# print and feed [1A]
socket.send(b"\x1A")
def send_status_information_request(socket):
# request status information [1B 69 53]
socket.send(b"\x1B\x69\x53")
def receive_status_information_response(socket):
# receive status information
response = socket.recv(32)
if (len(response) != 32):
sys.exit("Expected 32 bytes, but only received %d" % len(response))
return response
def handle_status_information(status_information):
def handle_reply_to_status_request(status_information):
print("Printer status")
print("--------------")
print("Media width: %dmm" % status_information[STATUS_OFFSET_MEDIA_WIDTH])
print("Media type: %s" % MediaType(status_information[STATUS_OFFSET_MEDIA_TYPE]).name)
print("Tape color information: %s" % TapeColor(status_information[STATUS_OFFSET_TAPE_COLOR_INFORMATION]).name)
print("Text color information: %s" % TextColor(status_information[STATUS_OFFSET_TEXT_COLOR_INFORMATION]).name)
print()
def handle_printing_completed(status_information):
print("Printing completed")
print("------------------")
mode = Mode(status_information[STATUS_OFFSET_MODE])
print("Mode: %s" % ", ".join([f.name for f in Mode if f in mode]))
sys.exit(0)
def handle_error_occurred(status_information):
print("Error occurred")
print("--------------")
error_information_1 = ErrorInformation1(status_information[STATUS_OFFSET_ERROR_INFORMATION_1])
error_information_2 = ErrorInformation2(status_information[STATUS_OFFSET_ERROR_INFORMATION_2])
print("Error information 1: %s" % ", ".join([f.name for f in ErrorInformation1 if f in error_information_1]))
print("Error information 2: %s" % ", ".join([f.name for f in ErrorInformation2 if f in error_information_2]))
sys.exit("An error has occurred; exiting program")
def handle_turned_off(status_information):
print("Turned off")
print("----------")
sys.exit("Device was turned off")
def handle_notification(status_information):
print("Notification")
print("------------")
print("Notification number: %s" % NotificationNumber(status_information[STATUS_OFFSET_NOTIFICATION_NUMBER]).name)
print()
def handle_phase_change(status_information):
print("Phase changed")
print("-------------")
phase_type = status_information[STATUS_OFFSET_PHASE_TYPE]
phase_number = int.from_bytes(status_information[STATUS_OFFSET_PHASE_NUMBER:STATUS_OFFSET_PHASE_NUMBER + 2], "big")
print("Phase type: %s" % PhaseType(phase_type).name)
print("Phase number: %s" % (PhaseNumberPrintingState(phase_number) if phase_type == PhaseType.PRINTING_STATE else PhaseNumberEditingState(phase_number)).name)
print()
handlers = {
StatusType.REPLY_TO_STATUS_REQUEST: handle_reply_to_status_request,
StatusType.PRINTING_COMPLETED: handle_printing_completed,
StatusType.ERROR_OCCURRED: handle_error_occurred,
StatusType.TURNED_OFF: handle_turned_off,
StatusType.NOTIFICATION: handle_notification,
StatusType.PHASE_CHANGE: handle_phase_change
}
status_type = status_information[STATUS_OFFSET_STATUS_TYPE]
handlers[status_type](status_information)
def main(*args):
if len(args) < 3:
print("Usage: %s <image-path> <bt-address> [bt-channel]" % args[0])
sys.exit(1)
image_path = args[1]
bt_address = args[2]
bt_channel = int(args[3]) if len(args) > 3 else 1
make_label(image_path, bt_address, bt_channel)
if __name__== "__main__":
main(*sys.argv)