Hey there!
I'm one of many happy owners of adafruit's sweet thermal printer.
I have connected it to my raspberry pi and am very thankfully working with your Python-Thermal-Printer library.
As I have mounted the printer to the frontside of a box I need the prints to be upside down so that they can be read when standing in front of the box. I was glad to find the upsideDownOn() Method in the library and gave it a try.
Unfortunately it only works partially. The text is now upside down but the lines are still in the old order. So if I print a string that'll go over 2 rows, the printer first prints row 2/2 and then row 1/2. Check the attached image for example.
Am I doing something wrong here or is that just the way it is?
This is the python script I'm running:
#!/usr/bin/python
import sys
sys.path.append('Python-Thermal-Printer')
from Adafruit_Thermal import *
from PIL import Image
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
printer.upsideDownOn()
im = Image.open("heart.bmp")
text = "I am a little dummy text and I am so long that I need two rows."
printer.println(text)
printer.feed(7)
Thermal Printer upsideDownOn() Method
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 3
- Joined: Fri Jul 04, 2014 9:56 am
Thermal Printer upsideDownOn() Method
- Attachments
-
- IMG_1245.JPG (119.33 KiB) Viewed 1242 times
-
- Posts: 3
- Joined: Fri Jul 04, 2014 9:56 am
Re: Thermal Printer upsideDownOn() Method
Okidoki, so I solved this myself.
There are two ways:
1. Use pythons textwrap module (this is quite easy PLUS you get automatic line breaks)
2. If you don't want automatic line breaking you need to apply some loop and list magic
My function for 1.:
My function for solution 2.:
Always keep in Mind that this stuff regards upsideDown printing mode ON.
There are two ways:
1. Use pythons textwrap module (this is quite easy PLUS you get automatic line breaks)
2. If you don't want automatic line breaking you need to apply some loop and list magic
My function for 1.:
Code: Select all
import textwrap
def textWrapped(text, maxColumn): #maxColumn can be fetched from Adafruit_Thermal.py (it is 32)
textWrapped = textwrap.wrap(text, width=maxColumn)
for i in range(len(textWrapped)):
textWrapped[i]+='\n'
textWrappedReversed = textWrapped[::-1]
stringForPrinter = ''.join(list(textWrappedReversed))
return stringForPrinter
My function for solution 2.:
Code: Select all
#here, too maxColumn can be fetched from Adafruit_Thermal.py but for some logic reason here it must be 31! I was too lazy to find out why.
def textFullColumn(text, maxColumn):
textChars = []
textLines = []
charCount = 0
width = maxColumn
for i in range(len(text)):
char = text[i]
textChars.append(char)
if i == len(text) - 1 and (charCount < width):
for j in range(width-charCount):
textChars.append(' ')
if ((char == '\n') or
(charCount == width)):
textLines.append(textChars)
#print textLines
textChars = []
charCount = 0
#print i
elif i == len(text) - 1:
textLines.append(textChars)
textChars = []
charCount = 0
else:
charCount += 1
textLinesReversed = itertools.chain(*textLines[::-1])
stringForPrinter = ''.join(list(textLinesReversed))
return stringForPrinter
Always keep in Mind that this stuff regards upsideDown printing mode ON.
- adafruit_support_mike
- Posts: 67454
- Joined: Thu Feb 11, 2010 2:51 pm
Re: Thermal Printer upsideDownOn() Method
Nice! Thanks for posting the solution. ;-)
- kdarius
- Posts: 8
- Joined: Thu Sep 24, 2015 4:39 pm
Re: Thermal Printer upsideDownOn() Method
Hello I am trying to get the printer to print upside down I got to the same spot as frosenberger but when i try to implement his code i get stringForPrinter is not defined. Please see the attached to see my code.
- Attachments
-
- printer.JPG (48.72 KiB) Viewed 1080 times
- adafruit_support_mike
- Posts: 67454
- Joined: Thu Feb 11, 2010 2:51 pm
Re: Thermal Printer upsideDownOn() Method
The variable stringForPrinter is only defined inside the function textWrapped. Your last line tries to use it outside the function, where it doesn't have any meaning.
You need something like this:
You need something like this:
Code: Select all
output = textWrapped( "this is a long string of text that will need to be wrapped", 32 )
printer.println( output )
- kdarius
- Posts: 8
- Joined: Thu Sep 24, 2015 4:39 pm
Re: Thermal Printer upsideDownOn() Method
cool so i got it to some what work but if you look at the attached picture start and end are off. also here is my new code
Code: Select all
from Adafruit_Thermal import *
import textwrap
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
printer.upsideDownOn()
def textWrapped(text, maxColumn):
textWrapped = textwrap.wrap(text, width=maxColumn)
for i in range(len(textWrapped)):
textWrapped[i]+='n'
textWrappedReversed = textWrapped[::-1]
stringForPrinter = ''.join(list(textWrappedReversed))
return stringForPrinter
output = textWrapped("this is a long string of text that will need to be wrapped",printer.maxColumn)
printer.println(output)
printer.feed(1)
printer.sleep() # Tell printer to sleep
printer.wake() # Call wake() before printing again, even if reset
printer.setDefault() # Restore printer to defaults
- Attachments
-
- 20150925_101138_resized.jpg (763.42 KiB) Viewed 1060 times
- kdarius
- Posts: 8
- Joined: Thu Sep 24, 2015 4:39 pm
Re: Thermal Printer upsideDownOn() Method
the issue was I had 'n' instead of '\n'
oh how typos change everything
oh how typos change everything
- kdarius
- Posts: 8
- Joined: Thu Sep 24, 2015 4:39 pm
Re: Thermal Printer upsideDownOn() Method
so I have everything working not and am implementing it into twitter. everything is working good except for one part. If you look at the image you can see some of the tweets are missing the top part of the first line. there is even one duplicate in there.
- Attachments
-
- 20150925_135445_resized.jpg (850.83 KiB) Viewed 1043 times
- kdarius
- Posts: 8
- Joined: Thu Sep 24, 2015 4:39 pm
Re: Thermal Printer upsideDownOn() Method
anyone have any thoughts to why this is happening?
- Keebie81
- Posts: 157
- Joined: Wed Sep 10, 2014 6:53 pm
Re: Thermal Printer upsideDownOn() Method
Can you code it to put a blank line in after a tweet This would give it a buffer space to print the next line. Maybe that would help
- kdarius
- Posts: 8
- Joined: Thu Sep 24, 2015 4:39 pm
Re: Thermal Printer upsideDownOn() Method
right now it is coded to have a space in-between the tweets. such as the tweets at the bottom between the Pres... and #Apple has release. I just don't know why sometimes it works and other times it has the issue.
- kdarius
- Posts: 8
- Joined: Thu Sep 24, 2015 4:39 pm
Re: Thermal Printer upsideDownOn() Method
right now it is coded to have a space in-between the tweets. such as the tweets at the bottom between the Pres... and #Apple has release. I just don't know why sometimes it works and other times it has the issue.
- kdarius
- Posts: 8
- Joined: Thu Sep 24, 2015 4:39 pm
Re: Thermal Printer upsideDownOn() Method
anyone have any thoughts on this?
- pburgess
- Posts: 4161
- Joined: Sun Oct 26, 2008 2:29 am
Re: Thermal Printer upsideDownOn() Method
That's strange. You can see it's not a feed problem (which would appear as a mostly black line); the line of text is itself shorter and not rendered in its entirety, which makes me think it's either a firmware bug or just a not-well-documented behavior, since the other lines appear correctly. When doing the text wrap in Python, is it appending a newline char at the end of each line? Carriage return? Both? You might try appending the same to the string before issuing it to the printer.
- maxhfowler
- Posts: 4
- Joined: Fri Apr 22, 2016 12:12 am
Re: Thermal Printer upsideDownOn() Method
Here is my code example (I took from this forum from the OP who it was at least working for on a line by line basis):
Code: Select all
#!/usr/bin/python
import sys
sys.path.append('Python-Thermal-Printer')
from Adafruit_Thermal import *
from PIL import Image
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
printer.upsideDownOn()
text = "Brair rabbit went to the briar patch and hooboy it was prickly"
printer.println(text)
printer.feed(7)
printer.upsideDownOff()
printer.println(text)
printer.feed(7)
If anyone has any ideas of how to debug this please let me know! I am at a loss. Thank you.
Please be positive and constructive with your questions and comments.