3232# - Trap errors properly. Some stuff just falls through right now.
3333# - Add docstrings throughout!
3434
35- # Python 2.X code using the library usu. needs to include the next line:
36- from __future__ import print_function
3735from serial import Serial
3836import time
3937import sys
38+ import math
4039
4140class Adafruit_Thermal (Serial ):
4241
@@ -181,12 +180,12 @@ def setTimes(self, p, f):
181180 def writeBytes (self , * args ):
182181 if self .writeToStdout :
183182 for arg in args :
184- sys .stdout .write (chr ( arg ))
183+ sys .stdout .write (bytes ([ arg ] ))
185184 else :
186- self .timeoutWait ()
187- self .timeoutSet (len (args ) * self .byteTime )
188185 for arg in args :
189- super (Adafruit_Thermal , self ).write (chr (arg ))
186+ self .timeoutWait ()
187+ self .timeoutSet (len (args ) * self .byteTime )
188+ super (Adafruit_Thermal , self ).write (bytes ([arg ]))
190189
191190 # Override write() method to keep track of paper feed.
192191 def write (self , * data ):
@@ -262,7 +261,7 @@ def setDefault(self):
262261 self .setCodePage ()
263262
264263 def test (self ):
265- self .write ("Hello world!" )
264+ self .write ("Hello world!" . encode ( 'cp437' , 'ignore' ) )
266265 self .feed (2 )
267266
268267 def testPage (self ):
@@ -341,20 +340,20 @@ def printBarcode(self, text, type):
341340 n = len (text )
342341 if n > 255 : n = 255
343342 if self .writeToStdout :
344- sys .stdout .write (chr (n ))
343+ sys .stdout .write (( chr (n )). encode ( 'cp437' , 'ignore' ))
345344 for i in range (n ):
346- sys .stdout .write (text [i ])
345+ sys .stdout .write (text [i ]. encode ( 'utf-8' , 'ignore' ) )
347346 else :
348- super (Adafruit_Thermal , self ).write (chr (n ))
347+ super (Adafruit_Thermal , self ).write (( chr (n )). encode ( 'utf-8' , 'ignore' ))
349348 for i in range (n ):
350349 super (Adafruit_Thermal ,
351- self ).write (text [i ])
350+ self ).write (text [i ]. encode ( 'utf-8' , 'ignore' ) )
352351 else :
353352 # Older firmware: write string + NUL
354353 if self .writeToStdout :
355- sys .stdout .write (text )
354+ sys .stdout .write (text . encode ( 'utf-8' , 'ignore' ) )
356355 else :
357- super (Adafruit_Thermal , self ).write (text )
356+ super (Adafruit_Thermal , self ).write (text . encode ( 'utf-8' , 'ignore' ) )
358357 self .prevByte = '\n '
359358
360359 # === Character commands ===
@@ -461,7 +460,7 @@ def feed(self, x=1):
461460 # datasheet claims sending bytes 27, 100, <x> works,
462461 # but it feeds much more than that. So, manually:
463462 while x > 0 :
464- self .write ('\n ' )
463+ self .write ('\n ' . encode ( 'cp437' , 'ignore' ) )
465464 x -= 1
466465
467466 # Feeds by the specified number of individual pixel rows
@@ -504,7 +503,7 @@ def underlineOff(self):
504503 self .writeBytes (27 , 45 , 0 )
505504
506505 def printBitmap (self , w , h , bitmap , LaaT = False ):
507- rowBytes = ( w + 7 ) / 8 # Round up to next byte boundary
506+ rowBytes = math . floor (( w + 7 ) / 8 ) # Round up to next byte boundary
508507 if rowBytes >= 48 :
509508 rowBytesClipped = 48 # 384 pixels max width
510509 else :
@@ -531,11 +530,10 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
531530 for y in range (chunkHeight ):
532531 for x in range (rowBytesClipped ):
533532 if self .writeToStdout :
534- sys .stdout .write (
535- chr (bitmap [i ]))
533+ sys .stdout .write (bytes ([bitmap [i ]]))
536534 else :
537535 super (Adafruit_Thermal ,
538- self ).write (chr ( bitmap [i ]))
536+ self ).write (bytes ([ bitmap [i ] ]))
539537 i += 1
540538 i += rowBytes - rowBytesClipped
541539 self .timeoutSet (chunkHeight * self .dotPrintTime )
@@ -549,17 +547,17 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
549547 # For any other behavior (scale, B&W threshold, etc.), use
550548 # the Imaging Library to perform such operations before
551549 # passing the result to this function.
552- def printImage (self , image , LaaT = False ):
550+ def printImage (self , image_file , LaaT = False ):
553551 from PIL import Image
554-
552+ image = Image . open ( image_file )
555553 if image .mode != '1' :
556554 image = image .convert ('1' )
557555
558556 width = image .size [0 ]
559557 height = image .size [1 ]
560558 if width > 384 :
561559 width = 384
562- rowBytes = ( width + 7 ) / 8
560+ rowBytes = math . floor (( width + 7 ) / 8 )
563561 bitmap = bytearray (rowBytes * height )
564562 pixels = image .load ()
565563
@@ -726,11 +724,11 @@ def setCharSpacing(self, spacing):
726724 # with existing code written for the Arduino library.
727725 def print (self , * args , ** kwargs ):
728726 for arg in args :
729- self .write (str (arg ))
727+ self .write (( str (arg )). encode ( 'cp437' , 'ignore' ))
730728
731729 # For Arduino code compatibility again
732730 def println (self , * args , ** kwargs ):
733731 for arg in args :
734- self .write (str (arg ))
735- self .write ('\n ' )
732+ self .write (( str (arg )). encode ( 'cp437' , 'ignore' ))
733+ self .write ('\n ' . encode ( 'cp437' , 'ignore' ) )
736734
0 commit comments