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

Commit 262b2cb

Browse files
committed
Initial implementation of DTR handshake (not working)
1 parent 8165c22 commit 262b2cb

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Adafruit_Thermal.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def __init__(self, *args, **kwargs):
5959
# NEW BEHAVIOR: if no parameters given, output is written
6060
# to stdout, to be piped through 'lp -o raw' (old behavior
6161
# was to use default port & baud rate).
62+
self.dtrEnabled = False
63+
self.dtrPin = None
64+
6265
baudrate = 19200
6366
if len(args) == 0:
6467
self.writeToStdout = True
@@ -69,6 +72,10 @@ def __init__(self, *args, **kwargs):
6972
# If both passed, use those values.
7073
baudrate = args[1]
7174

75+
elif len(args) == 3:
76+
self.dtrPin = args[2]
77+
args = [args[0], args[1]]
78+
7279
# Firmware is assumed version 2.68. Can override this
7380
# with the 'firmware=X' argument, where X is the major
7481
# version number * 100 + the minor version number (e.g.
@@ -134,6 +141,19 @@ def __init__(self, *args, **kwargs):
134141
18, # DC2
135142
35, # Print density
136143
(printBreakTime << 5) | printDensity)
144+
145+
# Enable DTR pin if requested
146+
if(self.dtrPin):
147+
try:
148+
import RPi.GPIO as GPIO
149+
except RuntimeError:
150+
print('Must run on Raspberry Pi for DTR Handshake')
151+
GPIO.setmode(GPIO.BCM)
152+
GPIO.setup(self.dtrPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
153+
self.writeBytes(29, 0x61, (1 << 5))
154+
self.dtrEnabled = True
155+
print('Printer DTR Handshake Enabled')
156+
137157
self.dotPrintTime = 0.03
138158
self.dotFeedTime = 0.0021
139159
else:
@@ -153,12 +173,16 @@ def __init__(self, *args, **kwargs):
153173

154174
# Sets estimated completion time for a just-issued task.
155175
def timeoutSet(self, x):
156-
self.resumeTime = time.time() + x
176+
if not self.dtrEnabled:
177+
self.resumeTime = time.time() + x
157178

158179
# Waits (if necessary) for the prior task to complete.
159180
def timeoutWait(self):
160181
if self.writeToStdout is False:
161-
while (time.time() - self.resumeTime) < 0: pass
182+
if self.dtrEnabled:
183+
while (GPIO.input(self.dtrPin) == GPIO.HIGH): pass
184+
else:
185+
while (time.time() - self.resumeTime) < 0: pass
162186

163187
# Printer performance may vary based on the power supply voltage,
164188
# thickness of paper, phase of the moon and other seemingly random

0 commit comments

Comments
 (0)