Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. #include “SPIFFS.h”
  2. #include “FS.h”
  3. /mode = “r”, “w”, “a”, “r+”, “w+”, “a+” (text file) or “rb”, “wb”, “ab”, “rb+”, “wb+”, “ab+” (binary)/
  4. //void listFilesInDir(File dir, int numTabs = 1);
  5. String command;
  6. int incomingByte = 0;
  7. unsigned int totalBytes = 0;
  8. unsigned int usedBytes = 0;
  9. unsigned int availBytes = 0;
  10. void setup() {
  11. Serial.begin(115200);
  12. Serial.flush();
  13. delay(500);
  14. Serial.println(F(“Inizializing FS…”));
  15. if (SPIFFS.begin()) {
  16. Serial.println(F(“SPIFFS mounted correctly.”));
  17. File dir = SPIFFS.open(“/”);
  18. listDir(SPIFFS, “/”, 0);
  19. // writeFile(SPIFFS, “/hello.txt”, “Hello “);
  20. // appendFile(SPIFFS, “/hello.txt”, “World!\r\n”);
  21. // readFile(SPIFFS, “/hello.txt”);
  22. // renameFile(SPIFFS, “/hello.txt”, “/foo.txt”);
  23. // readFile(SPIFFS, “/foo.txt”);
  24. // deleteFile(SPIFFS, “/foo.txt”);
  25. // testFileIO(SPIFFS, “/test.txt”);
  26. //deleteFile(SPIFFS, “/test.txt”);
  27. //readFile(SPIFFS, “/logos/brightnessup.bmp”);
  28. } else {
  29. Serial.println(F(“!An error occurred during SPIFFS mounting”));
  30. }
  31. }
  32.  
  33. /* ———– MAIN LOOP ———-/
  34. void loop() {
  35. if (Serial.available()) {
  36. command = Serial.readStringUntil(‘\n’);
  37. command.trim();// elimina LF e CR
  38. Serial.println(“Command received ” + command);
  39. if (command.startsWith(“DEL”, 0)) {/ ———- DELETE ——– /
  40. long posizione = command.indexOf(“/”);
  41. if (posizione != -1) {
  42. String nome = command.substring(posizione);
  43. // Serial.println(“>>>” + nome + “<<<“);
  44. int lung = nome.length();
  45. char Buf [lung + 1];
  46. nome.toCharArray(Buf, lung + 1);
  47. // Serial.println(“>>>” + (String) Buf + “<<<“);
  48. deleteFile(SPIFFS, Buf);
  49. }
  50. } else if (command.startsWith(“DIR”, 0)) {/ ———- DIRECTORY ——– /
  51. listDir(SPIFFS, “/”, 0);
  52. } else if (command.startsWith(“REA”, 0)) {/ ———- READ FILE ——– /
  53. long posizione = command.indexOf(“/”);
  54. if (posizione != -1) {
  55. String nome = command.substring(posizione);
  56. int lung = nome.length();
  57. char Buf [lung + 1];
  58. nome.toCharArray(Buf, lung + 1);
  59. readFile(SPIFFS, Buf);
  60. }
  61. } else if (command.startsWith(“REH”, 0)) {/ ———- READ FILE BINARY TO HEX——– /
  62. long posizione = command.indexOf(“/”);
  63. if (posizione != -1) {
  64. String nome = command.substring(posizione);
  65. int lung = nome.length();
  66. char Buf [lung + 1];
  67. nome.toCharArray(Buf, lung + 1);
  68. readFileH(SPIFFS, Buf);
  69. }
  70. } else if (command.startsWith(“CDI”, 0)) {/ ———- CREATE DIRECTORY ——– /
  71. long posizione = command.indexOf(“/”);
  72. if (posizione != -1) {
  73. String nome = command.substring(posizione);
  74. int lung = nome.length();
  75. char Buf [lung + 1];
  76. nome.toCharArray(Buf, lung + 1);
  77. createDir(SPIFFS, Buf);
  78. }
  79. } else if (command.startsWith(“RDI”, 0)) {/ ———- REMOVE DIRECTORY ——– /
  80. long posizione = command.indexOf(“/”);
  81. if (posizione != -1) {
  82. String nome = command.substring(posizione);
  83. int lung = nome.length();
  84. char Buf [lung + 1];
  85. nome.toCharArray(Buf, lung + 1);
  86. removeDir(SPIFFS, Buf);
  87. }
  88. } else if (command.startsWith(“FOR”, 0)) {/ ———- FORMAT——– /
  89. formatto();
  90. } else if (command.startsWith(“WRI”, 0)) {/ ———- WRITE FILE ——– */
  91. long posizione = command.indexOf(“/”);
  92. if (posizione != -1) {
  93. String nome = command.substring(posizione);
  94. int lung = nome.length();
  95. char Buf [lung + 1];
  96. nome.toCharArray(Buf, lung + 1);
  97. writeFile(SPIFFS, Buf, “”);
  98. }
  99. } else {
  100. Serial.println(” Invalid command”);
  101. Comandi();
  102. }
  103. }
  104. }
  105.  
  106. /* ———– END LOOP ———-*/
  107.  
  108. /* ———– LIST DIR ———-/
  109. void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  110. totalBytes = SPIFFS.totalBytes();
  111. usedBytes = SPIFFS.usedBytes();
  112. availBytes = totalBytes – usedBytes;
  113. Serial.println(“===== FILE SYSTEM INFO =====”);
  114. Serial.print(“Total space: “); Serial.print(totalBytes); Serial.println(” byte”);
  115. Serial.print(“Total space used: “); Serial.print(usedBytes); Serial.println(” byte”);
  116. Serial.print(“FREE SPACE : “); Serial.print(availBytes); Serial.println(” byte”);
  117. Serial.println();
  118. Serial.printf(“>>>>> LISTING DIRECTORY: %s\r\n”, dirname);
  119. File root = fs.open(dirname);
  120. if (!root) {
  121. Serial.println(“− failed to open directory”);
  122. return;
  123. }
  124. if (!root.isDirectory()) {
  125. Serial.println(” − not a directory”);
  126. return;
  127. }
  128. File file = root.openNextFile();
  129. while (file) {
  130. if (file.isDirectory()) {
  131. Serial.print(” DIR : “);
  132. Serial.println(file.name());
  133. if (levels) {
  134. listDir(fs, file.name(), levels – 1);
  135. }
  136. } else {
  137. Serial.print(“>>>>> FILE: “);
  138. Serial.print(file.name());
  139. Serial.print(“\tSIZE: “);
  140. Serial.println(file.size());
  141. }
  142. file = root.openNextFile();
  143. }
  144. file.close();
  145. Comandi();
  146. }
  147. / ————————— COMANDI ——————————/
  148. void Comandi() {
  149. Serial.println(“”);
  150. Serial.println(“========== LIST OF COMMANDS ==========”);
  151. Serial.println(“DIR << Directory List”);
  152. Serial.println(“DEL /dir/name << Delete file”);
  153. Serial.println(“REA /dir/name << Read file”);
  154. Serial.println(“REH /dir/name << Read file Binary TO HEX”);
  155. Serial.println(“WRI << Write a file (example: ‘WRI /Pippo/test.txt’ for create a file test.txt into directory Pippo, or just for create a directory)”);
  156. Serial.println(“CDI /dir/name << Create DIR”);
  157. Serial.println(“RDI /dir/name << Remove DIR”);
  158. Serial.println(“FOR << Format SPIFF”);
  159. Serial.println(“======================================”);
  160. }
  161. / ———– READ FILE ———-/
  162. /***************************************************************************
  163. Load configuration data from the SPIFF file, return 0 if OK
  164. SPIFFS open function needs path and mode:
  165. mode = “r”, “w”, “a”, “r+”, “w+”, “a+” (text file) or “rb”, “wb”, “ab”, “rb+”, “wb+”, “ab+” (binary)
  166. where r = read, w = write, a = append
  167. + means file is open for update (read and write)
  168. b means the file os open for binary operations
  169. Returns 0 if OK else:
  170. -1 = No SPIFFS file system
  171. -2 = File does not exist
  172. -3 = File too short
  173. -4 = Checksum does not compare
  174. ***************************************************************************/
  175. void readFile(fs::FS &fs, const char * path) {
  176. size_t len = 0;
  177. size_t i = 0;
  178. Serial.printf(“Reading file: %s\r\n”, path);
  179. File file = fs.open(path, “r”);
  180. file.setTimeout(0);
  181. if (file && !file.isDirectory()) {
  182. len = file.size();
  183. size_t flen = len;
  184. Serial.println(“- reading” );
  185. uint32_t sstart = millis();
  186. uint32_t eend = sstart;
  187. for (i = 0; i < len; i++) {
  188. incomingByte = file.read();
  189. Serial.write(incomingByte);
  190. }
  191. eend = millis() – sstart;
  192. Serial.println(“”);
  193. file.close();
  194. Serial.printf(“- %u bytes read in %u ms\r\n”, flen, eend);
  195. Serial.println(“—– FINISH —–“);
  196. } else {
  197. Serial.println(“- failed to open file for reading”);
  198. }
  199. }
  200. / ———– READ FILE BINARY TO HEX———-/
  201. void readFileH(fs::FS &fs, const char * path) {
  202. size_t len = 0;
  203. size_t i = 0;
  204. Serial.printf(“Reading file: %s\r\n”, path);
  205. File file = fs.open(path, “rb”);
  206. file.setTimeout(0);
  207. if (file && !file.isDirectory()) {
  208. len = file.size();
  209. size_t flen = len;
  210. Serial.println(“- reading” );
  211. uint32_t sstart = millis();
  212. uint32_t eend = sstart;
  213. for (i = 0; i < len; i++) {
  214. incomingByte = file.read();
  215. if ( incomingByte < 16) {/ need put a 0 more or we get a 5 and not a 05 in hex result when byte is from 0 to 15/
  216. Serial.print(“0”);
  217. }
  218. Serial.print(incomingByte, HEX);
  219. }
  220. eend = millis() – sstart;
  221. Serial.println(“”);
  222. file.close();
  223. Serial.printf(“- %u bytes read in %u ms\r\n”, flen, eend);
  224. Serial.println(“—– FINISH —–“);
  225. } else {
  226. Serial.println(“- failed to open file for reading”);
  227. }
  228. }
  229. / ———– WRITE FILE ———-/
  230. void writeFile(fs::FS &fs, const char * path, const char * message) {
  231. Serial.printf(“Writing file: %s\r\n”, path);
  232. File file = fs.open(path, “w”);
  233. delay(2000);
  234. if (!file) {
  235. Serial.println(“− failed to open file for writing”);
  236. return;
  237. }
  238. if (file.print(message)) {
  239. Serial.println(“− file written”);
  240. } else {
  241. Serial.println(“− write failed”);
  242. }
  243. file.close();
  244. }
  245. / ———– APPEND FILE ———-/
  246. void appendFile(fs::FS &fs, const char * path, const char * message) {
  247. Serial.printf(“Appending to file: %s\r\n”, path);
  248. File file = fs.open(path, FILE_APPEND);
  249. if (!file) {
  250. Serial.println(“− failed to open file for appending”);
  251. return;
  252. }
  253. if (file.print(message)) {
  254. Serial.println(“− message appended”);
  255. } else {
  256. Serial.println(“− append failed”);
  257. }
  258. file.close();
  259. }
  260. / ———– RENAME FILE ———-/
  261. void renameFile(fs::FS &fs, const char * path1, const char * path2) {
  262. Serial.printf(“Renaming file %s to %s\r\n”, path1, path2);
  263. if (fs.rename(path1, path2)) {
  264. Serial.println(“− file renamed”);
  265. } else {
  266. Serial.println(“− rename failed”);
  267. }
  268. }
  269. / ———– DELETE FILE ———-/
  270. void deleteFile(fs::FS &fs, const char * path) {
  271. Serial.printf(“Deleting file: %s\r\n”, path);
  272. Serial.println(“>>>” + (String) path + “<<<“);
  273. if (fs.remove(path)) {
  274. Serial.println(“− file deleted”);
  275. } else {
  276. Serial.println(“− delete failed”);
  277. }
  278. }
  279. / ———– FORMAT ———-/
  280. void formatto() {
  281. Serial.println(“Formatto SPIFF”);
  282. SPIFFS.format();
  283. SPIFFS.begin();
  284. Serial.println(“SPIFF FORMATTATO”);
  285. }
  286. / ———– CREATE DIR ———-/
  287. void createDir(fs::FS &fs, const char * path) {
  288. Serial.printf(“Creating Dir: %s\n”, path);
  289. if (fs.mkdir(path)) {
  290. Serial.println(“Dir created”);
  291. } else {
  292. Serial.println(“mkdir failed”);
  293. }
  294. }
  295. / ———– REMOVE DIR ———-/
  296. void removeDir(fs::FS &fs, const char * path) {
  297. Serial.printf(“Removing Dir: %s\n”, path);
  298. if (fs.rmdir(path)) {
  299. Serial.println(“Dir removed”);
  300. } else {
  301. Serial.println(“rmdir failed”);
  302. }
  303. }
  304. /// ———– TEST FILE ———-*/
  305. //void testFileIO(fs::FS &fs, const char * path) {
  306. // Serial.printf(“Testing file I/O with %s\r\n”, path);
  307. // static uint8_t buf[512];
  308. // size_t len = 0;
  309. // File file = fs.open(path, FILE_WRITE);
  310. // if (!file) {
  311. // Serial.println(“- failed to open file for writing”);
  312. // return;
  313. // }
  314. // size_t i;
  315. // Serial.print(“- writing” );
  316. // uint32_t start = millis();
  317. // for (i = 0; i < 512; i++) {
  318. // if ((i & 0x001F) == 0x001F) {
  319. // Serial.print(“.”);
  320. // }
  321. // file.write(buf, 512);
  322. // }
  323. // Serial.println(“”);
  324. // uint32_t end = millis() – start;
  325. // Serial.printf(” – %u bytes written in %u ms\r\n”, 512 * 512, end);
  326. // file.close();
  327. // file = fs.open(path);
  328. // start = millis();
  329. // end = start;
  330. // i = 0;
  331. // if (file && !file.isDirectory()) {
  332. // len = file.size();
  333. // size_t flen = len;
  334. // start = millis();
  335. // Serial.print(“- reading” );
  336. // while (len) {
  337. // size_t toRead = len;
  338. // if (toRead > 512) {
  339. // toRead = 512;
  340. // }
  341. // file.read(buf, toRead);
  342. // if ((i++ & 0x001F) == 0x001F) {
  343. // Serial.print(“.”);
  344. // }
  345. // len -= toRead;
  346. // }
  347. // Serial.println(“”);
  348. // end = millis() – start;
  349. // Serial.printf(“- %u bytes read in %u ms\r\n”, flen, end);
  350. // file.close();
  351. // } else {
  352. // Serial.println(“- failed to open file for reading”);
  353. // }
  354. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement