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

Implement compatibility to Python3 and Raspberry Pi3 #22

Open
wants to merge 5 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
23 changes: 12 additions & 11 deletions Adafruit_Thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from __future__ import print_function
from serial import Serial
import time
import math

class Adafruit_Thermal(Serial):

Expand All @@ -58,7 +59,7 @@ def __init__(self, *args, **kwargs):
# If both passed, use those values.
baudrate = 19200
if len(args) == 0:
args = [ "/dev/serial0", baudrate ]
args = [ "/dev/ttyS0", baudrate ]
elif len(args) == 1:
args = [ args[0], baudrate ]
else:
Expand Down Expand Up @@ -167,19 +168,19 @@ def setTimes(self, p, f):

# 'Raw' byte-writing method
def writeBytes(self, *args):
self.timeoutWait()
self.timeoutSet(len(args) * self.byteTime)
for arg in args:
super(Adafruit_Thermal, self).write(chr(arg))
self.timeoutWait()
self.timeoutSet(self.byteTime)
super(Adafruit_Thermal, self).write(bytes([arg]))


# Override write() method to keep track of paper feed.
def write(self, *data):
for i in range(len(data)):
c = data[i]
if c != 0x13:
for i in range(len(data[0])):
c = data[0][i]
if ord(c) != 0x13:
self.timeoutWait()
super(Adafruit_Thermal, self).write(c)
super(Adafruit_Thermal, self).write(c.encode('cp437','ignore'))
d = self.byteTime
if ((c == '\n') or
(self.column == self.maxColumn)):
Expand Down Expand Up @@ -267,7 +268,7 @@ def printBarcode(self, text, type):
# Print string
self.timeoutWait()
self.timeoutSet((self.barcodeHeight + 40) * self.dotPrintTime)
super(Adafruit_Thermal, self).write(text)
super(Adafruit_Thermal, self).write(text.encode('utf-8', 'ignore'))
self.prevByte = '\n'
self.feed(2)

Expand Down Expand Up @@ -417,7 +418,7 @@ def underlineOff(self):


def printBitmap(self, w, h, bitmap, LaaT=False):
rowBytes = (w + 7) / 8 # Round up to next byte boundary
rowBytes = math.floor((w + 7) / 8) # Round up to next byte boundary
if rowBytes >= 48:
rowBytesClipped = 48 # 384 pixels max width
else:
Expand All @@ -444,7 +445,7 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
for y in range(chunkHeight):
for x in range(rowBytesClipped):
super(Adafruit_Thermal, self).write(
chr(bitmap[i]))
bytes([bitmap[i]]))
i += 1
i += rowBytes - rowBytesClipped
self.timeoutSet(chunkHeight * self.dotPrintTime)
Expand Down
2 changes: 1 addition & 1 deletion printertest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from Adafruit_Thermal import *

printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
printer = Adafruit_Thermal("/dev/ttyS0", 19200, timeout=5)

# Test inverse on & off
printer.inverseOn()
Expand Down