// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // Creating and writing to files // For this to work there must be files on the card with the same // names as the ones used in this example // Because fat-4 shares many words with fat-1,2,3 the SID numbers // should be made the same, in other words fat-4 is an extension // of fat-1,2,3 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // REQUIRES: // #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb" sid=99 // #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb" sid=100 // #URL-lib "http://pin1.org/forthlib/flb/General/SPI.flb" sid=101 // #URL-lib "http://pin1.org/forthlib/flb/SD-Card/MMC.flb" sid=102 // #URL-lib "http://pin1.org/forthlib/flb/General/rtc.flb" sid=103 // #URL-lib "http://pin1.org/forthlib/flb/SD-Card/fat-1.flb" sid=200 // #URL-lib "http://pin1.org/forthlib/flb/SD-Card/fat-2.flb" sid=200 // #URL-lib "http://pin1.org/forthlib/flb/SD-Card/fat-3.flb" sid=200 // #URL-lib "http://pin1.org/forthlib/flb/SD-Card/fat-4.flb" sid=200 // // dump is used for debug and so is included in its entirity // #URL-lib "http://pin1.org/forthlib/flb/General/dump.flb" sid=103 // because f# is a none pulic word // #CurrentSid sid=200 : start // mounts device 0 csel 3 at 4mhz 4000000 spi.speed // 4Mhz card 3 0 mmc.mount ; // display file directory : dir f.ls ; : clr0 0 f.fb 512 0 fill ; // clears buffer to all 0 : clr20 0 f.fb 512 &20 fill ; // clears buffer to all spaces // Use start above to mount card before using any of the following words // create a file called joe.txt : s2 s" joe.txt" 0 f.create -1 <> abort" file creation prblem" ; : s2a s" joe.txt" 0 f.open -1 <> abort" file cant open joe.txt" ; // write random to file and close ** do after s2 or use s2a : s3 clr0 s" This is to sector 0 of joe.txt" 0 f.fb 33 move // move string into file buffer 0 0 f.wrandom // write to card clr0 s" This is to sector 57 of joe.txt" 0 f.fb 33 move 0 57 f.wrandom 0 f.close ; // look at file // NOTE as the buffer has been cleared to 0 beforehand then the // stying will be terminated so stype can be used. : s4 s" joe.txt" 0 f.open if cr ." SECTOR 0 " cr 0 0 f.rrandom 0= if 0 f.fb stype then cr ." SECTOR 57 " cr 0 57 f.rrandom 0= if 0 f.fb stype then else ." cant open file" then ; // append to file : s5 s" joe.txt" 0 f.open if clr0 s" This is to sector 64 of joe.txt" 0 f.fb 33 move 0 64 f.wrandom 0 f.close else ." cant open file" then ; // look again : s6 s" joe.txt" 0 f.open if cr ." SECTOR 0 " cr 0 0 f.rrandom 0= if 0 f.fb stype then cr ." SECTOR 57 " cr 0 57 f.rrandom 0= if 0 f.fb stype then cr ." SECTOR 58 " cr 0 64 f.rrandom 0= if 0 f.fb stype then else ." cant open file" then ; // write 67 sectors to the card : s8 s" joe.txt" 0 f.open 0= abort" Cant open file" 67 for clr0 s" Sector " 0 f.fb 8 move i <# # # #> 0 f.fb 10 + 2 move 0 i f.wrandom i . next 0 f.close ; // write 67 sectors to the card : s9 s" joe.txt" 0 f.open 0= abort" Cant open file" 67 for clr20 // clear buffer by placing spaces s" Sector " 0 f.fb 6 move i <# # # #> 0 f.fb 8 + 2 move 13 0 f.fb 11 + c! // add cr 10 0 f.fb 12 + c! // add lf 0 i f.wrandom i . next 0 f.close ; // delete file : s17 s" joe.txt" f.del ;