Writing data to and from a USB stick

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MSCFileSystem.h"
00003 
00004 MSCFileSystem msc("usb"); // Mount flash drive under the name "msc"
00005 Serial pc(USBTX,USBRX);
00006 
00007 int main() {
00008     unsigned char data[120*160];                // Test array of data
00009 
00010     // Fill the data with values
00011     for (int i=0; i<120*160; i++) {
00012         data[i] = 0xAA;                          // Fill array with data
00013     }
00014 
00015     // Create timers
00016     Timer Write_time, Read_time;
00017     Write_time.start();
00018 
00019     // Write to local file
00020     FILE *fp = fopen( "/usb/test.txt", "w");
00021     for (int i=0; i<120*160; i++) {
00022         fprintf(fp, "%c",data[i]);
00023     }
00024     fclose(fp);
00025 
00026     Write_time.stop();
00027     pc.printf("\n\rTime taken so write array = %f seconds",Write_time.read());
00028 
00029     Read_time.start();
00030     // Read from local file
00031     fopen("/usb/test.txt","r");
00032     // Get data from file and load back into cam
00033     for (int i=0; i<120*160; i++) {
00034         data[i] = fgetc(fp);          // Get the next character
00035     }
00036     fclose(fp);
00037     Read_time.stop();
00038     pc.printf("\n\rTime taken to read array = %f seconds",Read_time.read());
00039 }