USB BARCODE READER
Diff: main.cpp
- Revision:
- 2:107b7b44bd35
- Parent:
- 1:a12f904713ed
- Child:
- 3:a2c477c9da16
--- 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);