USB BARCODE READER

Dependencies:   USBHOST

Files at this revision

API Documentation at this revision

Comitter:
jamike
Date:
Wed Apr 26 20:09:08 2017 +0000
Parent:
1:a12f904713ed
Child:
3:a2c477c9da16
Commit message:
update mbed-os lib

Changed in this revision

FATFileSystem.lib Show diff for this revision Revisions of this file
USBHOST.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
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
--- a/FATFileSystem.lib	Fri Feb 17 12:04:00 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/teams/mbed-official/code/FATFileSystem/#e2ab678eb692
--- a/USBHOST.lib	Fri Feb 17 12:04:00 2017 +0000
+++ b/USBHOST.lib	Wed Apr 26 20:09:08 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/ST/code/USBHOST/#1c76b46ad779
+https://developer.mbed.org/teams/ST/code/USBHOST/#d3ac9e1c0035
--- a/main.cpp	Fri Feb 17 12:04:00 2017 +0000
+++ b/main.cpp	Wed Apr 26 20:09:08 2017 +0000
@@ -1,43 +1,115 @@
 #include "mbed.h"
 #include "USBHostMSD.h"
+#include "USBHostMouse.h"
+#include "USBHostKeyboard.h"
+#include "FATFileSystem.h"
+#include <stdlib.h>
+
+
 DigitalOut led(LED1);
+void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
+    printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
+}
+ 
+void mouse_task(void const *) {
+
+    USBHostMouse mouse;
+    printf("mouse started\n");
+    while(1) {
+       
+        // try to connect a USB mouse
+        while(!mouse.connect())
+            Thread::wait(500);
+
+        // when connected, attach handler called on mouse event
+        mouse.attachEvent(onMouseEvent);
+
+        // wait until the mouse is disconnected
+        while(mouse.connected())
+            Thread::wait(500);
+        printf("mouse seen disconnected\n");
+    }
+}
+void onKey(uint8_t key) {
+    printf("Key: %c\r\n", key);
+}
+void keyboard_task(void const *) {
+    
+    USBHostKeyboard keyboard;
+    
+    while(1) {
+        // try to connect a USB keyboard
+        while(!keyboard.connect())
+            Thread::wait(500);
+    
+        // when connected, attach handler called on keyboard event
+        keyboard.attach(onKey);
+        
+        // wait until the keyboard is disconnected
+        while(keyboard.connected())
+            Thread::wait(500);
+    }
+}
+
+
+
 void msd_task(void const *) {
-    printf("init msd\n");
-    USBHostMSD *msd;
+
+    USBHostMSD msd;
     int i = 0;
+    FATFileSystem fs("usb");
+    int err;
     printf("wait for usb memory stick insertion\n");
     while(1) {
-        msd = new USBHostMSD("usb");
+
+again:
         // try to connect a MSD device
-        while(!msd->connect()) {
+        while(!msd.connect()) {
             Thread::wait(500);
         }
+        if (fs.mount(&msd) != 0) goto again;
+        else  
+            printf("file system mounted\n");
+
+        if  (!msd.connect()) {
+            goto again;
+        }
 
         // in a loop, append a file
         // if the device is disconnected, we try to connect it again
 
         // append a file
-        FILE * fp = fopen("/usb/test1.txt", "a");
+        File file;
+        err = file.open(&fs, "test1.txt", O_WRONLY | O_CREAT |O_APPEND);
 
-        if (fp != NULL) {
-            fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++);
-            printf("Goodbye World!\r\n");
-            fclose(fp);
+        if (err == 0) {
+            char tmp[100];
+            sprintf(tmp,"Hello fun USB stick  World: %d!\r\n", i++);
+            file.write(tmp,strlen(tmp));
+            sprintf(tmp,"Goodbye World!\r\n");
+            file.write(tmp,strlen(tmp));
+            file.close();
         } else {
             printf("FILE == NULL\r\n");
         }
         Thread::wait(500);
         printf("again\n");
         // if device disconnected, try to connect again
-        while (msd->connected()) {
+        while (msd.connected()) {
             Thread::wait(500);
         }
-        delete msd;
+        while (fs.unmount()==-1) {
+            Thread::wait(500);
+            printf("unmount\n"); 
+        }
     }
 }
 
 int main() {
     Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
+    Thread mouseTask(mouse_task, NULL, osPriorityNormal, 1024* 4);
+    Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 1024 * 4);
+    
     while(1) {
         led=!led;
         Thread::wait(500);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Wed Apr 26 20:09:08 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#42be5c01a7f91292d5e27124ad9584236025f6ab
--- a/mbed-rtos.lib	Fri Feb 17 12:04:00 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- a/mbed.bld	Fri Feb 17 12:04:00 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/ef9c61f8c49f
\ No newline at end of file