Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you provide example to use IniFile with SdFat? #30

Closed
gregorysemah opened this issue Nov 15, 2021 · 2 comments
Closed

Can you provide example to use IniFile with SdFat? #30

gregorysemah opened this issue Nov 15, 2021 · 2 comments

Comments

@gregorysemah
Copy link

It seems that it is possible to use SdFat (with teensy 4.1)
But defining PREFER_SDFAT_LIBRARY seems not to work because of type of File
Can you provide a simple example please?

thanks

Here the code I use, no instantiation for the moment, just #include <IniFile.h>

#include <SPI.h>
#define PREFER_SDFAT_LIBRARY
#include <IPAddress.h>
#include <IniFile.h>

#define SD_FAT_TYPE 3

// SDCARD_SS_PIN is defined for the built-in SD on some boards.
#ifndef SDCARD_SS_PIN
const uint8_t SD_CS_PIN = SS;
#else  // SDCARD_SS_PIN
// Assume built-in SD is used.
const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
#endif  // SDCARD_SS_PIN

// Try to select the best SD card configuration.
#if HAS_SDIO_CLASS
#define SD_CONFIG SdioConfig(FIFO_SDIO)
#elif ENABLE_DEDICATED_SPI
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
#else  // HAS_SDIO_CLASS
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
#endif  // HAS_SDIO_CLASS

#if SD_FAT_TYPE == 0
SdFat sd;
File file;
File configFile;
#elif SD_FAT_TYPE == 1
SdFat32 sd;
File32 file;
File32 configFile;
#elif SD_FAT_TYPE == 2
SdExFat sd;
ExFile file;
ExFile configFile;
#elif SD_FAT_TYPE == 3
SdFs sd;
FsFile file;
FsFile configFile;
#else  // SD_FAT_TYPE
#error Invalid SD_FAT_TYPE
#endif  // SD_FAT_TYPE

void setup() {
  Serial.begin(112500);
  while (!Serial) {}

  Serial.println(F("\nType any character to begin."));
  while (!Serial.available()) {
    yield();
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(SD_CONFIG)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file.
  file = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (file) {
    Serial.print("Writing to test.txt...");
    file.println("testing 1, 2, 3.");
    // close the file:
    file.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  file = SD.open("test.txt");
  if (file) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (file.available()) {
      Serial.write(file.read());
    }
    // close the file:
    file.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}
void loop() {
  // nothing happens after setup
}
@stevemarple
Copy link
Owner

I don't use the Teensy boards so cannot make specific recommendations, nor test my suggested solution or create examples.

If you are defining PREFER_SDFAT_LIBRARY then you must make sure that the macro is set for all compilation units, including IniFile.cpp. I don't know if the Teensy has a way to set a macro in the build system that is external to the source files, if so you should use it. Unfortunately the standard @arduino IDE does not, which has been a source of frustration to me*, exactly because of issues like this. Your simplest fix is probably to edit your local copy of IniFile.h and add the line
#define PREFER_SDFAT_LIBRARY
I realise that editing the code of an external library is not an acceptable solution but this is a common problem, one that Arduino need to resolve.

* I submitted a pull request to solve this problem 6 years ago, which was not accepted for philosophical reasons ("such external defines are considered harmful"), even though all other IDEs and build systems that I am familiar with support this feature. Many other people have requested similar features and others have submitted pull requests too. The Teensy core could make the FAT library an option in the board menu, the STM32 core does something similar to select which type of USB support is included. My energy to fight this battle has gone so to get that feature into my Calunium core in I added a build menu option which allows a custom sketch include file to be used.

@stevemarple
Copy link
Owner

I'm closing this because the changes to support your request belong outside of this library. Hopefully you have a better solution available than editing library headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants