Go Down

Topic: Transferring SdFile object to another function (Read 953 times) previous topic - next topic

Leopard77

Hi everybody, I'm quite new on Arduino programming and I'm stuck with something that makes me crazy. I'm sure one of you would be able to help me. Here is my problem and the associated code :

In the first loop, I'm able to print the 10 first octets of the file. In the second loop, I print always the 11th octet of the file. I think this is because I send a copy of the bmpFile object to the read8 function and that the current position is therefore not changed. I imagine it would be better to use a pointer (or something like this) that could save memory and allow the current position to change following read calls. But I'm unable to write the correct code.

If somebody could help me, I'll apreciate.
Regards.

Code: [Select]
#include <SdFat.h>

#define SD_CS 4        //SD Chipselect
SdFat sd;

void setup(void) {
 
  Serial.begin(9600);
  sd.begin(SD_CS, SPI_FULL_SPEED); // Initialize SD Card
  Test("Zodiac00.bmp");
 
}

void loop() {}

void Test(char *filename) {
 
  SdFile bmpFile;
  int i;

  bmpFile.open(filename, O_READ); // Open requested file on SD card

  for (i=0; i<10; i++) {
    Serial.println(bmpFile.read());
  }
 
  for (i=0; i<10; i++) {
    Serial.println(read8(bmpFile));
  }
 
  bmpFile.close(); // Close File object
 
}

uint8_t read8(SdFile f) {
 
  return f.read();

}

johnwasser

Code: [Select]

uint8_t read8(SdFile *fp) {
 
  return fp->read();

}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Leopard77

It works great. Thanks for your help.
Best regards.

Go Up