Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

FILE - Code example
[FILE - Class for file management]

 #include <otSerial.h>
 #include <otTimer.h>
 #include <otMalloc.h>
 #include <otPrintf.h>
 #include <otFILE.h>
 
 otFILE  file;
 
 void setup()
 {
        UartEnableSignals(UART0);
        UartSetBaud(UART0, 115200);
        _initMalloc();
        setStdout(4096, UART0); // printf output is now directed on UART0
        DelayMs(1000);          // Wait terminal start
        
        if(file.init())
        {
                file.setStdout(printf);
                printf("Format SD (Y/N) ?  ");
                char ch = UartGetChar(UART0);
                stdOutLF();
                if(ch == 'y' || ch == 'Y')
                {
                        // Show format progress
                        file.showWriteActivity(true);
                        // Format SD
                        if(!file.format("SD-DISK"))
                                goto error;
                        file.showWriteActivity(false);
                }
                // Display free SD space
                printf("Disk free: %u Kbytes\n", file.free());
        }
        else
        {
 error:
                // Display error message
                printf("STATUS: %s\n", file.status());
                // Unmount and turn off SD
                file.end();
                DelayMs(100);
                HIBE;
        }
 }
 
 void loop()
 {
        // Open a file for write something
        FILE fd = file.fopen("test.txt", otFILE::A_CREATE_ALWAYS | otFILE::A_WRITE);
        if(fd)
        {
                // Write some strings
                for(int i = 0; i < 1000; i++)
                        file.fprintf(fd, "[%4d] Hello world\n", i);
                // Close the file
                file.fclose(fd);
                // Type written file
                file.type("test.txt");
                // Show directory contents
                file.ls("/");
                // UnMount and turn off SD
                file.end();
        }
        printf("End of test\n");
        DelayMs(100);
        IDLE;
 }
footer
otStudio - Library Reference - (C) 2020-23 Officina Turini, All Rights Reserved
Document built with Doxygen 1.4.0