From 262b2cb0e8881ae3a1bebf9fceff0dab7d360bf9 Mon Sep 17 00:00:00 2001 From: Derek Olson Date: Fri, 9 Feb 2018 14:41:43 -0600 Subject: [PATCH 1/2] Initial implementation of DTR handshake (not working) --- Adafruit_Thermal.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index 4c2cd3d..610616f 100644 --- a/Adafruit_Thermal.py +++ b/Adafruit_Thermal.py @@ -59,6 +59,9 @@ def __init__(self, *args, **kwargs): # NEW BEHAVIOR: if no parameters given, output is written # to stdout, to be piped through 'lp -o raw' (old behavior # was to use default port & baud rate). + self.dtrEnabled = False + self.dtrPin = None + baudrate = 19200 if len(args) == 0: self.writeToStdout = True @@ -69,6 +72,10 @@ def __init__(self, *args, **kwargs): # If both passed, use those values. baudrate = args[1] + elif len(args) == 3: + self.dtrPin = args[2] + args = [args[0], args[1]] + # Firmware is assumed version 2.68. Can override this # with the 'firmware=X' argument, where X is the major # version number * 100 + the minor version number (e.g. @@ -134,6 +141,19 @@ def __init__(self, *args, **kwargs): 18, # DC2 35, # Print density (printBreakTime << 5) | printDensity) + + # Enable DTR pin if requested + if(self.dtrPin): + try: + import RPi.GPIO as GPIO + except RuntimeError: + print('Must run on Raspberry Pi for DTR Handshake') + GPIO.setmode(GPIO.BCM) + GPIO.setup(self.dtrPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + self.writeBytes(29, 0x61, (1 << 5)) + self.dtrEnabled = True + print('Printer DTR Handshake Enabled') + self.dotPrintTime = 0.03 self.dotFeedTime = 0.0021 else: @@ -153,12 +173,16 @@ def __init__(self, *args, **kwargs): # Sets estimated completion time for a just-issued task. def timeoutSet(self, x): - self.resumeTime = time.time() + x + if not self.dtrEnabled: + self.resumeTime = time.time() + x # Waits (if necessary) for the prior task to complete. def timeoutWait(self): if self.writeToStdout is False: - while (time.time() - self.resumeTime) < 0: pass + if self.dtrEnabled: + while (GPIO.input(self.dtrPin) == GPIO.HIGH): pass + else: + while (time.time() - self.resumeTime) < 0: pass # Printer performance may vary based on the power supply voltage, # thickness of paper, phase of the moon and other seemingly random From 3c797de552b8895d1687af6a48e2b159d9945b3e Mon Sep 17 00:00:00 2001 From: Derek Olson Date: Tue, 29 May 2018 11:31:05 -0500 Subject: [PATCH 2/2] Add initial DTR handshake implementation --- Adafruit_Thermal.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index 610616f..a4a90aa 100644 --- a/Adafruit_Thermal.py +++ b/Adafruit_Thermal.py @@ -38,6 +38,20 @@ import time import sys +try: + import RPi.GPIO as GPIO +except: + print('Must run on Raspberry Pi for DTR Handshake') + +ASCII_TAB = '\t' # Horizontal tab +ASCII_LF = '\n' # Line feed +ASCII_FF = '\f' # Form feed +ASCII_CR = '\r' # Carriage return +ASCII_DC2 = 18 # Device control 2 +ASCII_ESC = 27 # Escape +ASCII_FS = 28 # Field separator +ASCII_GS = 29 # Group separator + class Adafruit_Thermal(Serial): resumeTime = 0.0 @@ -144,13 +158,9 @@ def __init__(self, *args, **kwargs): # Enable DTR pin if requested if(self.dtrPin): - try: - import RPi.GPIO as GPIO - except RuntimeError: - print('Must run on Raspberry Pi for DTR Handshake') GPIO.setmode(GPIO.BCM) GPIO.setup(self.dtrPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) - self.writeBytes(29, 0x61, (1 << 5)) + self.writeBytes(29, 97, (1 << 5)) self.dtrEnabled = True print('Printer DTR Handshake Enabled') @@ -208,9 +218,9 @@ def writeBytes(self, *args): sys.stdout.write(chr(arg)) else: self.timeoutWait() - self.timeoutSet(len(args) * self.byteTime) for arg in args: super(Adafruit_Thermal, self).write(chr(arg)) + self.timeoutSet(len(args) * self.byteTime) # Override write() method to keep track of paper feed. def write(self, *data): @@ -358,7 +368,6 @@ def printBarcode(self, text, type): 29, 119, 3, # Barcode width 29, 107, n) # Barcode type self.timeoutWait() - self.timeoutSet((self.barcodeHeight + 40) * self.dotPrintTime) # Print string if self.firmwareVersion >= 264: # Recent firmware: write length byte + string sans NUL @@ -369,16 +378,17 @@ def printBarcode(self, text, type): for i in range(n): sys.stdout.write(text[i]) else: - super(Adafruit_Thermal, self).write(chr(n)) + self.writeBytes(n) for i in range(n): - super(Adafruit_Thermal, - self).write(text[i]) + self.writeBytes(ord(text[i])) else: # Older firmware: write string + NUL if self.writeToStdout: sys.stdout.write(text) else: super(Adafruit_Thermal, self).write(text) + + self.timeoutSet((self.barcodeHeight + 40) * self.dotPrintTime) self.prevByte = '\n' # === Character commands === @@ -471,7 +481,7 @@ def justify(self, value): pos = 2 else: pos = 0 - self.writeBytes(0x1B, 0x61, pos) + self.writeBytes(27, 97, pos) # Feeds by the specified number of lines def feed(self, x=1): @@ -558,8 +568,7 @@ def printBitmap(self, w, h, bitmap, LaaT=False): sys.stdout.write( chr(bitmap[i])) else: - super(Adafruit_Thermal, - self).write(chr(bitmap[i])) + self.writeBytes(bitmap[i]) i += 1 i += rowBytes - rowBytesClipped self.timeoutSet(chunkHeight * self.dotPrintTime)