Thermal printer: print bitmap from SD card

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
davidbliss
 
Posts: 24
Joined: Mon Jun 25, 2012 3:10 pm

Thermal printer: print bitmap from SD card

Post by davidbliss »

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.

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

Re: Thermal printer: print bitmap from SD card

Post by dennisma »

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
davidbliss
 
Posts: 24
Joined: Mon Jun 25, 2012 3:10 pm

Re: Thermal printer: print bitmap from SD card

Post by davidbliss »

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.

User avatar
davidbliss
 
Posts: 24
Joined: Mon Jun 25, 2012 3:10 pm

Re: Thermal printer: print bitmap from SD card

Post by davidbliss »

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

Code: Select all

/*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));

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

Re: Thermal printer: print bitmap from SD card

Post by TheOilMan »

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

dherb
 
Posts: 1
Joined: Mon Apr 22, 2013 1:47 pm

Re: Thermal printer: print bitmap from SD card

Post by dherb »

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?

peek
 
Posts: 23
Joined: Sat Jan 14, 2012 6:17 am

Re: Thermal printer: print bitmap from SD card

Post by peek »

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...

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Thermal printer: print bitmap from SD card

Post by adafruit_support_mike »

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

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).

peek
 
Posts: 23
Joined: Sat Jan 14, 2012 6:17 am

Re: Thermal printer: print bitmap from SD card

Post by peek »

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...

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Thermal printer: print bitmap from SD card

Post by adafruit_support_mike »

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.

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

Re: Thermal printer: print bitmap from SD card

Post by scrubbl3r »

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

User avatar
technerdchris
 
Posts: 46
Joined: Sun Nov 25, 2012 4:08 am

Re: Thermal printer: print bitmap from SD card

Post by technerdchris »

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...

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Thermal printer: print bitmap from SD card

Post by adafruit_support_rick »

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.

peek
 
Posts: 23
Joined: Sat Jan 14, 2012 6:17 am

Re: Thermal printer: print bitmap from SD card

Post by peek »

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: 23
Joined: Sat Jan 14, 2012 6:17 am

Re: Thermal printer: print bitmap from SD card

Post by peek »

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.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”