Thermal printer: print bitmap from SD card
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Thermal printer: print bitmap from SD card
You're reading a .bmp file from SD? Bmps are in a different format. bitmapImageConvert turns them into a true bitmap, where each dot is represented by a single bit, instead of being represented by a byte.
-
- Posts: 23
- Joined: Sat Jan 14, 2012 6:17 am
Re: Thermal printer: print bitmap from SD card
i'm reading a .h file generated by bitmapImageConvert from SD.
generated files work when loaded from progmem but not when loaded from SD.
generated files work when loaded from progmem but not when loaded from SD.
-
- Posts: 23
- Joined: Sat Jan 14, 2012 6:17 am
Re: Thermal printer: print bitmap from SD card
this is the code i'm using:
Code: Select all
sdFile = SD.open("test01.h", FILE_READ);
if (sdFile) {
Serial.println("file opened.");
// cast the file to a stream (note, your width and height may be different)
printer.printBitmap(64, 64, dynamic_cast<Stream*>(&sdFile));
}
else Serial.println("error opening file.");
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Thermal printer: print bitmap from SD card
The .h file from bitmapImageConvert is an ascii text file, not a bitmap file. Bitmap files are binary, not ascii.
For example, you will have things like this in your .h file:
that will be read by printer.printBitmap as '0' 'x' '3' 'D' ',' '0' 'x' '8' 'F', which is 9 bytes of character data.
What you really want it to read is 2 bytes of binary data: 00111101 10001111.
When you include the .h file in your sketch, the compiler interprets and stores the 9 bytes of character data as the two bytes of binary data.
For example, you will have things like this in your .h file:
Code: Select all
0x3D,0x8F,
What you really want it to read is 2 bytes of binary data: 00111101 10001111.
When you include the .h file in your sketch, the compiler interprets and stores the 9 bytes of character data as the two bytes of binary data.
-
- Posts: 23
- Joined: Sat Jan 14, 2012 6:17 am
Re: Thermal printer: print bitmap from SD card
ah, i see, all makes more sense now!
thanks!
so i'm trying to get bitmapImageConvert to output binaries straight away.
changed
to
and deleted the line breaks and commas and other outputs to the file.
so i have a simple sequence of 0 and 1.
would that be about right?
anyways, no success so far.
next thing i tried was to stay with the hex format but change
PRINTER_PRINT((uint8_t)c); to PRINTER_PRINT((byte)c);
printBitmap function in Adafruit_Thermal.cpp
no success either.
would be super thankfull for any more input on this!
best,
p
thanks!
so i'm trying to get bitmapImageConvert to output binaries straight away.
changed
Code: Select all
output.format("0x%02X", sum);
Code: Select all
output.print(binary(sum, 8));
so i have a simple sequence of 0 and 1.
would that be about right?
anyways, no success so far.
next thing i tried was to stay with the hex format but change
PRINTER_PRINT((uint8_t)c); to PRINTER_PRINT((byte)c);
printBitmap function in Adafruit_Thermal.cpp
no success either.
would be super thankfull for any more input on this!
best,
p
- technerdchris
- Posts: 46
- Joined: Sun Nov 25, 2012 4:08 am
Re: Thermal printer: print bitmap from SD card
I was able to print from bitmap binaries stored on SD card... BUT... I had to use a custom php script to read the .h files made by the processing script (that I also changed) The bitmapImageConvert.pde didn't run for me as delivered, so I hard coded the filename at the top. I also changed the text output to add comments to the end of each row. These comments are used by the PHP script to write the correct sized file to disk.
Double check your output against the box192.bin sample that I included via a hex editor to make sure your endian-ness is correct. My php script works on ubuntu 10.04 LTS x86-64 server running an AMD 1055t processor. It took me a lot of googling and trial & error to get the output the way that I wanted. PHP folks aren't normally accustomed to dealing with bit shifting and endian-ness.
Here's the code I put in setup() to try out various things:
Pay REAL CLOSE attention to SRAM and bitmap sizes. There's a point where printing the bin files from the SD card causes 15+ second delay. And if at all possible, feed the printer 9V with a 1A supply. The improvement was so remarkable that I would go as far to advise against using 5V.
Chris
Double check your output against the box192.bin sample that I included via a hex editor to make sure your endian-ness is correct. My php script works on ubuntu 10.04 LTS x86-64 server running an AMD 1055t processor. It took me a lot of googling and trial & error to get the output the way that I wanted. PHP folks aren't normally accustomed to dealing with bit shifting and endian-ness.
Here's the code I put in setup() to try out various things:
Code: Select all
// Thermal Printer setup
// from adafruit
printer.begin();
// SD Card
if (!SD.begin(SD_CS)) {
printer.println(F("SD Card initialization failed!"));
} else {
sdcard_spcr=SPCR;
File myFile;
myFile = SD.open("box192.bin");
if( myFile ){
printer.printBitmap(220, 68, dynamic_cast<Stream*>(&myFile) );
} else {
printer.println(F("Opening file cw100x68.bin failed!"));
}
myFile.close();
}
Chris
- Attachments
-
- thermal_bmp.zip
- Files I used to get bmp binaries to SD Card and print
- (4.16 KiB) Downloaded 144 times
- technerdchris
- Posts: 46
- Joined: Sun Nov 25, 2012 4:08 am
Re: Thermal printer: print bitmap from SD card
Looking at your last question, regarding having the processing.org script output binary data, I'm not sure how easy that will be. And the results should not be 0101001010 etc. If you open it and "see" only 0s and 1s, something went wrong with your output.
When I was working on this, I know there are a number of FF bytes in a row at the top. So, if you see "00ff" something went wrong, probably you're writing 16 bit ints at a time and feeding it 8 bit 0xffs with each write, thus you need to shift over the first byte by 8 bits and bitwise OR it with the next 8 bit variable: " 8bitvariable << 8 | next8bitvariable ". Before you consider it "done" be sure to try out an example to verify little or big endian byte order. If the endian is backwards, the very first instance of writing 8 bit variable to 16 bit disc write would have been "ff00".
Take what Rick said...
Example of how a text editor sees this file:
When I was working on this, I know there are a number of FF bytes in a row at the top. So, if you see "00ff" something went wrong, probably you're writing 16 bit ints at a time and feeding it 8 bit 0xffs with each write, thus you need to shift over the first byte by 8 bits and bitwise OR it with the next 8 bit variable: " 8bitvariable << 8 | next8bitvariable ". Before you consider it "done" be sure to try out an example to verify little or big endian byte order. If the endian is backwards, the very first instance of writing 8 bit variable to 16 bit disc write would have been "ff00".
Take what Rick said...
You do not literally want the file to read "00111101 10001111", you want the contents of the file to "be" 3D8F ... but only as when viewed in a hex editor. Notice below the C0 and 03 entries? Those are the two pixel borders of the box192.bmp file. So if you run all this and check your new box192.bin, if 0C is first and not the C0, your endian is backwards, so you would do this "next8bitvariable << 8 | 8bitvariable ". I never really could figure out wth the bitmapImageConvert.pde file's code, so I wrote the PHP script.that will be read by printer.printBitmap as '0' 'x' '3' 'D' ',' '0' 'x' '8' 'F', which is 9 bytes of character data.
What you really want it to read is 2 bytes of binary data: 00111101 10001111.
Example of how a text editor sees this file:
-
- Posts: 23
- Joined: Sat Jan 14, 2012 6:17 am
Re: Thermal printer: print bitmap from SD card
thank you very much for your reply and the files!
will try to make this work in processing!
just read that the function saveBytes() saves in binary format, so i might have better luck with that compared to the printWriter functions used in bitmapImageConvert...
will try to make this work in processing!
just read that the function saveBytes() saves in binary format, so i might have better luck with that compared to the printWriter functions used in bitmapImageConvert...
- technerdchris
- Posts: 46
- Joined: Sun Nov 25, 2012 4:08 am
Re: Thermal printer: print bitmap from SD card
Processing.org's program irked me because (for me), it doesn't update the same as Arduino IDE and chipKIT's mpide and I'd have to relaunch processing with every change to the file. Even with a "shortcut" copied to the folder, it torqued me enough to just write a PHP CLI program.
-
- Posts: 23
- Joined: Sat Jan 14, 2012 6:17 am
Re: Thermal printer: print bitmap from SD card
managed to get it working in processing!
thanks to your valuable input and example files, many many thanks!
now i will have to find a way how to transfer the file to the sd card over serial!
maybe printing directly from serial would work too?!
thanks to your valuable input and example files, many many thanks!
now i will have to find a way how to transfer the file to the sd card over serial!
maybe printing directly from serial would work too?!
- Attachments
-
- convertImageToBinary.pde.zip
- processing 1.5 (!) sketch to output binary file to print image on thermal printer from sd
- (1.2 KiB) Downloaded 136 times
-
- Posts: 3
- Joined: Fri Sep 13, 2013 7:17 pm
Re: Thermal printer: print bitmap from SD card
Is there any way I can read directly from the SD card without having to use LCD Assistant?
-
- Posts: 23
- Joined: Sat Jan 14, 2012 6:17 am
Re: Thermal printer: print bitmap from SD card
it won't print jpgs, bmps, etc. directly from sd without converting the images first.
try the processing sketch i posted above to generate usable image data for printing from sd card.
then you won't need lcd assistant!
try the processing sketch i posted above to generate usable image data for printing from sd card.
then you won't need lcd assistant!
-
- Posts: 3
- Joined: Fri Sep 13, 2013 7:17 pm
Re: Thermal printer: print bitmap from SD card
Oh dear. It's giving me errors just with the unaltered code. It's saying that PImage and PrintWriter are not declared in the scope even after I included SoftwareSerial and Adafruit_Thermal. I found the PImage class, but I don't know how to properly include it in the sketch. :/
-
- Posts: 23
- Joined: Sat Jan 14, 2012 6:17 am
Re: Thermal printer: print bitmap from SD card
are you opening it in arduino ide?
it's a processing sketch and also only works in processing 1.5x!
it's a processing sketch and also only works in processing 1.5x!
-
- Posts: 4
- Joined: Thu Jan 16, 2014 12:00 pm
Re: Thermal printer: print bitmap from SD card
Hi,
I've read several times your posts there, but I can not figure out how I will success printing in stream mode with an arduino yun.
The fact is, to access the file on sd, I'm using the FileIO class, provided since the Arduino Yun, instead of the SD library . but I can't make it run or debug !
Could you please enlight me ?
1 - This is the code I've for now :
where "chemin" is something like "/mnt/sd/arduino/www/file.h" and file contains test bytes : "0x7F, 0xFF, 0xEF, 0xFD, 0xFB, 0x5E, 0xF7, 0xFE".
I don't have any error on compile but in execution, printer don't print and the Bridge connexion breaks : "Connection closed by foreign host" in the Console.
Also, You talk about a new structure of function, for which first bytes of the stream should be respolution values, but in the downloadable library, it's still the Adafruit_Thermal::printBitmap(int w, int h, Stream *stream) construction. I tried without success too.
Any help ?
I've read several times your posts there, but I can not figure out how I will success printing in stream mode with an arduino yun.
The fact is, to access the file on sd, I'm using the FileIO class, provided since the Arduino Yun, instead of the SD library . but I can't make it run or debug !
Could you please enlight me ?
1 - This is the code I've for now :
Code: Select all
File dataFile = FileSystem.open(chemin, FILE_READ);
while ( dataFile.available() > 0 ){
printer.printBitmap(384, 240, dynamic_cast<Stream*>(&dataFile));
}
I don't have any error on compile but in execution, printer don't print and the Bridge connexion breaks : "Connection closed by foreign host" in the Console.
Also, You talk about a new structure of function, for which first bytes of the stream should be respolution values, but in the downloadable library, it's still the Adafruit_Thermal::printBitmap(int w, int h, Stream *stream) construction. I tried without success too.
Any help ?
Please be positive and constructive with your questions and comments.