@@ -65,6 +65,9 @@ def __init__(self, mac, printertype=PrinterType.A6, timeout=1.0):
6565 self .mac = mac
6666 self .timeout = timeout
6767 self .printerType = printertype
68+
69+ # This buffer is used for continuous printing
70+ self .printBuffer = ''
6871
6972 def isConnected (self ):
7073 """
@@ -295,6 +298,20 @@ def getRowWidth(self):
295298 return 576
296299 else :
297300 raise ValueError ('Unsupported printer type' )
301+
302+ def getRowCharacters (self ):
303+ """
304+ Returns amount of characters that may fit in a single row.
305+ By default A6+ can fit up to 48 characters, A6 can fit up to 32 characters.
306+ """
307+
308+ if self .printerType == PrinterType .A6 :
309+ # TODO: Measure amount of characters that can fit in the line
310+ return 32
311+ elif self .printerType == PrinterType .A6p :
312+ return 48
313+ else :
314+ raise ValueError ('Unsupported printer type' )
298315
299316 def getHeightLimit (self ):
300317 """
@@ -397,14 +414,17 @@ def printBreak(self, size=0x40):
397414
398415 def writeASCII (self , text = '\n ' , wait = False ):
399416 """
417+ Deprecated.
400418 Write raw ASCII string to the printer.
401419 By default this printer accepts an ascii string for printing it with raw monospace
402- font. Printer has internal buffer (48 characters in Peripage A6+ ) that will
420+ font. Printer has internal buffer (getRowCharacters() ) that will
403421 accumulate the received characters. Printer will print out the buffer if meets a '\n '
404422 character or buffer overflows.
405423 This function expects only ASCII characters without control codes (0x00-0x20, 0xFF).
406424 This function is not recommended to use while printer is in byte stream printing mode
407425 or while it expects arguments for some of it's opcodes.
426+ If string contains sequently repeating '\n ' characters, the printer may freeze. So
427+ it's recommended to use printASCII() instead.
408428
409429 :param text: string containing ASCII characters
410430 :type text: str
@@ -417,6 +437,148 @@ def writeASCII(self, text='\n', wait=False):
417437 else :
418438 self .tellPrinter (bytes (text , 'ascii' ))
419439
440+ def printlnASCII (self , text = '\n ' , delay = 0.25 ):
441+ """
442+ Write raw ASCII string to the printer.
443+ By default this printer accepts an ascii string for printing it with raw monospace
444+ font. Printer has internal buffer (getRowCharacters()) that will
445+ accumulate the received characters. Printer will print out the buffer if meets a '\n '
446+ character or buffer overflows.
447+ This function expects only ASCII characters without control codes (0x00-0x20, 0xFF).
448+ This function is not recommended to use while printer is in byte stream printing mode
449+ or while it expects arguments for some of it's opcodes.
450+ If string contains sequently repeating '\n ' characters, they will be replaced with
451+ printBreak(30) which matches the length of the '\n \n '. This function automatically
452+ slices string into pieces of size getRowCharacters() and waits till new piece being
453+ printed.
454+ This function acts as println. This function will print out the data stored in the
455+ buffer of printASCII()
456+
457+ :param text: string containing ASCII characters
458+ :type text: str
459+ :param delay: delay between sending each line
460+ :type delay: float
461+ """
462+
463+ # Remove non-ASCII & control (except \n)
464+ text = '' .join ([i for i in text if (31 < ord (i ) or ord (i ) == 10 ) and ord (i ) < 127 ])
465+
466+ # Check for empty and print out newline
467+ text = self .printBuffer + text
468+ if len (text ) == 0 :
469+ self .printBreak (30 )
470+ time .sleep (delay )
471+ return
472+
473+ lines = text .split ('\n ' )
474+ self .printBuffer = ''
475+
476+ for l in lines :
477+ # Replace every empty line with break matching newline height
478+ if len (l ) == 0 :
479+ self .printBreak (30 )
480+ time .sleep (delay )
481+ else :
482+ # Split to lines
483+ parts = [l [i :i + self .getRowCharacters ()] for i in range (0 , len (l ), self .getRowCharacters ())]
484+
485+ for i , p in enumerate (parts ):
486+ self .tellPrinter (bytes (p , 'ascii' ))
487+ if i != 0 :
488+ time .sleep (delay )
489+
490+ # Push last line from the buffer
491+ self .tellPrinter (bytes ('\n ' , 'ascii' ))
492+ time .sleep (delay )
493+
494+ def printASCII (self , text = '\n ' , delay = 0.25 ):
495+ """
496+ Write raw ASCII string to the printer.
497+ By default this printer accepts an ascii string for printing it with raw monospace
498+ font. Printer has internal buffer (getRowCharacters()) that will
499+ accumulate the received characters. Printer will print out the buffer if meets a '\n '
500+ character or buffer overflows.
501+ This function expects only ASCII characters without control codes (0x00-0x20, 0xFF).
502+ This function is not recommended to use while printer is in byte stream printing mode
503+ or while it expects arguments for some of it's opcodes.
504+ If string contains sequently repeating '\n ' characters, they will be replaced with
505+ printBreak(30) which matches the length of the '\n \n '. This function automatically
506+ slices string into pieces of size getRowCharacters() and waits till new piece being
507+ printed.
508+ This function uses in class buffer to store tail of the text if text didn't end with
509+ '\n '.
510+
511+ :param text: string containing ASCII characters
512+ :type text: str
513+ :param delay: delay between sending each line
514+ :type delay: float
515+ """
516+
517+ # Remove non-ASCII & control (except \n)
518+ text = '' .join ([i for i in text if (31 < ord (i ) or ord (i ) == 10 ) and ord (i ) < 127 ])
519+
520+ # Check for empty and print out newline
521+ text = self .printBuffer + text
522+ self .printBuffer = ''
523+ if len (text ) == 0 :
524+ return
525+
526+ endLineBreak = text [- 1 ] == '\n '
527+ lines = text .split ('\n ' )
528+
529+ for i , l in enumerate (lines ):
530+ # Replace every empty line with break matching newline height
531+ if len (l ) == 0 :
532+ self .printBreak (30 )
533+ time .sleep (delay )
534+ else :
535+ # Split to lines
536+ parts = [l [i :i + self .getRowCharacters ()] for i in range (0 , len (l ), self .getRowCharacters ())]
537+
538+ for j , p in enumerate (parts ):
539+ # If this is the last part of the text and it ends with '\n', push it
540+ if j == len (parts )- 1 :
541+ if i == len (lines )- 1 :
542+ if endLineBreak :
543+ self .tellPrinter (bytes (p , 'ascii' ))
544+ time .sleep (delay )
545+ self .tellPrinter (bytes ('\n ' , 'ascii' ))
546+ time .sleep (delay )
547+ else :
548+ self .printBuffer = p
549+
550+ # Push out the string that is a full row
551+ if len (p ) == self .getRowCharacters ():
552+ self .tellPrinter (bytes (p , 'ascii' ))
553+ time .sleep (delay )
554+ self .tellPrinter (bytes ('\n ' , 'ascii' ))
555+ time .sleep (delay )
556+ self .printBuffer = ''
557+ else :
558+ self .tellPrinter (bytes (p , 'ascii' ))
559+ time .sleep (delay )
560+ self .tellPrinter (bytes ('\n ' , 'ascii' ))
561+ time .sleep (delay )
562+ else :
563+ self .tellPrinter (bytes (p , 'ascii' ))
564+ if j != 0 :
565+ time .sleep (delay )
566+
567+ def flushASCII (self , delay = 0.25 ):
568+ """
569+ Prints out the buffer used in printASCII() followed by newline.
570+
571+ :param delay: delay between sending each line
572+ :type delay: float
573+ """
574+
575+ if len (self .printBuffer ) > 0 :
576+ self .tellPrinter (bytes (self .printBuffer , 'ascii' ))
577+ time .sleep (delay )
578+ self .tellPrinter (bytes ('\n ' , 'ascii' ))
579+ time .sleep (delay )
580+ self .printBuffer = ''
581+
420582 def printRow (self , rowbytes ):
421583 """
422584 Send array of pixels represented with rowbytes bytes to the printer.
0 commit comments