RAMDisk example for the USBFileSystem
Dependencies: mbed USBFileSystem
Fork of USBFileSystem_RAMDISK_HelloWorld by
Diff: main.cpp
- Revision:
- 2:1c8a3c4ba703
- Parent:
- 1:e1b0157ce547
- Child:
- 3:fc1bfb25b644
--- a/main.cpp Tue Jul 30 18:27:18 2013 +0000 +++ b/main.cpp Sun Aug 04 19:59:07 2013 +0000 @@ -1,21 +1,58 @@ +/* +This program is based on Richard Green's RAM_DISK program for the KL25Z + +So all hail the Hypnotoad!!!! + +(And Richard Green, but only after the Hypnotoad) +*/ + + + #include "mbed.h" #include "USBMSD_Ram.h" -USBMSD_Ram sd; -DigitalOut led(LED_GREEN); -Serial pc(USBTX, USBRX); +DigitalOut myled(LED1); +USBMSD_Ram ram; + +void usbCallback(bool available) +{ + if (available) { + FILE *fp = fopen("/USB/IN.txt", "r"); + char buffer[100]; + fgets (buffer, 100, fp); + printf("%s\r\n", buffer); + fclose(fp); + } +} -int main() { - pc.baud(115200); +int main() +{ + ram.attachUSB(&usbCallback); + + wait(0.1); + printf("Hello World!\r\n"); + + //Connect USB + ram.connect(); + ram.usbMode(1); //Disconnect USB when files are locally written + + FILE* fp; + char buffer[101]; + printf("Type your text here! (100 max length)\n"); while(1) { - led = 1; - printf("Connecting!\n"); - if (!sd.connect()) - error("Failed to connect!\n"); - wait(5); - led = 0; - printf("Disconnecting!\n"); - sd.disconnect(); - wait(2); - } -} \ No newline at end of file + gets(buffer); + myled = !myled; + + //Open file for write, keep trying until we succeed + //This is needed because if USB happens to be writing it will fail + while(1) { + fp = fopen("/USB/OUT.txt", "w"); + if (fp != NULL) + break; + } + fprintf(fp, buffer); + fclose(fp); + + } + +}