How to auto create filename and index them

23 Mar 2010

Dear all,

I am building a data logger now. I can save all data (analog inputs) to a filename called "temp1.txt" in local file system upon triggering a button.

Say, I need to keep the file and acquire new set of data. How to keep the files in RAM and do not overwrite the same file? I have copied some codes from Igor Skochinsky:

#include "mbed.h"

AnalogIn xin(p20);
AnalogIn yin(p19);

LocalFileSystem local("local");               // Create the local filesystem under the name "local"

int file_no;
char filename[256];

write_file() {
    float xval;
    float yval;
    int loop=0;
    
    sprintf(filename, "/local/temp%d.txt", file_no);    // Open "tem%d.txt" on the local file system for writing
    FILE *fout = fopen(filename, "w");
    if ( fout != NULL ) {
        while(1){
            loop=loop+1;
            xval=xin.read();
            yval=yin.read();
            fprintf(fout, "x: %f, y: %f\r\n", xval, yval);
            if (loop>=10000) break;
        }
        fclose(fout);
        file_no++;
    }
}

int main() {
    file_no = 1;    // start from 1
    button.attach(write_file);
    while(1);    
}
However, there are two problems while I complied it.

1.  Explicity type is missing for the function of write_file. Should that be 'int write_file()'? What is the type of value returned from that function?

2.  For 'button.attach(write_file)', identifier button is undefined. How can I defined the button?

Cheers.

23 Mar 2010

Hi XueTao,

Try

#include "mbed.h"

AnalogIn xin(p20);
AnalogIn yin(p19);
DigitalIn button(p5);

LocalFileSystem local("local");               // Create the local filesystem under the name "local"

int file_no;
char filename[256];

void write_file() {
    float xval;
    float yval;
    int loop=0;
    
    sprintf(filename, "/local/temp%d.txt", file_no);    // Open "tem%d.txt" on the local file system for writing
    FILE *fout = fopen(filename, "w");
    if ( fout != NULL ) {
        while(1){
            loop=loop+1;
            xval=xin.read();
            yval=yin.read();
            fprintf(fout, "x: %f, y: %f\r\n", xval, yval);
            if (loop>=10000) break;
        }
        fclose(fout);
        file_no++;
    }
}

int main() {
    file_no = 1;    // start from 1
    button.attach(write_file);
    while(1);    
}
i.e. 1) void, 2) DigitalIn

Simon

23 Mar 2010

It is still not working. '"Class "mbed::DigitalIn" has no member "attach" (E135)"'

Can I use the any othe pin(p11) rather than p5, which I am gonna use for SD File System?

23 Mar 2010

Ok, here is what you actually want :)

#include "mbed.h"

AnalogIn xin(p20);
AnalogIn yin(p19);
InterruptIn button(p5);

LocalFileSystem local("local");               // Create the local filesystem under the name "local"

int file_no;
char filename[256];

void write_file() {
    float xval;
    float yval;
    int loop=0;
    
    sprintf(filename, "/local/temp%d.txt", file_no);    // Open "tem%d.txt" on the local file system for writing
    FILE *fout = fopen(filename, "w");
    if ( fout != NULL ) {
        while(1){
            loop=loop+1;
            xval=xin.read();
            yval=yin.read();
            fprintf(fout, "x: %f, y: %f\r\n", xval, yval);
            if (loop>=10000) break;
        }
        fclose(fout);
        file_no++;
    }
}

int main() {
    file_no = 1;    // start from 1
    button.rise(write_file);
    while(1);    
}
DigitalIn should be InterruptIn, and the interrupt methods are rise() and fall(). So this will get triggered on a rising edge on p5.

Sorry to have replied too quickly (i.e. with out testing :)

Simon

24 Mar 2010

Thanks for help anyway. It is able to be complied without any error. But there is no file founded in the local file system, do you know where the problem is. I guess that it could be the type of return value of write_file function, what do you think?

24 Mar 2010

I need the same solution.  I have considerd starting a timer.  Then read the timer and convert the time integer to a string.  Then write the filename with the string.  This would give sequential file names based on run time.  If you use a battery backup you could install the real-time clock and then date code the file as well.

I have not tried to code this as I have other functions that are taking priority right now...  Like parsing GPS. :)

24 Mar 2010

Hi,

Looking at what the code you posted is trying to do, it appears it is trying to create incrementing filenames. But the logic has a few bugs. So, here is a quick example showing how you could do that, keeping it simple:

// a way to create incrementing filenames, sford
// every time you hit reset, will create a file with incremental filename

#include "mbed.h"

LocalFileSystem local("local");               // Create the local filesystem under the name "local"

int main() {
    char filename[64];    
    int n = 0;
    
    // set "filename" equal to the next file to write
    while(1) {
        sprintf(filename, "/local/file%03d.txt", n);    // construct the filename fileNNN.txt
        FILE *fp = fopen(filename, "r");                // try and open it
        if(fp == NULL) {                                // if not found, we're done!
            break;
        }
        fclose(fp);                                     // close the file
        n++;                                            // and try the next one
    }
    
    FILE *fp = fopen(filename, "w");
    fprintf(fp, "I am file # %d\n", n);
    fclose(fp);
}
IncrementingFilename

With this code, every time you hit reset you should see a new file appear (after hitting F5 in explorer etc).

Hope this is what you are after to get you started.

Simon

13 Mar 2011

Hello Simon

By using this program I noticed that the file name has a restriction to 8bits. Is there any way to increase that? I am thinking of using the real time clock to name my files and because I am having a trouble on doing it, do you know an easy way to do it?

Thanks

16 Apr 2012

Hey, great post, just what I am looking for, so now on with my code......

I do always run into trouble building a string in C or assigning characters to arrays, but this seems to solve that issue (but not my understanding!)

@Georgios, I do know mbed file names are restricted to 8.3 format and I believe no folders.