Skip to content

Commit

Permalink
Add support for darker prints
Browse files Browse the repository at this point in the history
This adds the -e/--energy switch, which lets us specify the thermal
energy to the printer. It's a value between 0x0000 (light) and 0xffff
(darker).
  • Loading branch information
rbaron committed Sep 21, 2024
1 parent 2a4d0dc commit ac57b9c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
27 changes: 22 additions & 5 deletions catprinter/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,25 @@ def cmd_set_energy(val):
0,
0xff,
])
b_arr[7] = chk_sum(b_arr, 6, 2)
b_arr[8] = chk_sum(b_arr, 6, 2)
return bs(b_arr)


def cmd_apply_energy():
b_arr = bs(
[
81,
120,
-66,
0,
1,
0,
1,
0,
0xff,
]
)
b_arr[7] = chk_sum(b_arr, 6, 1)
return bs(b_arr)


Expand Down Expand Up @@ -167,13 +185,12 @@ def cmd_print_row(img_row):
return b_arr


def cmds_print_img(img, dark_mode=False):

PRINTER_MODE = CMD_PRINT_TEXT if dark_mode else CMD_PRINT_IMG

def cmds_print_img(img, energy: int = 0xffff):
data = \
CMD_GET_DEV_STATE + \
CMD_SET_QUALITY_200_DPI + \
cmd_set_energy(energy) + \
cmd_apply_energy() + \
CMD_LATTICE_START
for row in img:
data += cmd_print_row(row)
Expand Down
8 changes: 4 additions & 4 deletions print.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def parse_args():
'If omitted, the the script will try to auto discover '
'the printer based on its advertised BLE services.'
))
args.add_argument('-t', '--darker', action='store_true',
help="Print the image in text mode. This leads to more contrast, \
but slower speed.")
args.add_argument('-e', '--energy', type=lambda h: int(h.removeprefix("0x"), 16),
help="Thermal energy. Between 0x0000 (light) and 0xffff (darker)",
default="0xffff")
return args.parse_args()


Expand Down Expand Up @@ -72,7 +72,7 @@ def main():
return

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

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

0 comments on commit ac57b9c

Please sign in to comment.