Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Initial implementation of DTR handshake (not working!!) #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
53 changes: 43 additions & 10 deletions Adafruit_Thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,6 +73,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
Expand All @@ -69,6 +86,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.
Expand Down Expand Up @@ -134,6 +155,15 @@ def __init__(self, *args, **kwargs):
18, # DC2
35, # Print density
(printBreakTime << 5) | printDensity)

# Enable DTR pin if requested
if(self.dtrPin):
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.dtrPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
self.writeBytes(29, 97, (1 << 5))
self.dtrEnabled = True
print('Printer DTR Handshake Enabled')

self.dotPrintTime = 0.03
self.dotFeedTime = 0.0021
else:
Expand All @@ -153,12 +183,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
Expand All @@ -184,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):
Expand Down Expand Up @@ -334,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
Expand All @@ -345,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 ===
Expand Down Expand Up @@ -447,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):
Expand Down Expand Up @@ -534,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)
Expand Down