From 36c5c3cfcc4c452b83bc3113c2f38e36cad363f5 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 28 Feb 2017 22:04:03 +0700 Subject: [PATCH] add Helper class for NFFS file system --- libraries/Bluefruit52Lib/src/bluefruit.cpp | 13 +- libraries/Bluefruit52Lib/src/bluefruit.h | 6 +- .../nffs/examples/filesystem/filesystem.ino | 29 ++-- .../nffs/src/{nffs_lib.h => NewtNffs.cpp} | 90 ++++++----- libraries/nffs/src/NewtNffs.h | 55 +++++++ libraries/nffs/src/NffsFile.cpp | 153 ++++++++++++++++++ libraries/nffs/src/NffsFile.h | 82 ++++++++++ 7 files changed, 364 insertions(+), 64 deletions(-) rename libraries/nffs/src/{nffs_lib.h => NewtNffs.cpp} (88%) mode change 100755 => 100644 create mode 100644 libraries/nffs/src/NewtNffs.h create mode 100644 libraries/nffs/src/NffsFile.cpp create mode 100644 libraries/nffs/src/NffsFile.h diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index bd962fd6..42d74b55 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -35,8 +35,7 @@ /**************************************************************************/ #include "bluefruit.h" - -#include +#include // Note chaning these parameters will affect APP_RAM_BASE // --> need to update RAM in feather52_s132.ld linker @@ -55,7 +54,9 @@ #define CFG_DEFAULT_NAME "Bluefruit52" #define CFG_ADV_BLINKY_INTERVAL 500 -#define BLUEFRUIT_TASK_STACKSIZE (512*3) +#define CFG_BLUEFRUIT_TASK_STACKSIZE (512*3) + +#define CFG_BOND_PATH "/adafruit/bond" extern "C" { @@ -183,7 +184,7 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable) // Create RTOS Task for BLE Event TaskHandle_t task_hdl; - xTaskCreate( adafruit_bluefruit_task, "BLE Evt", BLUEFRUIT_TASK_STACKSIZE, NULL, TASK_PRIO_HIGH, &task_hdl); + xTaskCreate( adafruit_bluefruit_task, "BLE Evt", CFG_BLUEFRUIT_TASK_STACKSIZE, NULL, TASK_PRIO_HIGH, &task_hdl); _ble_event_sem = xSemaphoreCreateBinary(); VERIFY(_ble_event_sem, NRF_ERROR_NO_MEM); @@ -192,9 +193,11 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable) // Create Timer for led advertising blinky _led_blink_th = xTimerCreate(NULL, ms2tick(CFG_ADV_BLINKY_INTERVAL), true, NULL, bluefruit_blinky_cb); - // Also initialize nffs for bonding/config + // Initialize nffs for bonding (it is safe to call nffs_pkg_init() multiple time) // nffs_pkg_init(); + // + return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 0e1a2760..62c04d12 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -69,7 +69,7 @@ class AdafruitBluefruit // bool setTxPower(int8_t power); /*------------------------------------------------------------------*/ - /* Advertising & Scan Resp (active scan) + /* Advertising & Scan Response (active scan) *------------------------------------------------------------------*/ uint8_t getAdvLen(void); uint8_t getAdvData(uint8_t* buffer); @@ -159,7 +159,7 @@ class AdafruitBluefruit int8_t _tx_power; - char _name[32+1]; // 32 including null terminator + char _name[32+1]; BLECharacteristic* _chars_list[BLE_MAX_CHARS]; uint8_t _chars_count; @@ -191,8 +191,8 @@ public: // temporary uint16_t sys_attr_len; uint8_t* sys_attr; }_bond_data; -private: +private: ble_gap_addr_t _peer_addr; // Transmission Buffer Count for HVX notification, max is seen at 7 diff --git a/libraries/nffs/examples/filesystem/filesystem.ino b/libraries/nffs/examples/filesystem/filesystem.ino index d1c63b66..8d28aa0d 100644 --- a/libraries/nffs/examples/filesystem/filesystem.ino +++ b/libraries/nffs/examples/filesystem/filesystem.ino @@ -13,29 +13,31 @@ *********************************************************************/ #include -#include +#include #define FILENAME "/adafruit.txt" #define CONTENTS "Bluefruit Feather52's NFFS file contents" +NffsFile file; + // the setup function runs once when you press reset or power the board void setup() { Serial.begin(115200); Serial.println("nffs example"); Bluefruit.begin(); - nffs_pkg_init(); - - struct fs_file *file; + Nffs.begin(); + + file.open(FILENAME, FS_ACCESS_READ); // file existed - if ( 0 == fs_open(FILENAME, FS_ACCESS_READ, &file)) + if ( file.existed() ) { Serial.println(FILENAME " file existed"); uint32_t readlen; char buffer[64] = { 0 }; - fs_read(file, sizeof(buffer), buffer, &readlen); + readlen = file.read(buffer, sizeof(buffer)); buffer[readlen] = 0; Serial.println(buffer); @@ -43,11 +45,11 @@ void setup() { { Serial.print("Open " FILENAME " file to write ... "); - if( 0 == fs_open(FILENAME, FS_ACCESS_WRITE, &file) ) + if( file.open(FILENAME, FS_ACCESS_WRITE) ) { Serial.println("OK"); - fs_write(file, CONTENTS, strlen(CONTENTS)); - fs_close(file); + file.write(CONTENTS, strlen(CONTENTS)); + file.close(); }else { Serial.println("Failed (hint: paht must start with '//') "); @@ -56,9 +58,8 @@ void setup() { } // the loop function runs over and over again forever -void loop() { - digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) - delay(1000); // wait for a second - digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW - delay(1000); // wait for a second +void loop() +{ + digitalToggle(LED_BUILTIN); + delay(1000); } diff --git a/libraries/nffs/src/nffs_lib.h b/libraries/nffs/src/NewtNffs.cpp old mode 100755 new mode 100644 similarity index 88% rename from libraries/nffs/src/nffs_lib.h rename to libraries/nffs/src/NewtNffs.cpp index 145abb7b..e9aac2bf --- a/libraries/nffs/src/nffs_lib.h +++ b/libraries/nffs/src/NewtNffs.cpp @@ -1,43 +1,49 @@ -/**************************************************************************/ -/*! - @file nffs_lib.h - @author hathach - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2016, Adafruit Industries (adafruit.com) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -/**************************************************************************/ - -#ifndef _NFFS_LIB_H_ -#define _NFFS_LIB_H_ +/**************************************************************************/ +/*! + @file NewtNffs.cpp + @author hathach -#include "nffs/nffs.h" -#include "fs/fsutil.h" - -#endif /* _NFFS_LIB_H_ */ + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include "NewtNffs.h" + +NewtNffs Nffs; + +NewtNffs::NewtNffs(void) +{ + +} + +void NewtNffs::begin(void) +{ + nffs_pkg_init(); +} diff --git a/libraries/nffs/src/NewtNffs.h b/libraries/nffs/src/NewtNffs.h new file mode 100644 index 00000000..7de1ddef --- /dev/null +++ b/libraries/nffs/src/NewtNffs.h @@ -0,0 +1,55 @@ +/**************************************************************************/ +/*! + @file NewtNffs.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef NEWTNFFS_H_ +#define NEWTNFFS_H_ + +#include "nffs/nffs.h" +#include "fs/fsutil.h" + +#include "NffsFile.h" + +class NewtNffs +{ +public: + NewtNffs(void); + + void begin(void); +}; + + +extern NewtNffs Nffs; + +#endif /* NEWTNFFS_H_ */ diff --git a/libraries/nffs/src/NffsFile.cpp b/libraries/nffs/src/NffsFile.cpp new file mode 100644 index 00000000..2fb86156 --- /dev/null +++ b/libraries/nffs/src/NffsFile.cpp @@ -0,0 +1,153 @@ +/**************************************************************************/ +/*! + @file NffsFile.cpp + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include "NewtNffs.h" + +void NffsFile::_init(void) +{ + _file = NULL; + errnum = 0; +} + +NffsFile::NffsFile(void) +{ + _init(); +} + +NffsFile::NffsFile(const char* path, uint8_t flags) +{ + _init(); + open(path, flags); +} + +bool NffsFile::open(const char* path, uint8_t flags) +{ + errnum = fs_open(path, flags, &_file); + + return (errnum == FS_EOK); +} + +bool NffsFile::existed(void) +{ + return _file != NULL; +} + +bool NffsFile::close(void) +{ + errnum = fs_close(_file); + + return (errnum == FS_EOK); +} + +uint32_t NffsFile::size(void) +{ + uint32_t size = 0; + errnum = fs_filelen(_file, &size); + + return size; +} + +uint32_t NffsFile::tell(void) +{ + return fs_getpos(_file); +} + +bool NffsFile::seek(uint32_t offset) +{ + errnum = fs_seek(_file, offset); + + return (errnum == FS_EOK); +} + +bool NffsFile::seekForward (uint32_t offset) +{ + errnum = fs_seek(_file, tell() + offset); + + return (errnum == FS_EOK); +} + +bool NffsFile::seekBackward(uint32_t offset) +{ + errnum = fs_seek(_file, tell() - offset); + + return (errnum == FS_EOK); +} + + +int NffsFile::read( void ) +{ + uint8_t data; + return (this->read(&data, 1) == 1) ? data : EOF; +} + +size_t NffsFile::read(uint8_t * buf, size_t size) +{ + size_t readlen = 0; + errnum = fs_read(_file, (uint32_t) size, buf, (uint32_t*) &readlen); + + return readlen; +} + +size_t NffsFile::write( uint8_t b ) +{ + return this->write(&b, 1); +} + +size_t NffsFile::write( const uint8_t *data, size_t len ) +{ + errnum = fs_write(_file, data, len); + + return (errnum == FS_EOK) ? len : 0; +} + +int NffsFile::available( void ) +{ + return size() - tell(); +} + +int NffsFile::peek( void ) +{ + int ch = read(); + seekBackward(1); + return ch; +} + +void NffsFile::flush( void ) +{ + // do nothing +} + + diff --git a/libraries/nffs/src/NffsFile.h b/libraries/nffs/src/NffsFile.h new file mode 100644 index 00000000..4557bb5f --- /dev/null +++ b/libraries/nffs/src/NffsFile.h @@ -0,0 +1,82 @@ +/**************************************************************************/ +/*! + @file NffsFile.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef NFFSFILE_H_ +#define NFFSFILE_H_ + +#include + +class NffsFile : public Stream +{ +private: + struct fs_file* _file; + + void _init(void); + +public: + int errnum; + + NffsFile(void); + NffsFile(const char* path, uint8_t flags = FS_ACCESS_READ); + + bool open(const char* path, uint8_t flags = FS_ACCESS_READ); + bool close(void); + bool existed(void); + + uint32_t size(void); + uint32_t tell(void); + bool seek(uint32_t offset); + bool seekForward (uint32_t offset); + bool seekBackward(uint32_t offset); + + // Stream API + virtual size_t write ( uint8_t b ); + virtual size_t write ( const uint8_t *data, size_t len ); + virtual int available ( void ); + virtual int peek ( void ); + virtual void flush ( void ); + virtual int read ( void ); + + size_t read ( uint8_t * buf, size_t size ); + size_t read ( char * buf, size_t size ) + { + return read( (uint8_t*) buf, size); + } + + + using Print::write; +}; + +#endif /* NFFSFILE_H_ */