Skip to content

Commit 9ec21a8

Browse files
authored
Merge pull request #78 from rbaron/energy-fix
Add support for darker prints
2 parents 2a4d0dc + 836c95e commit 9ec21a8

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ $ pip install -r requirements.txt
1919
# Usage
2020
```bash
2121
$ ./print.py --help
22-
usage: print.py [-h] [-l {debug,info,warn,error}]
23-
[-b {mean-threshold,floyd-steinberg,halftone,none}] [-s]
24-
[-d DEVICE] [-t]
22+
usage: print.py [-h] [-l {debug,info,warn,error}] [-b {mean-threshold,floyd-steinberg,atkinson,halftone,none}] [-s] [-d DEVICE] [-e ENERGY]
2523
filename
2624

2725
prints an image on your cat thermal printer
@@ -33,19 +31,15 @@ options:
3331
-h, --help show this help message and exit
3432
-l {debug,info,warn,error}, --log-level {debug,info,warn,error}
3533
-b {mean-threshold,floyd-steinberg,atkinson,halftone,none}, --img-binarization-algo {mean-threshold,floyd-steinberg,atkinson,halftone,none}
36-
Which image binarization algorithm to use. If 'none'
37-
is used, no binarization will be used. In this case
38-
the image has to have a width of 384 px.
39-
-s, --show-preview If set, displays the final image and asks the user for
40-
confirmation before printing.
34+
Which image binarization algorithm to use. If 'none' is used, no binarization will be used. In this case the image has to
35+
have a width of 384 px.
36+
-s, --show-preview If set, displays the final image and asks the user for confirmation before printing.
4137
-d DEVICE, --device DEVICE
42-
The printer's Bluetooth Low Energy (BLE) address (MAC
43-
address on Linux; UUID on macOS) or advertisement name
44-
(e.g.: "GT01", "GB02", "GB03"). If omitted, the the
45-
script will try to auto discover the printer based on
46-
its advertised BLE services.
47-
-t, --darker Print the image in text mode. This leads to more
48-
contrast, but slower speed.
38+
The printer's Bluetooth Low Energy (BLE) address (MAC address on Linux; UUID on macOS) or advertisement name (e.g.:
39+
"GT01", "GB02", "GB03"). If omitted, the the script will try to auto discover the printer based on its advertised BLE
40+
services.
41+
-e ENERGY, --energy ENERGY
42+
Thermal energy. Between 0x0000 (light) and 0xffff (darker, default).
4943
```
5044
5145
# Example

catprinter/cmds.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,25 @@ def cmd_set_energy(val):
9494
0,
9595
0xff,
9696
])
97-
b_arr[7] = chk_sum(b_arr, 6, 2)
97+
b_arr[8] = chk_sum(b_arr, 6, 2)
98+
return bs(b_arr)
99+
100+
101+
def cmd_apply_energy():
102+
b_arr = bs(
103+
[
104+
81,
105+
120,
106+
-66,
107+
0,
108+
1,
109+
0,
110+
1,
111+
0,
112+
0xff,
113+
]
114+
)
115+
b_arr[7] = chk_sum(b_arr, 6, 1)
98116
return bs(b_arr)
99117

100118

@@ -167,13 +185,12 @@ def cmd_print_row(img_row):
167185
return b_arr
168186

169187

170-
def cmds_print_img(img, dark_mode=False):
171-
172-
PRINTER_MODE = CMD_PRINT_TEXT if dark_mode else CMD_PRINT_IMG
173-
188+
def cmds_print_img(img, energy: int = 0xffff):
174189
data = \
175190
CMD_GET_DEV_STATE + \
176191
CMD_SET_QUALITY_200_DPI + \
192+
cmd_set_energy(energy) + \
193+
cmd_apply_energy() + \
177194
CMD_LATTICE_START
178195
for row in img:
179196
data += cmd_print_row(row)

print.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def parse_args():
3535
'If omitted, the the script will try to auto discover '
3636
'the printer based on its advertised BLE services.'
3737
))
38-
args.add_argument('-t', '--darker', action='store_true',
39-
help="Print the image in text mode. This leads to more contrast, \
40-
but slower speed.")
38+
args.add_argument('-e', '--energy', type=lambda h: int(h.removeprefix("0x"), 16),
39+
help="Thermal energy. Between 0x0000 (light) and 0xffff (darker, default).",
40+
default="0xffff")
4141
return args.parse_args()
4242

4343

@@ -72,7 +72,7 @@ def main():
7272
return
7373

7474
logger.info(f'✅ Read image: {bin_img.shape} (h, w) pixels')
75-
data = cmds_print_img(bin_img, dark_mode=args.darker)
75+
data = cmds_print_img(bin_img, energy=args.energy)
7676
logger.info(f'✅ Generated BLE commands: {len(data)} bytes')
7777

7878
# Try to autodiscover a printer if --device is not specified.

0 commit comments

Comments
 (0)