Sample program for the USB Host lib with HID

Dependencies:   USBHost_DISCO-F746NG mbed

Files at this revision

API Documentation at this revision

Comitter:
DieterGraef
Date:
Fri Jun 17 09:01:37 2016 +0000
Parent:
1:df8fe4828132
Commit message:
Demo program now uses USB Stick on high speed and HID on fast speed interface. Move your mouse!

Changed in this revision

USBHost_DISCO-F746NG.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r df8fe4828132 -r ca1b5b911ba8 USBHost_DISCO-F746NG.lib
--- a/USBHost_DISCO-F746NG.lib	Mon Jun 13 17:28:04 2016 +0000
+++ b/USBHost_DISCO-F746NG.lib	Fri Jun 17 09:01:37 2016 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/DieterGraef/code/USBHost_DISCO-F746NG/#5396b6a93262
+https://developer.mbed.org/users/DieterGraef/code/USBHost_DISCO-F746NG/#7d6d9fc471bf
diff -r df8fe4828132 -r ca1b5b911ba8 main.cpp
--- a/main.cpp	Mon Jun 13 17:28:04 2016 +0000
+++ b/main.cpp	Fri Jun 17 09:01:37 2016 +0000
@@ -1,43 +1,103 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "USBHostMouseKeyboard.h"
+#include "USBHostMSD.h"
 #define FastSpeedInterface 0
 #define HighSpeedInterface 1
 DigitalOut led(LED1);
 
-void onMouseEventdev(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
-    printf("m: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
+void onMouseEventdev0(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
+    printf("0m: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
 }
 
-void onKeydev(uint8_t key) {
-    printf("Key: %c\r\n", key);
+void onKeydev0(uint8_t key) {
+    printf("0Key: %c\r\n", key);
 }
 
 
 void mouse_task(void const *) {
     // At the moment only one Interface can be used for the Host due to the use of
-    // USBHostMouseKb dev(FastSpeedInterface);
-    USBHostMouseKb dev(HighSpeedInterface);
+    USBHostMouseKb dev0(FastSpeedInterface);
     while(1) {
         // try to connect
-        while(!dev.connect())
+        while(!dev0.connect())
             Thread::wait(500);
-
         // when connected, attach handler
-        dev.attachMouseEvent(onMouseEventdev);
-        dev.attachKb(onKeydev);
-
+        dev0.attachMouseEvent(onMouseEventdev0);
+        dev0.attachKb(onKeydev0);
         while(1)
         {
-           dev.poll();
+
+           USBHost::poll();
            Thread::wait(50);
         }
 
     }
 }
 
+void msd_task(void const *) {
+
+    USBHostMSD msd("usb",HighSpeedInterface);
+    int i = 0;
+    int res;
+    int error=0;
+    while(1) {
+
+        // try to connect a MSD device
+        while(!msd.connect()) {
+            Thread::wait(500);
+            printf("waiting\n");
+        }
+
+        // in a loop, append a file
+        // if the device is disconnected, we try to connect it again
+        while(1) {
+
+            // append a file
+            FILE * fp = fopen("/usb/test1.txt", "a");
+
+            if (fp != NULL) {
+                res=fprintf(fp, "Hello fun USB Drive World: %d!\r\n", i++);
+                printf("Goodbye World!\r\n");
+                if (error==1)
+                {
+                printf("Goodbye New World!\r\n");
+                error=0;
+                }
+                fclose(fp);
+                if(res<0)
+                {
+                   printf("FILE write ERROR\r\n");
+                   msd.unmount();
+                   wait_ms(5000);
+                   msd.mount() ;
+                   error=1;
+                }
+
+            } else {
+                printf("FILE == NULL\r\n");
+                msd.unmount();
+                wait_ms(5000);
+                msd.mount() ;
+                error=1;
+            }
+
+            Thread::wait(500);
+
+            // if device disconnected, try to connect again
+            if (!msd.connected())
+                break;
+        }
+
+    }
+}
+
+
+
 int main() {
     Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
+    Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
+
     while(1) {
         led=!led;
         Thread::wait(500);