Skip to content

Add minimal support for templates #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions brotherlabel/pt_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,32 @@ def print_command() -> bytes:
@staticmethod
def print_command_with_feeding() -> bytes:
return b'\x1A'

@staticmethod
def choose_template(template=1) -> bytes:
assert template < 100
id = str(int(0))
id += str(int(int(template)/10))
id += str(int(int(template)%10))
return b'\x5E\x54\x53' + bytes(id, 'UTF-8')

@staticmethod
def select_object(name) -> bytes:
return b'\x5E\x4F\x4E' + bytes(name, 'UTF-8') + b'\x00'

@staticmethod
def directly_insert_object(text_string) -> bytes:
data = bytes(text_string, 'UTF-8')
return b'\x5E\x44\x49' + struct.pack('<H', len(data)) + data

@staticmethod
def template_print() -> bytes:
return b'\x5E\x46\x46'

@staticmethod
def initialize_dynamic_settings() -> bytes:
return b'\x5E\x49\x49'

@staticmethod
def initialize_template_data() -> bytes:
return b'\x5E\x49\x44'
15 changes: 15 additions & 0 deletions brotherlabel/pt_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,18 @@ def print(self, images: [Image]) -> Status:
raise TimeoutError("Status did not received")

return status

class PTTemplatePrinter(object):
def __init__(self, backend: Backend):
self.backend = backend

def print(self, id, custom_fields):
data = Commands.initialize_dynamic_settings()
data += Commands.initialize_template_data()
data += Commands.choose_template(id)
assert type(custom_fields) is dict
for key in custom_fields.keys():
data += Commands.select_object(key)
data += Commands.directly_insert_object(custom_fields[key])
data += Commands.template_print()
self.backend.write(data)
23 changes: 23 additions & 0 deletions example_template_usb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python

# Prequisities
# 1. Set up the printer.
# Using the P-touch Template Settings tool, specify the initial
# printer settings according to the host system environment
# or the host that the printer is connected to
#
# 2. Design the template.
# Using P-touch Editor, design the template to be transferred
# to the printer.
#
# 3. Transfer the templates.
# Using P-touch Transfer Manager, transfer the templates to
# the printer.

import brotherlabel

backend = brotherlabel.USBBackend("usb://04f9:2085")
printer = brotherlabel.PTTemplatePrinter(backend)

# NOTE: Modify template id and custom fields to correspond the template you have designed
printer.print(1, {'Object1': 'text1', 'Object2': 'text2'})
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup

setup(
name='py-brotherlabel',
name='py-brotherlabel-casambi',
version='0.1.0',
description='Raster print package for Brother label printers',
author='Masato Mizuta',
Expand Down