Skip to content

Commit acc52e0

Browse files
committed
AudioSourceIdxSDFAT, AudioSourceSDFAT as template class
1 parent aea6a4a commit acc52e0

File tree

2 files changed

+20
-38
lines changed

2 files changed

+20
-38
lines changed

src/AudioTools/Disk/AudioSourceIdxSDFAT.h

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,21 @@
1010
#define USE_SDFAT
1111
#include "AudioTools/Disk/SDIndex.h"
1212

13-
// SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
14-
// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
15-
#ifndef SD_FAT_TYPE
16-
#define SD_FAT_TYPE 1
17-
#endif
1813
// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur. (40?)
1914
#define SPI_CLOCK SD_SCK_MHZ(50)
20-
#if SD_FAT_TYPE == 0
21-
typedef SdFat AudioFs;
22-
typedef File AudioFile;
23-
#elif SD_FAT_TYPE == 1
24-
typedef SdFat32 AudioFs;
25-
typedef File32 AudioFile;
26-
#elif SD_FAT_TYPE == 2
27-
typedef SdExFat AudioFs;
28-
typedef ExFile AudioFile;
29-
#elif SD_FAT_TYPE == 3
30-
typedef SdFs AudioFs;
31-
typedef FsFile AudioFile;
32-
#endif
3315

3416
namespace audio_tools {
3517
/**
3618
* @brief ESP32 AudioSource for AudioPlayer using an SD card as data source.
3719
* An index file is used to speed up the access to the audio files by index.
3820
* This class is based on the Arduino SD implementation
3921
* For UTF8 Support change SdFatConfig.h #define USE_UTF8_LONG_NAMES 1
22+
* @param <SdFat32, File32>, <SdFs, FsFile>, <SdExFat, ExFile>, <SdFat, File>
4023
* @ingroup player
4124
* @author Phil Schatzmann
4225
* @copyright GPLv3
4326
*/
27+
template <typename AudioFs = SdFat32, typename AudioFile = File32>
4428
class AudioSourceIdxSDFAT : public AudioSource {
4529
public:
4630
/// Default constructor
@@ -69,6 +53,20 @@ class AudioSourceIdxSDFAT : public AudioSource {
6953
setup_index = setupIndex;
7054
}
7155

56+
/// Constructor for providing an open FS
57+
AudioSourceIdxSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex = true){
58+
TRACED();
59+
sd = fs;
60+
p_cfg = nullptr;
61+
owns_cfg = false;
62+
start_path = startFilePath;
63+
exension = ext;
64+
setup_index = setupIndex;
65+
is_sd_setup = true;
66+
// since we expect an open fs we do not close it
67+
is_close_sd = false;
68+
}
69+
7270
virtual ~AudioSourceIdxSDFAT() { end(); }
7371

7472
virtual void begin() override {
@@ -87,7 +85,7 @@ class AudioSourceIdxSDFAT : public AudioSource {
8785
void end() {
8886
if (is_sd_setup) {
8987
#ifdef ESP32
90-
sd.end();
88+
if (is_close_sd) sd.end();
9189
#endif
9290
if (owns_cfg) delete (p_cfg);
9391
is_sd_setup = false;
@@ -162,6 +160,7 @@ class AudioSourceIdxSDFAT : public AudioSource {
162160
const char *start_path = nullptr;
163161
const char *file_name_pattern = "*";
164162
bool setup_index = true;
163+
bool is_close_sd = true;
165164
bool is_sd_setup = false;
166165
int cs;
167166
bool owns_cfg = false;

src/AudioTools/Disk/AudioSourceSDFAT.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,21 @@
1010
#define USE_SDFAT
1111
#include "AudioTools/Disk/SDDirect.h"
1212

13-
// SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
14-
// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
15-
#ifndef SD_FAT_TYPE
16-
#define SD_FAT_TYPE 1
17-
#endif
1813
// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur. (40?)
1914
#define SPI_CLOCK SD_SCK_MHZ(50)
2015

21-
#if SD_FAT_TYPE == 0
22-
typedef SdFat AudioFs;
23-
typedef File AudioFile;
24-
#elif SD_FAT_TYPE == 1
25-
typedef SdFat32 AudioFs;
26-
typedef File32 AudioFile;
27-
#elif SD_FAT_TYPE == 2
28-
typedef SdExFat AudioFs;
29-
typedef ExFile AudioFile;
30-
#elif SD_FAT_TYPE == 3
31-
typedef SdFs AudioFs;
32-
typedef FsFile AudioFile;
33-
#endif
34-
3516
namespace audio_tools {
3617
/**
3718
* @brief ESP32 AudioSource for AudioPlayer using an SD card as data source.
3819
* This class is based on the Arduino SD implementation
3920
* Connect the SD card.
4021
* For UTF8 Support change SdFatConfig.h #define USE_UTF8_LONG_NAMES 1
22+
* @param <SdFat32, File32>, <SdFs, FsFile>, <SdExFat, ExFile>, <SdFat, File>
4123
* @ingroup player
4224
* @author Phil Schatzmann
4325
* @copyright GPLv3
4426
*/
27+
template <typename AudioFs = SdFat32, typename AudioFile = File32>
4528
class AudioSourceSDFAT : public AudioSource {
4629
public:
4730
/// Default constructor

0 commit comments

Comments
 (0)