Sample of using USB memory and SD card on GR-PEACH

Dependencies:   SDBlockDevice_GR_PEACH USBHost_custom

Fork of mbed-os-storage-access by Daiki Kato

main.cpp

Committer:
dkato
Date:
2017-03-16
Revision:
0:392df152119c
Child:
1:c1b5260c1491

File content as of revision 0:392df152119c:

#include "mbed.h"
#include "FATFileSystem.h"
#include "SDBlockDevice_GR_PEACH.h"
#include "USBHostMSD.h"

int main() {
    int i = 0;
    int strage_type = 0;
    char strage_str[16];

    FATFileSystem fs("storage");
    SDBlockDevice_GR_PEACH sd;
    USBHostMSD usb;

    while(1) {
        // try to connect a storage device
        fs.unmount();
        while (1) {
            if (sd.connect()) {
                strage_type = 0; // SD
                strcpy(strage_str, "SD card");
                fs.mount(&sd);
                break;
            }
            if (usb.connect()) {
                strage_type = 1; // USB
                strcpy(strage_str, "USB memory");
                fs.mount(&usb);
                break;
            }
            Thread::wait(500);
        }

        // in a loop, append a file
        // if the device is disconnected, we try to connect it again
        while(1) {
            // append a file
            FILE * fp = fopen("/storage/test1.txt", "a");

            if (fp != NULL) {
                fprintf(fp, "Hello %s World: %d!\r\n", strage_str, i++);
                printf("Goodbye World!\r\n");
                fclose(fp);
            } else {
                printf("FILE == NULL\r\n");
            }
            Thread::wait(500);

            // if device disconnected, try to connect again
            if (strage_type == 0) {
                if (!sd.connected())  break;
            } else {
                if (!usb.connected()) break;
            }
        }
    }
}