From b4222b16dd9f2714557e964b79e16f216b195e82 Mon Sep 17 00:00:00 2001 From: Woutvstk Date: Mon, 16 Dec 2024 11:55:07 +0100 Subject: [PATCH 1/6] "using namespace" should not be in in header File this can give problems that can not be worked around by the code that is using the library removed from SD.h and added to file.cpp and examples --- examples/Datalogger/Datalogger.ino | 2 ++ examples/DumpFile/DumpFile.ino | 2 ++ examples/Files/Files.ino | 2 ++ examples/NonBlockingWrite/NonBlockingWrite.ino | 2 ++ examples/ReadWrite/ReadWrite.ino | 2 ++ examples/listfiles/listfiles.ino | 2 ++ src/File.cpp | 2 ++ src/SD.h | 2 -- 8 files changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/Datalogger/Datalogger.ino b/examples/Datalogger/Datalogger.ino index c5a509c..592ac68 100644 --- a/examples/Datalogger/Datalogger.ino +++ b/examples/Datalogger/Datalogger.ino @@ -26,6 +26,8 @@ #include #include +using namespace SDLib; + const int chipSelect = 10; void setup() { diff --git a/examples/DumpFile/DumpFile.ino b/examples/DumpFile/DumpFile.ino index b6e9944..b6d1305 100644 --- a/examples/DumpFile/DumpFile.ino +++ b/examples/DumpFile/DumpFile.ino @@ -25,6 +25,8 @@ const int chipSelect = 10; +using namespace SDLib; + void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); diff --git a/examples/Files/Files.ino b/examples/Files/Files.ino index 2df0269..64f8b0a 100644 --- a/examples/Files/Files.ino +++ b/examples/Files/Files.ino @@ -21,6 +21,8 @@ */ #include +using namespace SDLib; + const int chipSelect = 10; File myFile; diff --git a/examples/NonBlockingWrite/NonBlockingWrite.ino b/examples/NonBlockingWrite/NonBlockingWrite.ino index 101bfd0..8a21244 100644 --- a/examples/NonBlockingWrite/NonBlockingWrite.ino +++ b/examples/NonBlockingWrite/NonBlockingWrite.ino @@ -33,6 +33,8 @@ */ #include +using namespace SDLib; + const int chipSelect = 10; // file name to use for writing diff --git a/examples/ReadWrite/ReadWrite.ino b/examples/ReadWrite/ReadWrite.ino index b505a27..aecbb35 100644 --- a/examples/ReadWrite/ReadWrite.ino +++ b/examples/ReadWrite/ReadWrite.ino @@ -20,6 +20,8 @@ */ #include +using namespace SDLib; + const int chipSelect = 10; File myFile; diff --git a/examples/listfiles/listfiles.ino b/examples/listfiles/listfiles.ino index ded9b13..1a7be4f 100644 --- a/examples/listfiles/listfiles.ino +++ b/examples/listfiles/listfiles.ino @@ -28,6 +28,8 @@ */ #include +using namespace SDLib; + const int chipSelect = 10; File root; diff --git a/src/File.cpp b/src/File.cpp index 3e27fb4..1a9e8e3 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -18,6 +18,8 @@ uint8_t nfilecount=0; */ +using namespace SDLib; + File::File(SdFile f, const char *n) { // oh man you are kidding me, new() doesn't exist? Ok we do it by hand! _file = (SdFile *)malloc(sizeof(SdFile)); diff --git a/src/SD.h b/src/SD.h index c81a7d3..340948b 100644 --- a/src/SD.h +++ b/src/SD.h @@ -126,8 +126,6 @@ namespace SDLib { // We enclose File and SD classes in namespace SDLib to avoid conflicts // with others legacy libraries that redefines File class. -// This ensure compatibility with sketches that uses only SD library -using namespace SDLib; // This allows sketches to use SDLib::File with other libraries (in the // sketch you must use SDFile instead of File to disambiguate) From a9739413913781eba7c63178bd54438d69d68e34 Mon Sep 17 00:00:00 2001 From: Woutvstk Date: Mon, 16 Dec 2024 15:20:24 +0100 Subject: [PATCH 2/6] added function in SDClass to request card size --- src/SD.cpp | 15 ++++++++++++++- src/SD.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/SD.cpp b/src/SD.cpp index 423db45..fd10eb0 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -50,7 +50,7 @@ */ -#include "SD.h" +#include namespace SDLib { @@ -366,6 +366,19 @@ namespace SDLib { root.openRoot(volume); } + /** + Get information about the volume size + + @return Returns the volume size in kB of the first volume/partition + */ + uint32_t SDClass::getvolumesize() + { + uint32_t volumesize = volume.blocksPerCluster(); // clusters are collections of blocks + volumesize *= volume.clusterCount(); // we'll have a lot of clusters + volumesize /= 2; + return (volumesize); //2 blocks make 1kB + } + //call this when a card is removed. It will allow you to insert and initialise a new card. void SDClass::end() { root.close(); diff --git a/src/SD.h b/src/SD.h index 340948b..8879689 100644 --- a/src/SD.h +++ b/src/SD.h @@ -106,6 +106,8 @@ namespace SDLib { return rmdir(filepath.c_str()); } + uint32_t getvolumesize(); + private: // This is used to determine the mode used to open a file From 5d5730a64ed09017dbdbf174f10fd0ab3eb5f451 Mon Sep 17 00:00:00 2001 From: Woutvstk Date: Mon, 16 Dec 2024 15:24:58 +0100 Subject: [PATCH 3/6] revert change to #include "SD.h" --- src/File.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/File.cpp b/src/File.cpp index 1a9e8e3..bf83f91 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -12,7 +12,7 @@ */ -#include +#include "SD.h" /* for debugging file open/close leaks uint8_t nfilecount=0; From b31c790f7d4f726d0409840139d010c37af44661 Mon Sep 17 00:00:00 2001 From: Woutvstk Date: Mon, 16 Dec 2024 16:56:04 +0100 Subject: [PATCH 4/6] added getCardType function to SDClass --- src/SD.cpp | 17 +++++++++++++---- src/SD.h | 4 +++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/SD.cpp b/src/SD.cpp index fd10eb0..5692c14 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -371,14 +371,23 @@ namespace SDLib { @return Returns the volume size in kB of the first volume/partition */ - uint32_t SDClass::getvolumesize() + uint32_t SDClass::getVolumeSize() { uint32_t volumesize = volume.blocksPerCluster(); // clusters are collections of blocks - volumesize *= volume.clusterCount(); // we'll have a lot of clusters - volumesize /= 2; + volumesize *= volume.clusterCount(); // we'll have a lot of clusters + volumesize /= 2; return (volumesize); //2 blocks make 1kB } - + + /** + Get card type + @return 0:none, 1:SDv1, 2:SDv2, 3:SDHC + */ + uint8_t SDClass::getCardType() + { + return card.type(); + } + //call this when a card is removed. It will allow you to insert and initialise a new card. void SDClass::end() { root.close(); diff --git a/src/SD.h b/src/SD.h index 8879689..ea73ad8 100644 --- a/src/SD.h +++ b/src/SD.h @@ -106,8 +106,10 @@ namespace SDLib { return rmdir(filepath.c_str()); } - uint32_t getvolumesize(); + uint32_t getVolumeSize(); + uint8_t getCardType(); + private: // This is used to determine the mode used to open a file From f43a1afe82e17974cb13b7810dae9675b2c1e69f Mon Sep 17 00:00:00 2001 From: Woutvstk Date: Tue, 17 Dec 2024 21:47:45 +0100 Subject: [PATCH 5/6] Changed SDFilesystem to SD for compatibility --- src/SD.cpp | 2 +- src/SD.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SD.cpp b/src/SD.cpp index 5692c14..9cfa73d 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -656,6 +656,6 @@ namespace SDLib { } } - SDClass SD; + SDClass SDSDcard; }; diff --git a/src/SD.h b/src/SD.h index ea73ad8..cb1fbfa 100644 --- a/src/SD.h +++ b/src/SD.h @@ -123,7 +123,7 @@ namespace SDLib { friend bool callback_openPath(SdFile&, const char *, bool, void *); }; - extern SDClass SD; + extern SDClass SDcard; }; @@ -135,6 +135,6 @@ namespace SDLib { // sketch you must use SDFile instead of File to disambiguate) typedef SDLib::File SDFile; typedef SDLib::SDClass SDFileSystemClass; -#define SDFileSystem SDLib::SD +#define SD SDLib::SDcard #endif From 09643d113126833938b8632bc1b971c222330fe8 Mon Sep 17 00:00:00 2001 From: Woutvstk Date: Tue, 17 Dec 2024 21:50:14 +0100 Subject: [PATCH 6/6] Revert "Changed SDFilesystem to SD for compatibility" This reverts commit f43a1afe82e17974cb13b7810dae9675b2c1e69f. --- src/SD.cpp | 2 +- src/SD.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SD.cpp b/src/SD.cpp index 9cfa73d..5692c14 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -656,6 +656,6 @@ namespace SDLib { } } - SDClass SDSDcard; + SDClass SD; }; diff --git a/src/SD.h b/src/SD.h index cb1fbfa..ea73ad8 100644 --- a/src/SD.h +++ b/src/SD.h @@ -123,7 +123,7 @@ namespace SDLib { friend bool callback_openPath(SdFile&, const char *, bool, void *); }; - extern SDClass SDcard; + extern SDClass SD; }; @@ -135,6 +135,6 @@ namespace SDLib { // sketch you must use SDFile instead of File to disambiguate) typedef SDLib::File SDFile; typedef SDLib::SDClass SDFileSystemClass; -#define SD SDLib::SDcard +#define SDFileSystem SDLib::SD #endif