Thermal printer: print bitmap from SD card
Moderators: adafruit_support_bill, adafruit

Thermal printer: print bitmap from SD card

by davidbliss on Fri Feb 01, 2013 10:35 pm

Hi,

Could someone point me to a simple example of how to print a bitmap from a file on an SD card?

The library won't let me pass it a File object, and I can't figure out how to use a Stream.
davidbliss
 
Posts: 14
Joined: Mon Jun 25, 2012 3:10 pm

Re: Thermal printer: print bitmap from SD card

by dennisma on Fri Feb 01, 2013 10:53 pm

You might want to check out this blog post: http://gofreerange.com/hello-printer.

He wrote his own library that connects the thermal printer to the Internet.

In his post he says:
  • "The thermal printer that I’ve used is the exact same one that Adafruit provide as part of their “IoT Printer Kit”, which prints out tweets matching a given keyword as they are encountered.".
  • "I added the ability to convert images into files containing the right bits for the printer, and wrote an Arduino library to print bitmaps using those files (and subsequently from any Stream object)."
This library was featured on a Hack-a-day post http://hackaday.com/2012/04/15/printing ... t-printer/

Good luck.
User avatar
dennisma
 
Posts: 31
Joined: Fri Feb 01, 2013 5:39 pm

Re: Thermal printer: print bitmap from SD card

by davidbliss on Sat Feb 02, 2013 4:41 pm

Hi, Thanks for the reply. I have spent a bit of time with that article and his library.

I've also been able to modify the Adafruit library to accept a File rather than a Stream, but it seems like the ideal way to get this working would be to be able to pass a File as a Stream and use the library as it was originally intended. I'm just not understanding how to do this in C++.

Ultimately, I want to be able to print a combination of text and images, and would like to use the unaltered Adafruit library to do so.
davidbliss
 
Posts: 14
Joined: Mon Jun 25, 2012 3:10 pm

Re: Thermal printer: print bitmap from SD card

by davidbliss on Sat Feb 02, 2013 5:10 pm

Hi I was able to figure this out. I was using the wrong casting syntax previously. This works:

Code: Select all | TOGGLE FULL SIZE
/*set up printer as you normally would*/

// open the file.
myFile = SD.open("test.bmp", FILE_READ);

// cast the file to a stream (note, your width and height may be different)
printer.printBitmap(384, 205, dynamic_cast<Stream*>(&myFile));
davidbliss
 
Posts: 14
Joined: Mon Jun 25, 2012 3:10 pm

Re: Thermal printer: print bitmap from SD card

by TheOilMan on Sun Mar 24, 2013 4:14 pm

I am trying to print a bitmap from the SD card as per your suggestion, but I just get random black lines.... I have tried a variety of bitmap types, 16bit, 1bit etc., but all with the same result. Can you elaborate on what you did. Did you just use the standard Adafruit library or have you tweaked it? What format is the image file on the SD card?

Thanks
TheOilMan
 
Posts: 1
Joined: Sun Mar 24, 2013 4:09 pm

Re: Thermal printer: print bitmap from SD card

by dherb on Mon Apr 22, 2013 1:53 pm

Hi guys - just following up here... I know the library supports printing directly from a stream source but haven't got it to work either (same random lines and stuff). Could someone upload some working sample code perhaps?
dherb
 
Posts: 1
Joined: Mon Apr 22, 2013 1:47 pm

Re: Thermal printer: print bitmap from SD card

by peek on Thu May 02, 2013 9:01 am

I would also like to know, how to get this to work.

adafruit states that to stream images to printer:
The new method expects the width and height to be
encoded in the first four bytes of the stream.

https://github.com/lazyatom/Thermal-Pri ... 6810a4c330

but they don't say how...
peek
 
Posts: 21
Joined: Sat Jan 14, 2012 6:17 am

Re: Thermal printer: print bitmap from SD card

by adafruit_support_mike on Thu May 02, 2013 11:16 pm

The Adafruit library is here: https://github.com/adafruit/Adafruit-Th ... er-Library , and the exact method that prints a bitmap from a stream looks like this:

Code: Select all | TOGGLE FULL SIZE
void Adafruit_Thermal::printBitmap(Stream *stream) {
  uint8_t  tmp;
  uint16_t width, height;

  tmp    =  stream->read();
  width  = (stream->read() << 8) + tmp;

  tmp    =  stream->read();
  height = (stream->read() << 8) + tmp;

  printBitmap(width, height, stream);
}


It expects bits in big-endian order, but bytes in little-endian order. If your image size was 640x480, the hexidecimal representations of those values are 640 == 0x280 and 480 == 0x1e0. The code would expect to see the byte sequence (0x80, 0x02, 0xe0, 0x01).
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10076
Joined: Thu Feb 11, 2010 2:51 pm

Re: Thermal printer: print bitmap from SD card

by peek on Mon May 13, 2013 3:33 am

Oh, wrong library...

Thanks for the answer, but:

How do you make a Stream?

Can I make a stream out of data from Serial.read()??

Sorry, not so good with c++ to get this, banging my head over this since the last 10h or so...
peek
 
Posts: 21
Joined: Sat Jan 14, 2012 6:17 am

Re: Thermal printer: print bitmap from SD card

by adafruit_support_mike on Mon May 13, 2013 3:03 pm

The official page for the Arduino Stream class is here: http://arduino.cc/de/Reference/Stream

The Serial class does use the Stream class to handle input and output.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10076
Joined: Thu Feb 11, 2010 2:51 pm

Re: Thermal printer: print bitmap from SD card

by scrubbl3r on Thu Jul 18, 2013 10:54 pm

I've dredged up every single piece of information on the topic [printing from a stream with the Adafruit Thermal printer]. No one's figured it out? I would love to know how to do this.

Best,
G
scrubbl3r
 
Posts: 13
Joined: Sat Jul 13, 2013 6:26 pm

Re: Thermal printer: print bitmap from SD card

by unexpectedly on Sun Aug 11, 2013 4:13 am

BUMP: I'm currently trying to do exactly this with a thermal printer and the Ada 1.8" TFT w/ joystick and sd card...

With the dynamic cast to Stream *, it sort of prints, but is inverse and upside down. So yeah, looks like the printBitmap( Stream * ) function is not expecting the same input as the processing image convert sketch is... I've started a write up here about my project.

Since no Arduino can store the 18k byte array for a 384x384 mono bitmap in SRAM, I'm trying to use the SD card for that. I've gotten every other example working (as well as combining them all) so that I can print on the printer that I've opened a file from SD Card and it even tried to print my business's logo, only incorrectly.

My chances appear to be:
[*]use processing bitmapImageConvert.pde to save the file on the SD Card and somehow treat it like a byte array
[*]convert the printBitmap(Stream *) function to either discover the Endian-ness of the bitmap or just alter it accordingly
[*]translate bitmapImageConvert.pde to work in the sketch

I'm using a Mega, so I'm only 37k into its 256k program memory...
unexpectedly
 
Posts: 42
Joined: Sun Nov 25, 2012 4:08 am

Re: Thermal printer: print bitmap from SD card

by adafruit_support_rick on Sun Aug 11, 2013 12:29 pm

The trouble with BMPs is that they start in the lower-left. That's why it draws upside down.
You should have a look at the tftbmp.pde example sketch in the Adafruit_TFTLCD library to see how it solves this problem. Briefly, it computes the file position of the start of each row in the bitmap, and then does a file seek to that position. In effect, it starts reading near the end of the file and works backwards.

adafruit_support_rick
 
Posts: 12785
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Thermal printer: print bitmap from SD card

by peek on Sun Sep 08, 2013 5:19 am

My chances appear to be:
[*]use processing bitmapImageConvert.pde to save the file on the SD Card and somehow treat it like a byte array
[*]convert the printBitmap(Stream *) function to either discover the Endian-ness of the bitmap or just alter it accordingly
[*]translate bitmapImageConvert.pde to work in the sketch


did you manage to translate bitmapImageConvert.pde so the outputted .h file can be used for printing an image from sd?

i'm somehow failing to do that...

best,
p
peek
 
Posts: 21
Joined: Sat Jan 14, 2012 6:17 am

Re: Thermal printer: print bitmap from SD card

by peek on Sun Sep 08, 2013 11:16 am

i don't get this.

why does an image c header file made using the bitmapImageConvert.pde sketch print from progmem correctly and totally wrong(i am not talking about upside down..) when opening the file from sd.

i see that when using progmem a different constructed function of printBitmap is used. (uint_8 vs. Stream)
but the way the bytes are read looks the same to me.

are bytes saved on sd in a different order?

so clueless here.
peek
 
Posts: 21
Joined: Sat Jan 14, 2012 6:17 am