; filesystem demonstration snippet ; For VM2 (with Application Board 2 if Memory card used) ; Requires Venom version 2012 10 11 or later. ; ; What the program does ; You can choose whther to use RAM, SD memory card or Flash for your file system by setting ; the FSMDEDIA macro to the approriate value ; ; Each time the program is run, a line of text is added to the file fsdemo.txt ; showing the time and date when the program was run ; When the file gets large enough it gets emptied and renamed, to keep an older copy. ; A series of such 'backup' files is maintained. ; ; ; macros for file system media types #Define RAMFS 0 #Define MMCFS 1 #Define FLASHFS 2 ; choose one of the above #Define FSMEDIA RAMFS #Define RAMDISKSIZE 50 * 1024 To init If FSMEDIA = RAMFS [ Make fs FileSystem("RAM", RAMDISKSIZE) ] Else If FSMEDIA = MMCFS [ Make fs FileSystem("MMC", 13, 0) ] Else [ Make fs FileSystem("FLA") ] Make clock RealTimeClock Make rand RandomNumberGen(0) End ; init To main Print "file system demonstration",CR Print fs Forever [ ; opening a text file, appending a line to it and closing again ; If the file doesn't exist yet, it is automatically created empty when opened Print CR, "opening file", CR f := fs.Open("fsdemo.txt", Char) Print "writing to file", CR ; illustrating print to file Print To f, "fsdemo run at ", clock, CR Print To f, "Example text written to file", CR ; Illustrating Printf with a file f.PrintF ("here are some random numbers: %u %u %u %u\n", rand.Get, rand.Get, rand.Get, rand.Get) ; Close file after we've written to it f.Close ; illustrating some file messages. ; The file grows each time round the loop. When it passes 400 bytes, it is archived ; to a chain of numbered files and a new file started. This is a common technique for limiting ; file sizes by allowing old data to be deleted. Normally you'd have a bigger size limit. ; have to open file to find its length f := fs.Open("fsdemo.txt", Char) fsize := f.Length Print CR, "Contents of current file:",CR, "**** START ****", CR ; Illustrating printing the entire contents of the file as text Print f f.Close Print "file size is ", fsize:1, " bytes", CR If fsize > 400 [ ; illustrating Find, Remove and Name in a sequence to rotate older versions of files Print "rotating files... " If fs.Find("fsdemo3.txt") fs.Remove("fsdemo3.txt") If fs.Find("fsdemo2.txt") fs.Name("fsdemo2.txt", "fsdemo3.txt") If fs.Find("fsdemo1.txt") fs.Name("fsdemo1.txt", "fsdemo2.txt") If fs.Find("fsdemo.txt") fs.Name("fsdemo.txt", "fsdemo1.txt") fs.Flush Print "files rotated", CR ] Print "**** END ****", CR, "File system listing:", CR, fs, CR ; As there is a pause in file activity while we wait for user to press a key, ; this is a good time to makes sure the memory card (if used) is up to date ; in case the user removes it. fs.Flush Print "Press any key to repeat test>" serial.Get ; wait for user to press a key Print CR ] End ; main