LoginSignup
4
3

More than 3 years have passed since last update.

ESP32のSPIFFSライブラリ作成

Last updated at Posted at 2019-04-24

 espressif(公式)がgithubで提供しているSPIFFSのサンプルコードをライブラリ化しました。
ライブラリ化したコードはgithubで公開しています。
(ライセンスはフリー)
元のソースコードはこちらです。

環境

 開発環境:Arduino-IDE(Arduino-ESP32)
 使用ボード:ESP-32-DevKit

関数一覧

・void begin(void)
  SPIFFSの初期化
・void listDir(const char *dirname, uint8_t levels);
  SPIフラッシュメモリ内のファイルをリスト表示
・void readFile(const char *path, char *buf);
  指定したパス(*path)のファイルを読み込んで、引数の*bufに格納
・void writeFile(const char *path, const char *message);
  指定したパス(*path)のファイルを作成し、文字列(*message)を書き込み
・void appendFile(const char *path, const char *message);
  指定したパス(*path)のファイルに文字列(*message)を追記
・void renameFile(const char *path1, const char *path2);
  指定したパス(*path1)のファイル名を指定したファイル名(*path2)に変更
・void deleteFile(const char *path);
  指定したファイル(*path)を削除
・void testFileIO(const char * path);
  SPIFFSの動作確認

サンプルコード

サンプルコードは各関数を順に実行するようになっています。
実行すると以下のようにシリアルコンソールへログが表示されます。

Serial console output.PNG

spiffs_example.ino
#include "esp32_spiffs.h"

ESP32_SPIFFS spiffs;

char file_buf[16384];

void setup() { 
  Serial.begin(115200);
  memset(file_buf,0,sizeof(file_buf));
  spiffs.begin();

  // ファイルを作成して文字列を書き込む
  spiffs.writeFile("/hello.txt", "Hello ");
  // 指定したファイルに文字列を追記
  spiffs.appendFile("/hello.txt", "World!\n");
  // ファイルをリスト表示
  spiffs.listDir("/", 0);
  // 指定したファイル名を変更
  spiffs.renameFile("/hello.txt", "/foo.txt");
  // 指定したファイルを読み込む
  spiffs.readFile("/foo.txt",file_buf);
  Serial.printf("%s\n",file_buf);
  // 指定したファイルを削除
  spiffs.deleteFile("/foo.txt");
//  spiffs.testFileIO("/test.txt");
//  spiffs.deleteFile("/test.txt");
  Serial.println( "Test complete" );  
}

void loop() {
}
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3