diff --git a/Examples/PropWare_FileReader/FileReader_Demo.cpp b/Examples/PropWare_FileReader/FileReader_Demo.cpp
index 5862c48d..6ed805b2 100644
--- a/Examples/PropWare_FileReader/FileReader_Demo.cpp
+++ b/Examples/PropWare_FileReader/FileReader_Demo.cpp
@@ -44,15 +44,9 @@ using PropWare::BlockStorage;
int main() {
const SD driver;
FatFS filesystem(driver);
- uint8_t dataBuffer[driver.get_sector_size()];
- filesystem.mount(dataBuffer);
+ filesystem.mount();
- BlockStorage::MetaData bufferMetaData;
- BlockStorage::Buffer buffer = {
- buf: dataBuffer,
- meta: &bufferMetaData
- };
- FatFileReader reader(filesystem, "fat_test.txt", buffer);
+ FatFileReader reader(filesystem, "fat_test.txt");
reader.open();
while (!reader.eof())
diff --git a/Examples/PropWare_FileWriter/CMakeLists.txt b/Examples/PropWare_FileWriter/CMakeLists.txt
index b152b49c..e773407e 100644
--- a/Examples/PropWare_FileWriter/CMakeLists.txt
+++ b/Examples/PropWare_FileWriter/CMakeLists.txt
@@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.3)
find_package(PropWare REQUIRED)
set(BOARD activityboard)
-set(MODEL cmm)
project(FileWriter_Demo)
diff --git a/Examples/PropWare_FileWriter/FileWriter_Demo.cpp b/Examples/PropWare_FileWriter/FileWriter_Demo.cpp
index 89728d0b..31886b10 100644
--- a/Examples/PropWare_FileWriter/FileWriter_Demo.cpp
+++ b/Examples/PropWare_FileWriter/FileWriter_Demo.cpp
@@ -48,12 +48,6 @@ int main() {
const SD driver;
FatFS filesystem(driver);
- uint8_t readBufferData[driver.get_sector_size()];
- BlockStorage::MetaData readMetaData;
- BlockStorage::Buffer readBuffer = {
- buf: readBufferData,
- meta: &readMetaData
- };
uint8_t writeBufferData[driver.get_sector_size()];
BlockStorage::MetaData writeMetaData;
BlockStorage::Buffer writeBuffer = {
@@ -61,9 +55,9 @@ int main() {
meta: &writeMetaData
};
- filesystem.mount(readBufferData);
+ filesystem.mount();
- FatFileReader reader(filesystem, "fat_test.txt", readBuffer);
+ FatFileReader reader(filesystem, "fat_test.txt");
FatFileWriter writer(filesystem, "new2.txt", writeBuffer);
reader.open();
diff --git a/PropWare/CMakeLists.txt b/PropWare/CMakeLists.txt
index a1172bde..4217d38f 100644
--- a/PropWare/CMakeLists.txt
+++ b/PropWare/CMakeLists.txt
@@ -25,6 +25,7 @@ set(PROPWARE_SOURCES
${CMAKE_CURRENT_LIST_DIR}/memory/blockstorage.h
${CMAKE_CURRENT_LIST_DIR}/memory/eeprom.h
${CMAKE_CURRENT_LIST_DIR}/memory/sd.h
+ ${CMAKE_CURRENT_LIST_DIR}/memory/sharedbuffers.cpp
${CMAKE_CURRENT_LIST_DIR}/motor/stepper.h
${CMAKE_CURRENT_LIST_DIR}/sensor/analog/mcp3xxx.h
${CMAKE_CURRENT_LIST_DIR}/sensor/analog/pcf8591.h
diff --git a/PropWare/filesystem/fat/fatfilereader.h b/PropWare/filesystem/fat/fatfilereader.h
index 69c01591..66083138 100644
--- a/PropWare/filesystem/fat/fatfilereader.h
+++ b/PropWare/filesystem/fat/fatfilereader.h
@@ -30,6 +30,8 @@
namespace PropWare {
+extern BlockStorage::Buffer SHARED_BUFFER;
+
/**
* @brief Read a file on a FAT 16 or FAT 32 storage device
*
@@ -88,7 +90,8 @@ class FatFileReader : virtual public FatFile, virtual public FileReader {
* @param[in] logger This is only used for printing debug statements. Use of the logger is limited
* such that all references will be optimized out in normal application code
*/
- FatFileReader (FatFS &fs, const char name[], BlockStorage::Buffer &buffer, const Printer &logger = pwOut)
+ FatFileReader (FatFS &fs, const char name[], BlockStorage::Buffer &buffer = SHARED_BUFFER,
+ const Printer &logger = pwOut)
: File(fs, name, buffer, logger),
FatFile(fs, name, buffer, logger),
FileReader(fs, name, buffer, logger) {
diff --git a/PropWare/filesystem/fat/fatfilewriter.h b/PropWare/filesystem/fat/fatfilewriter.h
index 45d82d45..9ec1d582 100644
--- a/PropWare/filesystem/fat/fatfilewriter.h
+++ b/PropWare/filesystem/fat/fatfilewriter.h
@@ -30,6 +30,8 @@
namespace PropWare {
+extern BlockStorage::Buffer SHARED_BUFFER;
+
/**
* @brief Concrete class for writing or modifying a FAT 16/32 file
*/
@@ -46,7 +48,8 @@ class FatFileWriter : public virtual FatFile, public virtual FileWriter {
* @param[in] logger This is only used for printing debug statements. Use of the logger is limited
* such that all references will be optimized out in normal application code
*/
- FatFileWriter (FatFS &fs, const char name[], BlockStorage::Buffer &buffer, const Printer &logger = pwOut)
+ FatFileWriter (FatFS &fs, const char name[], BlockStorage::Buffer &buffer = SHARED_BUFFER,
+ const Printer &logger = pwOut)
: File(fs, name, buffer, logger),
FatFile(fs, name, buffer, logger),
FileWriter(fs, name, buffer, logger) {
diff --git a/PropWare/filesystem/fat/fatfs.h b/PropWare/filesystem/fat/fatfs.h
index 6db95ac3..a1974f1d 100644
--- a/PropWare/filesystem/fat/fatfs.h
+++ b/PropWare/filesystem/fat/fatfs.h
@@ -34,6 +34,9 @@
namespace PropWare {
+extern uint8_t HALF_K_DATA_BUFFER1[];
+extern uint8_t HALF_K_DATA_BUFFER2[];
+
/**
* FAT 16/32 filesystem driver - can be used with SD cards or any other PropWare::BlockStorage device
*/
@@ -62,16 +65,22 @@ class FatFS : public Filesystem {
/**
* @brief Constructor
*
- * @param[in] *driver Address of a the driver which is capable of reading the physical hardware. Commonly,
- * this would be an instance of PropWare::SD, but one could potentially write a driver
- * for a floppy disk or CD driver or any other block storage device
- * @param[in] *logger Useful for debugging, a logger can be given to help determine when something goes
- * wrong. All code using the logger will be optimized out by GCC so long as you only
- * call public method
+ * @param[in] *driver Address of a the driver which is capable of reading the physical hardware.
+ * Commonly, this would be an instance of PropWare::SD, but one could
+ * potentially write a driver for a floppy disk or CD driver or any other block
+ * storage device
+ * @param[in] fatBuffer[] Byte array that can be used to store blocks of the file allocation table (FAT)
+ * used during mounting, file searching, and file expansion.
+ * @param[in] *logger Useful for debugging, a logger can be given to help determine when something
+ * goes wrong. All code using the logger will be optimized out by GCC so long as
+ * you only call public method
+ *
+ * @warning The default value for the `buffer[]` parameter is only valid when using a SD card or other
+ * `PropWare::BlockStorage` driver with a block size of 512-bytes or less.
*/
- FatFS (const BlockStorage &driver, const Printer &logger = pwOut)
+ FatFS (const BlockStorage &driver, uint8_t fatBuffer[] = HALF_K_DATA_BUFFER1, const Printer &logger = pwOut)
: Filesystem(driver, logger),
- m_fat(NULL),
+ m_fat(fatBuffer),
m_fatMod(false) {
}
@@ -87,8 +96,11 @@ class FatFS : public Filesystem {
/**
* @see PropWare::Filesystem::mount
+ *
+ * @warning The default value for the `buffer[]` parameter is only valid when using a SD card or other
+ * `PropWare::BlockStorage` driver with a block size of 512-bytes or less.
*/
- PropWare::ErrorCode mount (uint8_t buffer[], const uint8_t partition = 0) {
+ PropWare::ErrorCode mount (uint8_t buffer[] = HALF_K_DATA_BUFFER2, const uint8_t partition = 0) {
PropWare::ErrorCode err;
if (this->m_mounted)
@@ -101,9 +113,6 @@ class FatFS : public Filesystem {
this->m_fatMod = false;
this->m_nextFileId = 0;
- // Allocate buffer for the FAT
- if (NULL == this->m_fat)
- this->m_fat = (uint8_t *) malloc(this->m_sectorSize);
this->m_dirMeta.name = "Current working directory";
// Excellent information on determining FAT type can be found on page 14 of the following document,
@@ -125,17 +134,10 @@ class FatFS : public Filesystem {
* @see PropWare::Filesystem::unmount
*/
PropWare::ErrorCode unmount () {
- if (this->m_mounted) {
- PropWare::ErrorCode err;
-
- if (NULL != this->m_fat) {
- check_errors(this->flush_fat());
- free(this->m_fat);
- this->m_fat = NULL;
- }
- }
-
- return NO_ERROR;
+ if (this->m_mounted)
+ return this->flush_fat();
+ else
+ return NO_ERROR;
}
/**
diff --git a/PropWare/memory/sharedbuffers.cpp b/PropWare/memory/sharedbuffers.cpp
new file mode 100644
index 00000000..cef44480
--- /dev/null
+++ b/PropWare/memory/sharedbuffers.cpp
@@ -0,0 +1,41 @@
+/**
+ * @file PropWare/memory/sd.cpp
+ *
+ * @author David Zemon
+ *
+ * @copyright
+ * The MIT License (MIT)
+ *
Copyright (c) 2013 David Zemon
+ *
Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include
+#include
+
+using PropWare::BlockStorage;
+
+namespace PropWare {
+
+uint8_t HALF_K_DATA_BUFFER1[512];
+uint8_t HALF_K_DATA_BUFFER2[512];
+BlockStorage::MetaData SHARED_BUFFER_METADATA;
+BlockStorage::Buffer SHARED_BUFFER = {
+ buf: HALF_K_DATA_BUFFER2,
+ meta: &SHARED_BUFFER_METADATA
+};
+
+}
\ No newline at end of file