Browse Source

Limit memory use on Teensy 2.0

main
PaulStoffregen 4 years ago
parent
commit
50f065598f
1 changed files with 23 additions and 7 deletions
  1. +23
    -7
      src/SD.h

+ 23
- 7
src/SD.h View File

#define BUILTIN_SDCARD 254 #define BUILTIN_SDCARD 254
#endif #endif


#if defined(__arm__)
// Support everything on 32 bit boards with enough memory
#define SDFAT_FILE FsFile
#define SDFAT_BASE SdFs
#define MAX_FILENAME_LEN 256
#elif defined(__AVR__)
// Limit to 32GB cards on 8 bit Teensy with only limited memory
#define SDFAT_FILE File32
#define SDFAT_BASE SdFat32
#define MAX_FILENAME_LEN 64
#endif

class SDFile : public File class SDFile : public File
{ {
public: public:
SDFile(const FsFile &file) : sdfatfile(file), filename(nullptr) { }
SDFile(const SDFAT_FILE &file) : sdfatfile(file), filename(nullptr) { }
virtual ~SDFile(void) { virtual ~SDFile(void) {
if (sdfatfile) sdfatfile.close(); if (sdfatfile) sdfatfile.close();
if (filename) free(filename); if (filename) free(filename);
} }
virtual const char * name() { virtual const char * name() {
if (!filename) { if (!filename) {
filename = (char *)malloc(256);
filename = (char *)malloc(MAX_FILENAME_LEN);
if (filename) { if (filename) {
sdfatfile.getName(filename, 256);
sdfatfile.getName(filename, MAX_FILENAME_LEN);
} else { } else {
static char zeroterm = 0; static char zeroterm = 0;
filename = &zeroterm; filename = &zeroterm;
return sdfatfile.isDirectory(); return sdfatfile.isDirectory();
} }
virtual File openNextFile(uint8_t mode=0) { virtual File openNextFile(uint8_t mode=0) {
FsFile file = sdfatfile.openNextFile();
SDFAT_FILE file = sdfatfile.openNextFile();
if (file) return File(new SDFile(file)); if (file) return File(new SDFile(file));
return File(); return File();
} }
} }
using Print::write; using Print::write;
private: private:
FsFile sdfatfile;
SDFAT_FILE sdfatfile;
char *filename; char *filename;
}; };


File open(const char *filepath, uint8_t mode = FILE_READ) { File open(const char *filepath, uint8_t mode = FILE_READ) {
oflag_t flags = O_READ; oflag_t flags = O_READ;
if (mode == FILE_WRITE) flags = O_READ | O_WRITE | O_CREAT; if (mode == FILE_WRITE) flags = O_READ | O_WRITE | O_CREAT;
FsFile file = sdfs.open(filepath, flags);
SDFAT_FILE file = sdfs.open(filepath, flags);
if (file) { if (file) {
// Arduino's default FILE_WRITE starts at end of file // Arduino's default FILE_WRITE starts at end of file
if (mode == FILE_WRITE) file.seekEnd(0); if (mode == FILE_WRITE) file.seekEnd(0);
return sdfs.rmdir(filepath); return sdfs.rmdir(filepath);
} }
public: // allow access, so users can mix SD & SdFat APIs public: // allow access, so users can mix SD & SdFat APIs
SdFs sdfs;
SDFAT_BASE sdfs;
}; };


extern SDClass SD; extern SDClass SD;


// do not expose these defines in Arduino sketches or other libraries
#undef SDFAT_FILE
#undef SDFAT_BASE
#undef MAX_FILENAME_LEN




#define SD_CARD_TYPE_SD1 0 #define SD_CARD_TYPE_SD1 0

Loading…
Cancel
Save