四博智联产品售后

 找回密码
 立即注册
搜索
查看: 12207|回复: 0

Arduino 文件系统

[复制链接]

253

主题

306

帖子

1831

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1831
发表于 2015-12-9 18:20:23 | 显示全部楼层 |阅读模式
  1. #include <ESP8266WiFi.h>
  2. #include "FS.h"


  3. void fail(const char* msg) {
  4.   Serial.println(msg);
  5.   while (true) {
  6.     yield();
  7.   }
  8. }

  9. void setup() {
  10.   Serial.begin(115200);
  11.   Serial.setDebugOutput(true);
  12.   WiFi.mode(WIFI_OFF);
  13.   Serial.println("\n\nFS test\n");

  14.   {
  15.     if (!SPIFFS.format()) {
  16.       fail("format failed");
  17.     }
  18.     Dir root = SPIFFS.openDir("/");
  19.     int count = 0;
  20.     while (root.next()) {
  21.       ++count;
  22.     }
  23.     if (count > 0) {
  24.       fail("some files left after format");
  25.     }
  26.   }


  27.   if (!SPIFFS.begin()) {
  28.     fail("SPIFFS init failed");
  29.   }

  30.   String text = "write test";
  31.   {
  32.     File out = SPIFFS.open("/tmp.txt", "w");
  33.     if (!out) {
  34.       fail("failed to open tmp.txt for writing");
  35.     }
  36.     out.print(text);
  37.   }

  38.   {
  39.     File in = SPIFFS.open("/tmp.txt", "r");
  40.     if (!in) {
  41.       fail("failed to open tmp.txt for reading");
  42.     }
  43.     Serial.printf("size=%d\r\n", in.size());
  44.     if (in.size() != text.length()) {
  45.       fail("invalid size of tmp.txt");
  46.     }
  47.     Serial.print("Reading data: ");
  48.     in.setTimeout(0);
  49.     String result = in.readString();
  50.     Serial.println(result);
  51.     if (result != text) {
  52.       fail("invalid data in tmp.txt");
  53.     }
  54.   }

  55.   {
  56.     for (int i = 0; i < 10; ++i) {
  57.       String name = "seq_";
  58.       name += i;
  59.       name += ".txt";

  60.       File out = SPIFFS.open(name, "w");
  61.       if (!out) {
  62.         fail("can't open seq_ file");
  63.       }

  64.       out.println(i);
  65.     }
  66.   }
  67.   {
  68.     Dir root = SPIFFS.openDir("/");
  69.     while (root.next()) {
  70.       String fileName = root.fileName();
  71.       File f = root.openFile("r");
  72.       Serial.printf("%s: %d\r\n", fileName.c_str(), f.size());
  73.     }
  74.   }

  75.   {
  76.     Dir root = SPIFFS.openDir("/");
  77.     while (root.next()) {
  78.       String fileName = root.fileName();
  79.       Serial.print("deleting ");
  80.       Serial.println(fileName);
  81.       if (!SPIFFS.remove(fileName)) {
  82.         fail("remove failed");
  83.       }
  84.     }
  85.   }

  86.   {
  87.     File tmp = SPIFFS.open("/tmp1.txt", "w");
  88.     tmp.println("rename test");
  89.   }

  90.   {
  91.     if (!SPIFFS.rename("/tmp1.txt", "/tmp2.txt")) {
  92.       fail("rename failed");
  93.     }
  94.     File tmp2 = SPIFFS.open("/tmp2.txt", "r");
  95.     if (!tmp2) {
  96.       fail("open tmp2 failed");
  97.     }
  98.   }

  99.   {
  100.     FSInfo info;
  101.     if (!SPIFFS.info(info)) {
  102.       fail("info failed");
  103.     }
  104.     Serial.printf("Total: %u\nUsed: %u\nBlock: %u\nPage: %u\nMax open files: %u\nMax path len: %u\n",
  105.                   info.totalBytes,
  106.                   info.usedBytes,
  107.                   info.blockSize,
  108.                   info.pageSize,
  109.                   info.maxOpenFiles,
  110.                   info.maxPathLength
  111.                  );
  112.   }

  113.   {
  114.     if (!SPIFFS.format()) {
  115.       fail("format failed");
  116.     }
  117.     Dir root = SPIFFS.openDir("/");
  118.     int count = 0;
  119.     while (root.next()) {
  120.       ++count;
  121.     }
  122.     if (count > 0) {
  123.       fail("some files left after format");
  124.     }
  125.   }
  126.   {
  127.     File tmp = SPIFFS.open("/tmp.txt", "w");
  128.   }
  129.   {
  130.     File tmp = SPIFFS.open("/tmp.txt", "w");
  131.     if (!tmp) {
  132.       fail("failed to re-open empty file");
  133.     }
  134.   }
  135.   Serial.println("success");
  136. }

  137. void loop() {
  138. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|四博智联 Inc. ( 粤ICP备15034758号-1

GMT+8, 2024-4-19 05:23 , Processed in 0.060913 second(s), 33 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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