YFROBOT创客社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9246|回复: 0
打印 上一主题 下一主题

ESP8266 core for Arduino学习:file system 学习

[复制链接]

签到天数: 868 天

[LV.10]以坛为家III

跳转到指定楼层
楼主
发表于 2016-3-24 14:40:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 AllBlue 于 2016-7-15 16:03 编辑

文章链接:http://esp8266.github.io/Arduino/versions/2.1.0/doc/filesystem.html

Flash layout

Even though file system is stored on the same flash chip as the program, programming new sketch will not modify file system contents. This allows to use file system to store sketch data, configuration files, or content for Web server.

The following diagram illustrates flash layout used in Arduino environment:

|--------------|-------|---------------|--|--|--|--|--|
^              ^       ^               ^     ^
Sketch    OTA update   File system   EEPROM  WiFi config (SDK)

File system size depends on the flash chip size. Depending on the board which is selected in IDE, you have the following options for flash size:

BoardFlash chip size, bytesFile system size, bytes
Generic module512k64k, 128k
Generic module1M64k, 128k, 256k, 512k
Generic module2M1M
Generic module4M3M
Adafruit HUZZAH4M1M, 3M
ESPresso Lite 1.04M1M, 3M
ESPresso Lite 2.04M1M, 3M
NodeMCU 0.94M1M, 3M
NodeMCU 1.04M1M, 3M
Olimex MOD-WIFI-ESP8266(-DEV)2M1M
SparkFun Thing512k64k
SweetPea ESP-2104M1M, 3M
WeMos D1 & D1 mini4M1M, 3M
ESPDuino4M1M, 3M

Note: to use any of file system functions in the sketch, add the following include to the sketch:

#include "FS.h"
Uploading files to file system

ESP8266FS is a tool which integrates into the Arduino IDE. It adds a menu item toTools menu for uploading the contents of sketch data directory into ESP8266 flash file system.

  • Download the tool: https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.2.0/ESP8266FS-0.2.0.zip.
  • In your Arduino sketchbook directory, create tools directory if it doesn't exist yet
  • Unpack the tool into tools directory (the path will look like<home_dir>/Arduino/tools/ESP8266FS/tool/esp8266fs.jar)
  • Restart Arduino IDE
  • Open a sketch (or create a new one and save it)
  • Go to sketch directory (choose Sketch > Show Sketch Folder)
  • Create a directory named data and any files you want in the file system there
  • Make sure you have selected a board, port, and closed Serial Monitor
  • Select Tools > ESP8266 Sketch Data Upload. This should start uploading the files into ESP8266 flash file system. When done, IDE status bar will displaySPIFFS Image Uploaded message.
File system object (SPIFFS)
begin
SPIFFS.begin()

This method mounts SPIFFS file system. It must be called before any other FS APIs are used. Returns true if file system was mounted successfully, false otherwise.

format
SPIFFS.format()

Formats the file system. May be called either before or after calling begin. Returnstrue if formatting was successful.

open
SPIFFS.open(path, mode)

Opens a file. path should be an absolute path starting with a slash (e.g./dir/filename.txt). mode is a string specifying access mode. It can be one of "r", "w", "a", "r+", "w+", "a+". Meaning of these modes is the same as for fopen C function.

Returns File object. To check whether the file was opened successfully, use the boolean operator.


[C] 纯文本查看 复制代码
File f = SPIFFS.open("/f.txt", "w");
if (!f) {    
Serial.println("file open failed");
}

exists
SPIFFS.exists(path)

Returns true if a file with given path exists, false otherwise.

openDir
SPIFFS.openDir(path)

Opens a directory given its absolute path. Returns a Dir object.

remove
SPIFFS.remove(path)

Deletes the file given its absolute path. Returns true if file was deleted successfully.

rename
SPIFFS.rename(pathFrom, pathTo)

Renames file from pathFrom to pathTo. Paths must be absolute. Returns true if file was renamed successfully.

info
FSInfo fs_info;
SPIFFS.info(fs_info);

Fills FSInfo structure with information about the file system. Returns true is successful, false otherwise.

Filesystem information structure
[C] 纯文本查看 复制代码
struct FSInfo { 
 size_t totalBytes; 
 size_t usedBytes; 
 size_t blockSize; 
 size_t pageSize; 
 size_t maxOpenFiles; 
 size_t maxPathLength;
};

This is the structure which may be filled using FS::info method. - totalBytes — total size of useful data on the file system - usedBytes — number of bytes used by files - blockSize — SPIFFS block size - pageSize — SPIFFS logical page size -maxOpenFiles — max number of files which may be open simultaneously -maxPathLength — max file name length (including one byte for zero termination)

Directory object (Dir)

The purpose of Dir object is to iterate over files inside a directory. It provides three methods: next(), fileName(), and openFile(mode).

The following example shows how it should be used:

[C] 纯文本查看 复制代码
Dir dir = SPIFFS.openDir("/data");
while (dir.next()) {    
Serial.print(dir.fileName());   
File f = dir.openFile("r");    
Serial.println(f.size());
}

dir.next() returns true while there are files in the directory to iterate over. It must be called before calling fileName and openFile functions.

openFile method takes mode argument which has the same meaning as forSPIFFS.open function.

File object

SPIFFS.open and dir.openFile functions return a File object. This object supports all the functions of Stream, so you can use readBytes, findUntil,parseInt, println, and all other Stream methods.

There are also some functions which are specific to File object.

seek
file.seek(offset, mode)

This function behaves like fseek C function. Depending on the value of mode, it moves current position in a file as follows:

  • if mode is SeekSet, position is set to offset bytes from the beginning.
  • if mode is SeekCur, current position is moved by offset bytes.
  • if mode is SeekEnd, position is set to offset bytes from the end of the file.

Returns true if position was set successfully.

position
file.position()

Returns the current position inside the file, in bytes.

size
file.size()

Returns file size, in bytes.

name
String name = file.name();

Returns file name, as const char*. Convert it to String for storage.

close
file.close()

Close the file. No other operations should be performed on File object after closefunction was called.


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|联系我们|YFROBOT ( 苏ICP备20009901号-2  

GMT+8, 2024-12-27 05:32 , Processed in 0.048446 second(s), 25 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表