forked from arduino-libraries/WiFiNINA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request arduino-libraries#74 from arduino-libraries/unowif…
…irev2_ota Add helpers for Filesystem operations
- Loading branch information
Showing
9 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
This example shows how to interact with NiNa internal memory partition | ||
APIs are modeled on SerialFlash library (not on SD) to speedup operations and avoid buffers. | ||
*/ | ||
|
||
#include <WiFiNINA.h> | ||
|
||
void setup() { | ||
|
||
Serial.begin(115200); | ||
while (!Serial); | ||
|
||
// check for the presence of the shield: | ||
if (WiFi.status() == WL_NO_SHIELD) { | ||
Serial.println("WiFi shield not present"); | ||
// don't continue: | ||
while (true); | ||
} | ||
|
||
WiFiStorageFile file = WiFiStorage.open("/fs/testfile"); | ||
|
||
if (file) { | ||
file.erase(); | ||
} | ||
|
||
String test = "Cantami o Diva del pelide Achille"; | ||
file.write(test.c_str(), test.length()); | ||
|
||
if (file) { | ||
file.seek(0); | ||
while (file.available()) { | ||
uint8_t buf[128]; | ||
int ret = file.read(buf, 128); | ||
Serial.write(buf, ret); | ||
} | ||
} | ||
} | ||
|
||
void loop() { | ||
// put your main code here, to run repeatedly: | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
WiFiStorage.cpp - Library for Arduino boards based on NINA wifi module. | ||
Copyright (c) 2018 Arduino SA. All rights reserved. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include "WiFiStorage.h" | ||
|
||
WiFiStorageFile WiFiStorageClass::open(const char *filename) { | ||
WiFiStorageFile file(filename); | ||
file.size(); | ||
return file; | ||
} | ||
|
||
WiFiStorageFile WiFiStorageClass::open(String filename) { | ||
return open(filename.c_str()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
WiFiStorage.h - Library for Arduino boards based on NINA wifi module. | ||
Copyright (c) 2018 Arduino SA. All rights reserved. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef wifistorage_h | ||
#define wifistorage_h | ||
|
||
#include "utility/wifi_drv.h" | ||
|
||
class WiFiStorageFile; | ||
|
||
class WiFiStorageClass | ||
{ | ||
public: | ||
static bool begin(); | ||
|
||
static WiFiStorageFile open(const char *filename); | ||
static WiFiStorageFile open(String filename); | ||
|
||
static bool exists(const char *filename) { | ||
uint32_t len; | ||
return (WiFiDrv::existsFile(filename, strlen(filename), &len) > 0); | ||
} | ||
static bool exists(const char *filename, uint32_t* len) { | ||
return (WiFiDrv::existsFile(filename, strlen(filename), len) > 0); | ||
} | ||
static bool remove(const char *filename) { | ||
WiFiDrv::deleteFile(filename, strlen(filename)); | ||
return true; | ||
} | ||
static bool rename(const char * old_file_name, const char * new_file_name) { | ||
return (WiFiDrv::renameFile(old_file_name, strlen(old_file_name), new_file_name, strlen(new_file_name)) == 0); | ||
} | ||
static bool read(const char *filename, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) { | ||
WiFiDrv::readFile(filename, strlen(filename), offset, buffer, buffer_len); | ||
return true; | ||
} | ||
static bool write(const char *filename, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) { | ||
WiFiDrv::writeFile(filename, strlen(filename), offset, buffer, buffer_len); | ||
return true; | ||
} | ||
static bool download(const char* url, const char *filename) { | ||
WiFiDrv::downloadFile(url, strlen(url), filename, strlen(filename)); | ||
return true; | ||
} | ||
|
||
static bool remove(String filename) { | ||
return remove(filename.c_str()); | ||
} | ||
static bool rename(String old_file_name, String new_file_name) { | ||
return rename(old_file_name.c_str(), new_file_name.c_str()); | ||
} | ||
static bool read(String filename, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) { | ||
return read(filename.c_str(), offset, buffer, buffer_len); | ||
} | ||
static bool write(String filename, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) { | ||
return write(filename.c_str(), offset, buffer, buffer_len); | ||
} | ||
static bool download(String url, String filename) { | ||
return download(url.c_str(), filename.c_str()); | ||
} | ||
}; | ||
|
||
extern WiFiStorageClass WiFiStorage; | ||
|
||
|
||
class WiFiStorageFile | ||
{ | ||
public: | ||
constexpr WiFiStorageFile(const char* _filename) : filename(_filename) { } | ||
|
||
operator bool() { | ||
return WiFiStorage.exists(filename, &length); | ||
} | ||
uint32_t read(void *buf, uint32_t rdlen) { | ||
if (offset + rdlen > length) { | ||
if (offset >= length) return 0; | ||
rdlen = length - offset; | ||
} | ||
WiFiStorage.read(filename, offset, (uint8_t*)buf, rdlen); | ||
offset += rdlen; | ||
return rdlen; | ||
} | ||
uint32_t write(const void *buf, uint32_t wrlen) { | ||
WiFiStorage.write(filename, offset, (uint8_t*)buf, wrlen); | ||
offset += wrlen; | ||
return wrlen; | ||
} | ||
void seek(uint32_t n) { | ||
offset = n; | ||
} | ||
uint32_t position() { | ||
return offset; | ||
} | ||
uint32_t size() { | ||
WiFiStorage.exists(filename, &length); | ||
return length; | ||
} | ||
uint32_t available() { | ||
WiFiStorage.exists(filename, &length); | ||
return length - offset; | ||
} | ||
void erase() { | ||
offset = 0; | ||
WiFiStorage.remove(filename); | ||
} | ||
void flush(); | ||
void close() { | ||
offset = 0; | ||
} | ||
protected: | ||
friend class WiFiStorageClass; | ||
uint32_t offset = 0; | ||
uint32_t length = 0; | ||
const char* filename; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.