Dieter Graef / Mbed 2 deprecated DISCO-F746NG_USB_Host

Dependencies:   USBHost_DISCO-F746NG mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "USBHostMouseKeyboard.h"
00004 #include "USBHostMSD.h"
00005 #define FastSpeedInterface 0
00006 #define HighSpeedInterface 1
00007 DigitalOut led(LED1);
00008 
00009 void onMouseEventdev0(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
00010     printf("0m: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
00011 }
00012 
00013 void onKeydev0(uint8_t key) {
00014     printf("0Key: %c\r\n", key);
00015 }
00016 
00017 
00018 void mouse_task(void const *) {
00019     // At the moment only one Interface can be used for the Host due to the use of
00020     USBHostMouseKb dev0(FastSpeedInterface);
00021     while(1) {
00022         // try to connect
00023         while(!dev0.connect())
00024             Thread::wait(500);
00025         // when connected, attach handler
00026         dev0.attachMouseEvent(onMouseEventdev0);
00027         dev0.attachKb(onKeydev0);
00028         while(1)
00029         {
00030 
00031            USBHost::poll();
00032            Thread::wait(50);
00033         }
00034 
00035     }
00036 }
00037 
00038 void msd_task(void const *) {
00039 
00040     USBHostMSD msd("usb",HighSpeedInterface);
00041     int i = 0;
00042     int res;
00043     int error=0;
00044     while(1) {
00045 
00046         // try to connect a MSD device
00047         while(!msd.connect()) {
00048             Thread::wait(500);
00049             printf("waiting\n");
00050         }
00051 
00052         // in a loop, append a file
00053         // if the device is disconnected, we try to connect it again
00054         while(1) {
00055 
00056             // append a file
00057             FILE * fp = fopen("/usb/test1.txt", "a");
00058 
00059             if (fp != NULL) {
00060                 res=fprintf(fp, "Hello fun USB Drive World: %d!\r\n", i++);
00061                 printf("Goodbye World!\r\n");
00062                 if (error==1)
00063                 {
00064                 printf("Goodbye New World!\r\n");
00065                 error=0;
00066                 }
00067                 fclose(fp);
00068                 if(res<0)
00069                 {
00070                    printf("FILE write ERROR\r\n");
00071                    msd.unmount();
00072                    wait_ms(5000);
00073                    msd.mount() ;
00074                    error=1;
00075                 }
00076 
00077             } else {
00078                 printf("FILE == NULL\r\n");
00079                 msd.unmount();
00080                 wait_ms(5000);
00081                 msd.mount() ;
00082                 error=1;
00083             }
00084 
00085             Thread::wait(500);
00086 
00087             // if device disconnected, try to connect again
00088             if (!msd.connected())
00089                 break;
00090         }
00091 
00092     }
00093 }
00094 
00095 
00096 
00097 int main() {
00098     Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
00099     Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
00100 
00101     while(1) {
00102         led=!led;
00103         Thread::wait(500);
00104     }
00105 }
00106