stevemarple / IniFile

Arduino library to parse ini files.
GNU Lesser General Public License v2.1
87 stars 45 forks source link

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

Closed gregorysemah closed 2 years ago

gregorysemah commented 2 years ago

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


#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 commented 2 years ago

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 commented 2 years ago

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.