![](/media/cache/profiles/3ec0e2c7f4536e7ce9e9ce183b1de9e8.jpg.50x50_q85.jpg)
RAMDisk example for the USBFileSystem
Dependencies: mbed USBFileSystem
Fork of USBFileSystem_RAMDISK_HelloWorld by
main.cpp@2:1c8a3c4ba703, 2013-08-04 (annotated)
- Committer:
- Sissors
- Date:
- Sun Aug 04 19:59:07 2013 +0000
- Revision:
- 2:1c8a3c4ba703
- Parent:
- 1:e1b0157ce547
- Child:
- 3:fc1bfb25b644
v1.0
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 2:1c8a3c4ba703 | 1 | /* |
Sissors | 2:1c8a3c4ba703 | 2 | This program is based on Richard Green's RAM_DISK program for the KL25Z |
Sissors | 2:1c8a3c4ba703 | 3 | |
Sissors | 2:1c8a3c4ba703 | 4 | So all hail the Hypnotoad!!!! |
Sissors | 2:1c8a3c4ba703 | 5 | |
Sissors | 2:1c8a3c4ba703 | 6 | (And Richard Green, but only after the Hypnotoad) |
Sissors | 2:1c8a3c4ba703 | 7 | */ |
Sissors | 2:1c8a3c4ba703 | 8 | |
Sissors | 2:1c8a3c4ba703 | 9 | |
Sissors | 2:1c8a3c4ba703 | 10 | |
ragmondo | 0:fc0ff7c1656b | 11 | #include "mbed.h" |
ragmondo | 0:fc0ff7c1656b | 12 | #include "USBMSD_Ram.h" |
ragmondo | 0:fc0ff7c1656b | 13 | |
Sissors | 2:1c8a3c4ba703 | 14 | DigitalOut myled(LED1); |
Sissors | 2:1c8a3c4ba703 | 15 | USBMSD_Ram ram; |
Sissors | 2:1c8a3c4ba703 | 16 | |
Sissors | 2:1c8a3c4ba703 | 17 | void usbCallback(bool available) |
Sissors | 2:1c8a3c4ba703 | 18 | { |
Sissors | 2:1c8a3c4ba703 | 19 | if (available) { |
Sissors | 2:1c8a3c4ba703 | 20 | FILE *fp = fopen("/USB/IN.txt", "r"); |
Sissors | 2:1c8a3c4ba703 | 21 | char buffer[100]; |
Sissors | 2:1c8a3c4ba703 | 22 | fgets (buffer, 100, fp); |
Sissors | 2:1c8a3c4ba703 | 23 | printf("%s\r\n", buffer); |
Sissors | 2:1c8a3c4ba703 | 24 | fclose(fp); |
Sissors | 2:1c8a3c4ba703 | 25 | } |
Sissors | 2:1c8a3c4ba703 | 26 | } |
ragmondo | 0:fc0ff7c1656b | 27 | |
Sissors | 2:1c8a3c4ba703 | 28 | int main() |
Sissors | 2:1c8a3c4ba703 | 29 | { |
Sissors | 2:1c8a3c4ba703 | 30 | ram.attachUSB(&usbCallback); |
Sissors | 2:1c8a3c4ba703 | 31 | |
Sissors | 2:1c8a3c4ba703 | 32 | wait(0.1); |
Sissors | 2:1c8a3c4ba703 | 33 | printf("Hello World!\r\n"); |
Sissors | 2:1c8a3c4ba703 | 34 | |
Sissors | 2:1c8a3c4ba703 | 35 | //Connect USB |
Sissors | 2:1c8a3c4ba703 | 36 | ram.connect(); |
Sissors | 2:1c8a3c4ba703 | 37 | ram.usbMode(1); //Disconnect USB when files are locally written |
Sissors | 2:1c8a3c4ba703 | 38 | |
Sissors | 2:1c8a3c4ba703 | 39 | FILE* fp; |
Sissors | 2:1c8a3c4ba703 | 40 | char buffer[101]; |
Sissors | 2:1c8a3c4ba703 | 41 | printf("Type your text here! (100 max length)\n"); |
Sissors | 1:e1b0157ce547 | 42 | while(1) { |
Sissors | 2:1c8a3c4ba703 | 43 | gets(buffer); |
Sissors | 2:1c8a3c4ba703 | 44 | myled = !myled; |
Sissors | 2:1c8a3c4ba703 | 45 | |
Sissors | 2:1c8a3c4ba703 | 46 | //Open file for write, keep trying until we succeed |
Sissors | 2:1c8a3c4ba703 | 47 | //This is needed because if USB happens to be writing it will fail |
Sissors | 2:1c8a3c4ba703 | 48 | while(1) { |
Sissors | 2:1c8a3c4ba703 | 49 | fp = fopen("/USB/OUT.txt", "w"); |
Sissors | 2:1c8a3c4ba703 | 50 | if (fp != NULL) |
Sissors | 2:1c8a3c4ba703 | 51 | break; |
Sissors | 2:1c8a3c4ba703 | 52 | } |
Sissors | 2:1c8a3c4ba703 | 53 | fprintf(fp, buffer); |
Sissors | 2:1c8a3c4ba703 | 54 | fclose(fp); |
Sissors | 2:1c8a3c4ba703 | 55 | |
Sissors | 2:1c8a3c4ba703 | 56 | } |
Sissors | 2:1c8a3c4ba703 | 57 | |
Sissors | 2:1c8a3c4ba703 | 58 | } |