![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Writing data to and from a USB stick
main.cpp@0:5934350323b2, 2011-11-06 (annotated)
- Committer:
- ms523
- Date:
- Sun Nov 06 15:45:12 2011 +0000
- Revision:
- 0:5934350323b2
Working with a USB stick
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ms523 | 0:5934350323b2 | 1 | #include "mbed.h" |
ms523 | 0:5934350323b2 | 2 | #include "MSCFileSystem.h" |
ms523 | 0:5934350323b2 | 3 | |
ms523 | 0:5934350323b2 | 4 | MSCFileSystem msc("usb"); // Mount flash drive under the name "msc" |
ms523 | 0:5934350323b2 | 5 | Serial pc(USBTX,USBRX); |
ms523 | 0:5934350323b2 | 6 | |
ms523 | 0:5934350323b2 | 7 | int main() { |
ms523 | 0:5934350323b2 | 8 | unsigned char data[120*160]; // Test array of data |
ms523 | 0:5934350323b2 | 9 | |
ms523 | 0:5934350323b2 | 10 | // Fill the data with values |
ms523 | 0:5934350323b2 | 11 | for (int i=0; i<120*160; i++) { |
ms523 | 0:5934350323b2 | 12 | data[i] = 0xAA; // Fill array with data |
ms523 | 0:5934350323b2 | 13 | } |
ms523 | 0:5934350323b2 | 14 | |
ms523 | 0:5934350323b2 | 15 | // Create timers |
ms523 | 0:5934350323b2 | 16 | Timer Write_time, Read_time; |
ms523 | 0:5934350323b2 | 17 | Write_time.start(); |
ms523 | 0:5934350323b2 | 18 | |
ms523 | 0:5934350323b2 | 19 | // Write to local file |
ms523 | 0:5934350323b2 | 20 | FILE *fp = fopen( "/usb/test.txt", "w"); |
ms523 | 0:5934350323b2 | 21 | for (int i=0; i<120*160; i++) { |
ms523 | 0:5934350323b2 | 22 | fprintf(fp, "%c",data[i]); |
ms523 | 0:5934350323b2 | 23 | } |
ms523 | 0:5934350323b2 | 24 | fclose(fp); |
ms523 | 0:5934350323b2 | 25 | |
ms523 | 0:5934350323b2 | 26 | Write_time.stop(); |
ms523 | 0:5934350323b2 | 27 | pc.printf("\n\rTime taken so write array = %f seconds",Write_time.read()); |
ms523 | 0:5934350323b2 | 28 | |
ms523 | 0:5934350323b2 | 29 | Read_time.start(); |
ms523 | 0:5934350323b2 | 30 | // Read from local file |
ms523 | 0:5934350323b2 | 31 | fopen("/usb/test.txt","r"); |
ms523 | 0:5934350323b2 | 32 | // Get data from file and load back into cam |
ms523 | 0:5934350323b2 | 33 | for (int i=0; i<120*160; i++) { |
ms523 | 0:5934350323b2 | 34 | data[i] = fgetc(fp); // Get the next character |
ms523 | 0:5934350323b2 | 35 | } |
ms523 | 0:5934350323b2 | 36 | fclose(fp); |
ms523 | 0:5934350323b2 | 37 | Read_time.stop(); |
ms523 | 0:5934350323b2 | 38 | pc.printf("\n\rTime taken to read array = %f seconds",Read_time.read()); |
ms523 | 0:5934350323b2 | 39 | } |