Understanding output to function

Wondering if i can have some help.

I’ve been trying the sketch indicated here but that library appears to be deprecated.

The library that I’m using takes one single argument file.print(x); (which is fine for text), but I am unsure what to use for these loops as they have two arguments.

 // 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-(w*3)%4)%4);                 // and padding as needed

I pondered using a for loop, but not quite sure how to grab/pass the values for the loop - Is this just checking the size of the array and transferring each byte at a time?

There's a HUGE difference between print and write.

The library that I'm using takes one single argument

print usually comes by inheriting Print. So I have a hard time imagining that you have a library with a different version. But alas, why don't you tell us what library you have that you think only has one overload for print.

If you don’t want to use the SdFat library, why not just switch to the SD library that come with Arduino?

It doesn’t work with the ESP8266, I’m afraid.

I tried to use the attached library, but throws out a few errors. I’m not sure this is the right one. Also tried the default SD one, but that lacks <SdFatUtil.h>

Arduino: 1.8.9 (Linux), Board: "Arduino/Genuino Uno"

/home/carlos/Desktop/sketch_aug13b/sketch_aug13b.ino: In function 'void setup()':
sketch_aug13b:20:32: error: no matching function for call to 'SdFat::init(SPISettings, int)'
  if (!sd.init(SPI_FULL_SPEED, 8)) {
                                ^
In file included from /home/carlos/Arduino/libraries/SdFat-master/src/FatLib/FatFile.h:38:0,
                 from /home/carlos/Arduino/libraries/SdFat-master/src/FatLib/ArduinoFiles.h:33,
                 from /home/carlos/Arduino/libraries/SdFat-master/src/FatLib/FatLib.h:27,
                 from /home/carlos/Arduino/libraries/SdFat-master/src/SdFat.h:33,
                 from /home/carlos/Desktop/sketch_aug13b/sketch_aug13b.ino:2:
/home/carlos/Arduino/libraries/SdFat-master/src/FatLib/FatVolume.h:230:8: note: candidate: bool FatVolume::init()
   bool init() {
        ^
/home/carlos/Arduino/libraries/SdFat-master/src/FatLib/FatVolume.h:230:8: note:   candidate expects 0 arguments, 2 provided
/home/carlos/Arduino/libraries/SdFat-master/src/FatLib/FatVolume.h:243:8: note: candidate: bool FatVolume::init(uint8_t)
   bool init(uint8_t part);
        ^
/home/carlos/Arduino/libraries/SdFat-master/src/FatLib/FatVolume.h:243:8: note:   candidate expects 1 argument, 2 provided
sketch_aug13b:58:21: error: no matching function for call to 'print(int [6])'
      Serial.print(px);
                     ^
In file included from /home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Stream.h:26:0,
                 from /home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/HardwareSerial.h:29,
                 from /home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Arduino.h:232,
                 from sketch/sketch_aug13b.ino.cpp:1:
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:68:12: note: candidate: size_t Print::print(char) <near match>
     size_t print(char);
            ^
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:68:12: note:   conversion of argument 1 would be ill-formed:
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:69:12: note: candidate: size_t Print::print(unsigned char, int) <near match>
     size_t print(unsigned char, int = DEC);
            ^
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:69:12: note:   conversion of argument 1 would be ill-formed:
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:70:12: note: candidate: size_t Print::print(int, int) <near match>
     size_t print(int, int = DEC);
            ^
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:70:12: note:   conversion of argument 1 would be ill-formed:
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:71:12: note: candidate: size_t Print::print(unsigned int, int) <near match>
     size_t print(unsigned int, int = DEC);
            ^
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:71:12: note:   conversion of argument 1 would be ill-formed:
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:72:12: note: candidate: size_t Print::print(long int, int) <near match>
     size_t print(long, int = DEC);
            ^
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:72:12: note:   conversion of argument 1 would be ill-formed:
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:73:12: note: candidate: size_t Print::print(long unsigned int, int) <near match>
     size_t print(unsigned long, int = DEC);
            ^
/home/carlos/Desktop/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Print.h:73:12: note:   conversion of argument 1 would be ill-formed:
exit status 1
no matching function for call to 'SdFat::init(SPISettings, int)'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

SD-master.zip (62.4 KB)

It’s much preferable to post a github link than the .zip file.

Nobody who knows anything about basic security on the internet will unzip your zip file. That’s a good way to get infected.

casemod:
It doesn’t work with the ESP8266, I’m afraid.

The SD library that comes with the ESP8266 core doesn’t work with the ESP826?!?
This compiles without error for NodeMCU 1.0:

#include <SPI.h>
#include <SD.h>


const int chipSelect = 4;


/*
  WRITE BMP TO SD CARD
  Jeff Thompson
  Summer 2012


  TO USE MEGA:
  The SdFat library must be edited slightly to use a Mega - in line 87
  of SdFatConfig.h, change to:


   #define MEGA_SOFT_SPI 1


  (this uses pins 10-13 for writing to the card)


  Writes pixel data to an SD card, saved as a BMP file.  Lots of code
  via the following...


  BMP header and pixel format:
   http://stackoverflow.com/a/2654860


  SD save:
   http://arduino.cc/forum/index.php?topic=112733 (lots of thanks!)
  ... and the SdFat example files too


  www.jeffreythompson.org
*/


char name[] = "9px_0000.bmp";       // filename convention (will auto-increment)
const int w = 16;                   // image width in pixels
const int h = 9;                    // " height
const boolean debugPrint = true;    // print details of process over serial?


const int imgSize = w * h;
int px[w * h];                      // actual pixel data (grayscale - added programatically below)


File file;


void setup()
{


  // iteratively create pixel data
  int increment = 256 / (w * h);    // divide color range (0-255) by total # of px
  for (int i = 0; i < imgSize; i++)
  {
    px[i] = i * increment;          // creates a gradient across pixels for testing
  }


  // SD setup
  Serial.begin(9600);
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1)
      delay(1);
  }
  
  Serial.println("card initialized.");
  
  // if name exists, create new filename
  for (int i = 0; i < 10000; i++)
  {
    name[4] = (i / 1000) % 10 + '0'; // thousands place
    name[5] = (i / 100) % 10 + '0'; // hundreds
    name[6] = (i / 10) % 10 + '0';  // tens
    name[7] = i % 10 + '0';         // ones
    
    file = SD.open(name, FILE_WRITE);
    if (file)
    {
      break;
    }
  }


  // set fileSize (used in bmp header)
  int rowSize = 4 * ((3 * w + 3) / 4);  // how many bytes in the row (used to create padding)
  int fileSize = 54 + h * rowSize;      // headers (54 bytes) + pixel data


  // create image data; heavily modified version via:
  // http://stackoverflow.com/a/2654860
  unsigned char *img = NULL;            // image data
  if (img)                              // if there's already data in the array, clear it
  {
    free(img);
  }
  img = (unsigned char *)malloc(3 * imgSize);


  for (int y = 0; y < h; y++)
  {
    for (int x = 0; x < w; x++)
    {
      int colorVal = px[y * w + x];                      // classic formula for px listed in line
      img[(y * w + x) * 3 + 0] = (unsigned char)(colorVal); // R
      img[(y * w + x) * 3 + 1] = (unsigned char)(colorVal); // G
      img[(y * w + x) * 3 + 2] = (unsigned char)(colorVal); // B
      // padding (the 4th byte) will be added later as needed...
    }
  }


  // print px and img data for debugging
  if (debugPrint)
  {
    Serial.print("\nWriting \"");
    Serial.print(name);
    Serial.print("\" to file...\n");
    for (int i = 0; i < imgSize; i++)
    {
      Serial.print(px[i]);
      Serial.print("  ");
    }
  }


  // create padding (based on the number of pixels in a row
  unsigned char bmpPad[rowSize - 3 * w];
  for (size_t i = 0; i < sizeof bmpPad; i++)       // fill with 0s
  {
    bmpPad[i] = 0;
  }


  // create file headers (also taken from StackOverflow example)
  unsigned char bmpFileHeader[14] =              // file header (always starts with BM!)
  {
    'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0
  };
  unsigned char bmpInfoHeader[40] =              // info about the file (size, etc)
  {
    40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 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 (thanks forum!)
  file.write(bmpFileHeader, sizeof(bmpFileHeader));    // write file header
  file.write(bmpInfoHeader, sizeof(bmpInfoHeader));    // " info header


  for (int i = 0; i < h; i++)                          // iterate image array
  {
    file.write(img + (w * (h - i - 1) * 3), 3 * w);    // write px data
    file.write(bmpPad, (4 - (w * 3) % 4) % 4);         // and padding as needed
  }
  file.close();                                        // close file when done writing


  if (debugPrint)
  {
    Serial.print("\n\n---\n");
  }
}


void loop() { }

johnwasser:
The SD library that comes with the ESP8266 core doesn't work with the ESP826?!?
This compiles without error for NodeMCU 1.0:

Doesn't work on mine. Can you share your OS and Node MCU version?

I'm compiling on 1.8.9 Linux Version and this variant of the ESP8266 libraries. GitHub here.

MacOS 10.14.5
Boards Manager says that I have V2.5.2 of "esp8266 by ESP8266 Community"

This is now sorted. There are a number of issues with the .bmp code, it seems it's limited to 256 pixels.

In lieu of such I decided to start from scratch. So far, so good - I'm just optimising the output to get the full 320*240 resolution.

I also realised there is no reason to stick to 8 bit writes - moving many of those to 16bit.

As to the SD, I used the library that comes with V2.5.1.