Pages: [1]   Go Down
Author Topic: Transferring SdFile object to another function  (Read 801 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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();

}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8096
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

}
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: