sdfat - fgets

Hello there :slight_smile: ,

Looking at the sample for sdfat fgets, is there a way of altering it (with a button attached to pin 8 when it goes high, pressed) to show the next line, as the example loads all the lines in 1 go.

// Demo of fgets function to read lines from a file.
#include <SPI.h>
#include <SdFat.h>

// SD chip select pin
const uint8_t chipSelect = SS;

SdFat sd;
// print stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// store error strings in flash memory
#define error(s) sd.errorHalt(F(s))
//------------------------------------------------------------------------------
void demoFgets() {
  char line[25];
  int n;
  // open test file
  SdFile rdfile("fgets.txt", O_READ);

  // check for open error
  if (!rdfile.isOpen()) {
    error("demoFgets");
  }

  cout << endl << F(
         "Lines with '>' end with a '\\n' character\n"
         "Lines with '#' do not end with a '\\n' character\n"
         "\n");

  // read lines from the file
  while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
    if (line[n - 1] == '\n') {
      cout << '>' << line;
    } else {
      cout << '#' << line << endl;
    }
  }
}
//------------------------------------------------------------------------------
void makeTestFile() {
  // create or open test file
  SdFile wrfile("fgets.txt", O_WRITE | O_CREAT | O_TRUNC);

  // check for open error
  if (!wrfile.isOpen()) {
    error("MakeTestFile");
  }

  // write test file
  wrfile.print(F(
                 "Line with CRLF\r\n"
                 "Line with only LF\n"
                 "Long line that will require an extra read\n"
                 "\n"  // empty line
                 "Line at EOF without NL"
               ));
  wrfile.close();
}
//------------------------------------------------------------------------------
void setup(void) {
  Serial.begin(9600);
  while (!Serial) {}  // Wait for Leonardo

  cout << F("Type any character to start\n");
  while (Serial.read() <= 0) {}
  delay(400);  // catch Due reset problem

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
    sd.initErrorHalt();
  }

  makeTestFile();

  demoFgets();

  cout << F("\nDone\n");
}
void loop(void) {}

Many Thanks,
Pete

Will offer a bit of $ via PayPal if anyone can help?

Here is a version of the fgets example that waits for pin 8 to go high and then low after each line. It has a debounce that can be adjusted by the value of DEBOUNCE_COUNT.

Hope you are capable of modifying the example for your needs.

// Demo of fgets function to read lines from a file.
#include <SPI.h>
#include <SdFat.h>

// SD chip select pin
const uint8_t chipSelect = SS;

SdFat sd;
// print stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// store error strings in flash memory
#define error(s) sd.errorHalt(F(s))
//------------------------------------------------------------------------------
// wait for switch to be pushed and released.
void waitSwitch() {
  const uint8_t SWITCH_PIN = 8;
  const uint8_t DEBOUNCE_COUNT = 10;
  uint8_t count = DEBOUNCE_COUNT;
  
  // Wait for pin to go high.
  while (!digitalRead(SWITCH_PIN)) {}
  
  // Wait for pin to go low with debounce.
  while (count) {
    delay(10);
    if (digitalRead(SWITCH_PIN)) {
      count = DEBOUNCE_COUNT;
    } else {
      count--;
    }
  }
}
//------------------------------------------------------------------------------
void demoFgets() {
  char line[25];
  int n;
  // open test file
  SdFile rdfile("fgets.txt", O_READ);

  // check for open error
  if (!rdfile.isOpen()) {
    error("demoFgets");
  }

  cout << endl << F(
         "Lines with '>' end with a '\\n' character\n"
         "Lines with '#' do not end with a '\\n' character\n"
         "\n");

  // read lines from the file
  while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
    if (line[n - 1] == '\n') {
      cout << '>' << line;
    } else {
      cout << '#' << line << endl;
    }
    waitSwitch();
  }
}
//------------------------------------------------------------------------------
void makeTestFile() {
  // create or open test file
  SdFile wrfile("fgets.txt", O_WRITE | O_CREAT | O_TRUNC);

  // check for open error
  if (!wrfile.isOpen()) {
    error("MakeTestFile");
  }

  // write test file
  wrfile.print(F(
                 "Line with CRLF\r\n"
                 "Line with only LF\n"
                 "Long line that will require an extra read\n"
                 "\n"  // empty line
                 "Line at EOF without NL"
               ));
  wrfile.close();
}
//------------------------------------------------------------------------------
void setup(void) {
  Serial.begin(9600);
  while (!Serial) {}  // Wait for Leonardo

  cout << F("Type any character to start\n");
  while (Serial.read() <= 0) {}
  delay(400);  // catch Due reset problem

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
    sd.initErrorHalt();
  }

  makeTestFile();

  demoFgets();

  cout << F("\nDone\n");
}
void loop(void) {}