From 80f62009153acbf4e40c2f73ab11dba7e879f947 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Thu, 10 Mar 2016 16:01:35 +0100 Subject: [PATCH] FEATURE disable panel buttons --- escpos/constants.py | 5 +++ escpos/escpos.py | 23 +++++++++++++ test/test_function_panel_button.py | 54 ++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 test/test_function_panel_button.py diff --git a/escpos/constants.py b/escpos/constants.py index c4bddd3..44402a7 100644 --- a/escpos/constants.py +++ b/escpos/constants.py @@ -54,6 +54,11 @@ _CUT_PAPER = lambda m: GS + b'V' + m PAPER_FULL_CUT = _CUT_PAPER(b'\x00') # Full cut paper PAPER_PART_CUT = _CUT_PAPER(b'\x01') # Partial cut paper +# Panel buttons (e.g. the FEED button) +_PANEL_BUTTON = lambda n: ESC + b'c5' + six.int2byte(n) +PANEL_BUTTON_ON = _PANEL_BUTTON(0) # enable all panel buttons +PANEL_BUTTON_OFF = _PANEL_BUTTON(1) # disable all panel buttons + # Text format # TODO: Acquire the "ESC/POS Application Programming Guide for Paper Roll # Printers" and tidy up this stuff too. diff --git a/escpos/escpos.py b/escpos/escpos.py index f3345f8..4498456 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -674,3 +674,26 @@ class Escpos(object): self._raw(CTL_HT) elif ctl.upper() == "VT": self._raw(CTL_VT) + + def panel_buttons(self, enable=True): + """ Controls the panel buttons on the printer (e.g. FEED) + + When enable is set to False the panel buttons on the printer will be disabled. Calling the method with + enable=True or without argument will enable the panel buttons. + + If panel buttons are enabled, the function of the panel button, such as feeding, will be executed upon pressing + the button. If the panel buttons are disabled, pressing them will not have any effect. + + This command is effective until the printer is initialized, reset or power-cycled. The default is enabled panel + buttons. + + Some panel buttons will always work, especially when printer is opened. See for more information the manual + of your printer and the escpos-command-reference. + + :param enable: controls the panel buttons + :rtype: None + """ + if enable: + self._raw(PANEL_BUTTON_ON) + else: + self._raw(PANEL_BUTTON_OFF) diff --git a/test/test_function_panel_button.py b/test/test_function_panel_button.py new file mode 100644 index 0000000..403bc75 --- /dev/null +++ b/test/test_function_panel_button.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +"""tests for panel button function + +:author: `Patrick Kanzler `_ +:organization: `python-escpos `_ +:copyright: Copyright (c) 2016 `python-escpos `_ +:license: GNU GPL v3 +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +from nose.tools import with_setup + +import escpos.printer as printer +import os + +devfile = 'testfile' + + +def setup_testfile(): + """create a testfile as devfile""" + fhandle = open(devfile, 'a') + try: + os.utime(devfile, None) + finally: + fhandle.close() + + +def teardown_testfile(): + """destroy testfile again""" + os.remove(devfile) + + +@with_setup(setup_testfile, teardown_testfile) +def test_function_panel_button_on(): + """test the panel button function (enabling) by comparing output""" + instance = printer.File(devfile=devfile) + instance.panel_buttons() + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'\x1B\x63\x35\x00') + + +@with_setup(setup_testfile, teardown_testfile) +def test_function_panel_button_off(): + """test the panel button function (disabling) by comparing output""" + instance = printer.File(devfile=devfile) + instance.panel_buttons(False) + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'\x1B\x63\x35\x01')