Shop OBEX P1 Docs P2 Docs Learn Events
Automatic/Variable File naming to SD card — Parallax Forums

Automatic/Variable File naming to SD card

TurboManTurboMan Posts: 19
edited 2012-09-04 11:48 in Propeller 1
I'm not a super programmer by any means so be nice. I have searched in the forums for a couple hours and I apparently don't know what key words to use because I came up empty. I currently have created a type of datalogger that is steaming 2channels 1200 Hz binary to an SD card. I'm using Kwabena FAT 16/32 drivers and they work fabulous. I'm probably missing something simple but at minimum, I would like to auto increment the file name such as File1.DAT, File2.DAT...... I can't figure out how to send a string with a variable in it. The call is such... fat0.openFile(string("File.DAT"), "W"). At min. I can use an index funtion to create a numeric counter but I've tried every way I can think of to insert that generated # into the "File###.DAT" with no success.
I'm sure this is a simple task for the seasoned programmer. Help anyone?

TM

Comments

  • TappermanTapperman Posts: 319
    edited 2012-08-27 08:28
    TurboMan wrote: »
    but I've tried every way I can think of to insert that generated # into the "File###.DAT" with no success.
    I'm sure this is a simple task for the seasoned programmer. Help anyone?

    TM

    Try looking in your library for 'simple_numbers' and see if the 'dec' method doesn't help? It may not be exactly what you want, but it should be close!

    ... Tim
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-08-27 09:31
    You could use something like this.
    DAT
      filename byte "FILE0000.DAT", 0
    
    pub GenerateFilename(index) | i, divisor
      divisor := 10000
      repeat i from 4 to 7
        index //= divisor
        divisor /= 10
        byte[@filename][i] := index / divisor + "0"
    
    Call GenerateFilename with your numeric index value, and pass a pointer to filename to the file open routine by using "@filename".
  • TurboManTurboMan Posts: 19
    edited 2012-08-27 09:45
    Hmmm.... I see the dec function in simplenumbers does the conversion to a string but how would I insert the new converted string into the naming convention. Example, the call to create a file is fat0.openFile(string("File.DAT"), "W"). If I used the VAR ntsr that holds the converted string, then the obvious thing would be to send "File","ntsr",".DAT" except we know that "ntsr" is now not the VAR but a string"nstr". I can manually send "File","1",".DAT" and of course that works, but when I try to insert it with say (nstr) that doesn't work either. My question is how do I get the variable into that line and have it still think it is a string?

    BTW Thanks Tim for a response and maybe I missed the answer.
  • TurboManTurboMan Posts: 19
    edited 2012-08-27 09:50
    Yikes, That looks like it might do the trick. I may have been making this wayyy to complicated. (not that I've done that before)

    Thanks Dave.
  • SRLMSRLM Posts: 5,045
    edited 2012-08-27 10:05
    The general method is as follows:

    1. bytemove the prefix of the filename ("FILE")
    2. Use a decimal to string routine to convert the number to a string
    3. Move the string from 2. to the end of the string from 1.
    4. Move the suffix (".DAT") to the end of that.

    In spin, this can be done in at most 9 lines.
  • KyeKye Posts: 2,200
    edited 2012-08-27 17:19
    Here's code that scans too:
    CON _AUTONAME_SIZE = 5 ' Five digits.
    
    DAT AUTONAMEArray byte "00000DT.DAT", _NULL ' File name.
    
    PRI mainAutoName | nameBuffer, nameCounter ' Auto names a file in the current directory.
    
      fat.listEntries("W") ' Goto the top of the directory.
      repeat while(nameBuffer := fat.listEntries("N"))
        result #>= DECIn(nameBuffer) ' Find the largest file name that starts with a number.
    
      nameBuffer := DECOut((1 + (result~)) // 65_536)
      nameCounter := strsize(nameBuffer)
    
      bytemove(@AUTONAMEArray, string("00000"), _AUTONAME_SIZE)
      bytemove((@AUTONAMEArray + (_AUTONAME_SIZE - nameCounter)), nameBuffer, nameCounter)
    
      fat.openFile(fat.newFile(@AUTONAMEArray), "W")
    
    

    The top 5 lines allow you to figure out what the file was with the largest index.
  • TurboManTurboMan Posts: 19
    edited 2012-08-28 09:58
    Thanks Kye,
    I will experiment with the options that everyone has given. It's great to be able to get help like this. BTW The SD FAT code that you created is very nice. I haven't had any issues with the few cards that I have used. Seems stable so far.
    Again Thanks to everyone.

    TM
  • JonnyMacJonnyMac Posts: 9,108
    edited 2012-08-30 10:58
    Yesterday I wrote a little animation recorder that has this bit of code in the start-up section:
    ' look for next file to use
    
      if (AUTO_NAME == YES)                                         ' increment file name?
        repeat filenum from 0 to 99
          make_nfilename(filenum, @VixenFile, 5)                    ' add ## to FileName
          ifnot (open_file(@VixenFile, "r"))                        ' if it doesn't open
            quit                                                    '  next available file# found
          else
            close_file                                              ' close and try next one
    


    It uses this method:
    pub make_nfilename(n, spntr, pos)
    
    '' Adds 2-digit # n to string at spntr
    '' -- pos is position of 10s digit in string
    
      n := 00 #> n <# 99                                            ' limit
    
      byte[spntr][pos+0] := n  / 10 + "0"                           ' convert digits to ASCII
      byte[spntr][pos+1] := n // 10 + "0"
    


    During the program run, this happens when a new record session is initiated:
    if (AUTO_NAME == YES)                                 ' inc filename?
      make_nfilename(filenum++, @VixenFile, 5)            '  yes, make new name
    


    Note the post-increment operator on filenum. Finally, the base file name was store in a dat section like this:
    dat
    
    VixenFile               byte    "vixen00.bin", 0
    


    All this is to say that the suggestions you've been given work in practice; my animation file number auto increments every time I do a recording and when I power back up,it starts at the right place.
  • TurboManTurboMan Posts: 19
    edited 2012-09-04 11:48
    Thanks Jon. I used the examples that Kye and Dave and SRLM posted and was able to create a 5 digit prefix file format "00000GRV.DAT" and have the system up and running. I use a single N.O. pushbutton switch to start data record, stop data record, unload the SD and Restart the SD while it is running and not corrupting the card. A couple LEDs give me full indication of where I'm at in the process. At bootup and incrementally betreen recordings I scan the SD to check where I left off in the file sequence. I used full DuplexSerial to help send messages during debug. It's been fun learning the SD storage process. For whatever reason my biggest hurdle was wrapping my mind around the process of creating the format in the DAT section, using that format to create an array that you can then send as a string. I read the DAT section in the manual several times and kept glossing over the "Quoted strings of characters are also allowed; they are treated as a comma-separated list of characters.". Once I realized that you could enter the array as characters, it fell together. Kye made it real nice to see his method of scanning the files and comparing.
    Thanks to all, I'm past that and on to the next expansion to the device.....
    TM
Sign In or Register to comment.