-
Notifications
You must be signed in to change notification settings - Fork 9
/
print_thread.py
67 lines (52 loc) · 2.07 KB
/
print_thread.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
import logging
import traceback
from PyQt6.QtCore import QThread, pyqtSignal
from typing import Optional
from labelmaker import LabelMaker, BUFFER_HEIGHT, PRINT_MARGIN
from labelmaker.config import LabelMakerConfig
log = logging.getLogger(__name__)
class PrintThread(QThread):
done = pyqtSignal('PyQt_PyObject')
log = pyqtSignal('PyQt_PyObject')
def __init__(self, print_image, print_device, config: Optional[LabelMakerConfig] = None):
QThread.__init__(self)
self.config = config
self.print_image = print_image
self.print_device = print_device
# run method gets called when we start the thread
def run(self):
try:
buf = bytearray()
log.info('Building bit map from image...')
# State for bit packing
bit_cursor = 8
byte = 0
for x in range(0, self.print_image.width()):
for y in range(0, BUFFER_HEIGHT):
if y < PRINT_MARGIN or y >= (BUFFER_HEIGHT - PRINT_MARGIN):
pixel = 0xffffffff
else:
pixel = self.print_image.pixel(x, y - PRINT_MARGIN)
bit_cursor -= 1
if pixel <= 0xff000000:
# print('#', end='')
byte |= (1 << bit_cursor)
else:
pass
# print(' ', end='')
if bit_cursor == 0:
# packed = unsigned_char.pack(byte)
buf.append(byte)
byte = 0
bit_cursor = 8
# print()
log.info(
'Printing label, width: {0} height: {1}'.format(self.print_image.width(), self.print_image.height()))
lm = LabelMaker(self.print_device, self.config)
lm.print_label(buf)
#self.print_device.test()
self.done.emit(None)
except Exception as x:
self.done.emit(x)
traceback.print_exc()
print('wat4')