5 years, 1 month ago.

[PROBLEM] How can i use LocalFileSystem ? (not working)

I try to use LocalFileSystem on LPC1768 but it doesn't work very well. Maybe somebody can try to helps me.

Presentation

Step 1: I try a simple program

#include "mbed.h"

LocalFileSystem local("local");

int main(){
    FILE *fp = fopen("/local/out.txt", "w");
    fprintf(fp,"test\r\n");
    fclose(fp);
}

This one works it generates a file out.txt with "test" printed.

Step 2: I try another program with a while loop

#include "mbed.h"

LocalFileSystem local("local");
DigitalIn button(p12);

int main(){
    FILE *fp = fopen("/local/out.txt", "w");
    while(button.read()!=0){
        fprintf(fp,"test\r\n");
        wait(1);
    }
    fclose(fp);
}

In this one it must print "test" every second util i press a button. But when i press the button the mbed led begin to blinks (indicate an error). When I disconnect and reconnect the mbed the file out.txt was created but nothing is written in it. But what I have found is that a part of the memory has been used.

/media/uploads/AlexandreBurkert/capture.png

Step 3: After this i try to put wrinting step into a function

#include "mbed.h"

LocalFileSystem local("local");

void write(){
    FILE *fp = fopen("/local/out.txt", "w");
    fprintf(fp,"test\r\n");
    fclose(fp);
}

int main(){
    write();
}

This one works it generates a file out.txt with "test" printed.

Step 4: I also try with a while loop and a function

#include "mbed.h"

LocalFileSystem local("local");
DigitalIn button(p12);

void write(){
    FILE *fp = fopen("/local/out.txt", "w");
    fprintf(fp,"test\r\n");
    fclose(fp);
}

int main(){
    while(button.read()!=0){
        write();
        wait(1);
    }
}

But in this one it doesn't work, the error appears before you even press the button.

Objectives

I would like to be able to store values in a file on the mbed

Questions

Is there a bug with the LocalFileSystem in MBED OS 5 ? Is it me who misuses it ? Can't we use it on a loop ? (If this is true, it will become difficult to use it with MBED OS 5)

Thank you for the help you will give me

1 Answer

5 years, 1 month ago.

You need to use append to add data to a file, you can't write the same filename more than once.

This works in OS2 and OS5

#include "mbed.h"
 
LocalFileSystem local("local");
DigitalIn button(p12);


DigitalOut led1(LED1);   // add a bit of debug to see if anything is happening 
DigitalOut led2(LED2);
 
int hits;   // hit counter
 
void write(){
    led2=1;
    FILE *fp = fopen("/local/out.txt", "a");    // use the append to ADD data to the out.txt file
    fprintf(fp,"test %d \r\n", hits);           // add test + hit count to the out.txt file
    fclose(fp);
    led2=0;
}
 
int main(){
    
    button.mode(PullUp);    // pull up the pin if you are using a switch to GND

    while(1){   // keep looping round wating for button press
    
        if(button==0){
            led1=1;
            write();
            while(button==0); // wait until button released
            led1=0;
            wait_ms(10);    // small delay for debounce
            hits++;         // increase hit counter       
        }
    }    
}

Accepted Answer

After one month i tested this program, but i can't see any file named out.txt. Does it do the same thing on your side ?

posted by Alexandre Meyer 26 Apr 2019