Hi all, I'm working with the Sparkfun MicroSD shield and the SdFat library to write files to the card. There are tons of examples on how to write ascii data to text files, but I'm having a hard time figuring out how to write a file from an array of bytes. My code is below - mostly pulled from the SdFat examples and a Stack Overflow example on creating bmp files.
The SD stuff works (writes to the card, creates files, etc) and my computer even sees them as bmp files (not sure if the header is correct, or that the extension is all it needs), but I'm not getting a readable image.
Any help would be fantastic!
//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>char name[] = "9px_00.bmp";
int w = 3;
int h = 2;
int px[] = {
255, 0, 255, 0, 255, 0 };
boolean debugPrint = true;SdFat sd;
SdFile file;void setup() {
// SD setup
Serial.begin(9600);
if (!sd.init(SPI_FULL_SPEED, 8)) {
sd.initErrorHalt();
}// if name exists, create new filename
for (int i=0; i<1000; i++) {
name[4] = i/10 + '0';
name[5] = i%10 + '0';
if (file.open(name, O_CREAT | O_EXCL | O_WRITE)) {
break;
}
}// create image data
// heavily modified version via: Writing BMP image in pure c/c++ without other libraries - Stack Overflow
unsigned char img = NULL; // image data
int filesize = 54 + 3 * w * h; // w is image width, h is image height
if (img) {
free(img);
}
img = (unsigned char )malloc(3wh);
//memset(img,0,sizeof(img)); // not sure if I really need this; runs fine without...for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
int colorVal = px[yw + x];
img[(yw + x)3+2] = (unsigned char)(colorVal);
img[(yw + x)3+1] = (unsigned char)(colorVal);
img[(yw + x)*3+0] = (unsigned char)(colorVal);
}
}// print px and img data for debugging
if (debugPrint) {
Serial.print("Writing "");
Serial.print(name);
Serial.print("" to file...\n");
for (int i=0; i<wh; i++) {
Serial.print(px);*
_ Serial.print(" ");_
}*
}*
// create file headers (also taken from above example)*
unsigned char bmpFileHeader[14] = {*
'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 };*
unsigned char bmpInfoHeader[40] = {*
40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0 };*
unsigned char bmpPad[3] = {*
0,0,0 };*
bmpFileHeader[ 2] = (unsigned char)(filesize );*
bmpFileHeader[ 3] = (unsigned char)(filesize>> 8);*
bmpFileHeader[ 4] = (unsigned char)(filesize>>16);*
bmpFileHeader[ 5] = (unsigned char)(filesize>>24);*
bmpInfoHeader[ 4] = (unsigned char)( w );*
bmpInfoHeader[ 5] = (unsigned char)( w>> 8);*
bmpInfoHeader[ 6] = (unsigned char)( w>>16);*
bmpInfoHeader[ 7] = (unsigned char)( w>>24);*
bmpInfoHeader[ 8] = (unsigned char)( h );*
bmpInfoHeader[ 9] = (unsigned char)( h>> 8);*
bmpInfoHeader[10] = (unsigned char)( h>>16);*
bmpInfoHeader[11] = (unsigned char)( h>>24);*
// write the file!*
// this is a combination of the bmp example above and*
// one from the SdFat library (it doesn't create a usable*
// bmp file, though)...*
file.write(bmpFileHeader, sizeof(bmpFileHeader)); // write file header*
file.write(bmpInfoHeader, sizeof(bmpInfoHeader)); // " info header*
for (int i=0; i<sizeof(img); i++) { // iterate image array*
_ file.write(img+(w*(h-i-1)3), w); // write px data_
_ file.write(bmpPad, (4-(w3)%4)%4); // and padding as needed_}*
if (debugPrint) {*
_ Serial.println("\n---");_}*
}
void loop() { }
[/quote]