Advertisement
marcushund

sfs-demo.ino

Jun 20th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. /**
  2.  * This app shows the possibilities of the Simple|Silly File System.
  3.  * The filesystem uses the external flash memory of the Spark-core to store files. it can store 16 files of max 64kB each.
  4.  * I use it in a small webserver app I made; it works stable and is pretty fast.
  5.  * This is the beta version, please informa me about hints and bugs: @marcus
  6.  * enjoy!
  7.  *
  8.  *  
  9.  */
  10.  
  11. #include "sfs.h"
  12.  
  13.  
  14. sfs fs = sfs(); //simple|silly file system
  15. //proto's
  16. String get_file_content(String name,String output); //<< does not work in the online compiler, so code is commented out
  17.  
  18.  
  19. void setup() {
  20.     Serial1.begin(57600); // used to show the results
  21.     char buffer[65];
  22.     char filename[] = "Myfile";
  23.     char filename1[] = "Football";
  24.     int len;
  25.     Serial1.println("SFS Demo");
  26.    // fs.erase_all_files();
  27.     fs.create(filename);
  28.     fs.append("Hello World!!\r\n");
  29.     fs.append("A sparking new world!");
  30.     fs.close();
  31.  
  32.     fs.create(filename1);
  33.     fs.append("World Cup Football:\r\n");
  34.     fs.append("The Netherlands will win it!!");
  35.     fs.close();
  36.  
  37.  
  38.    
  39.     //read a file in chunks: handy when you have to send a large file to a stream.
  40.     fs.open(filename);      
  41.     while (fs.read(buffer, 64)) {
  42.         Serial1.print(buffer); // or choose another output method, or store the data in a variable.
  43.     }
  44.     fs.close();
  45.     Serial1.println("\r\n");
  46.    
  47.     //Get a file PHP style. WARNING: I compile locally, than it works, but in the online environment online it does not...
  48.     String content = get_file_content(filename1,content);
  49.     Serial1.println(content);
  50.  
  51.     //Get de directory of files:
  52.     fs.dir(NULL); //initialize directory functions.    
  53.     char name[44];
  54.     while ((len = fs.dir(name)) > 0) {
  55.         Serial1.print(name);
  56.         Serial1.print("\tfilesize:");
  57.         Serial1.println(len, DEC);
  58.     }
  59. }
  60.  
  61. void loop() {
  62.  
  63. }
  64. /**
  65.  * Dumps a file in a HEX editor like style
  66.  * @param name
  67.  */
  68. void dump_file(char*name) {
  69.     int handle = fs.get_handle(name);
  70.     fs.dump(handle, 256);
  71. }
  72.  
  73. String get_file_content(String name,String output){
  74.     /* This piece works on my local compile, but not in the online editor... have to sort that out.
  75.     unsigned char fn[33];
  76.     char buffer[65];
  77.     name.getBytes(fn,32,0);
  78.     fs.open((char*)fn);
  79.     while (fs.read(buffer, 64)) {
  80.         output+=buffer;
  81.     }
  82.     fs.close();
  83.     return output;
  84.      */
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement