
USB BARCODE READER
main.cpp
- Committer:
- jamike
- Date:
- 2017-04-26
- Revision:
- 2:107b7b44bd35
- Parent:
- 1:a12f904713ed
- Child:
- 3:a2c477c9da16
File content as of revision 2:107b7b44bd35:
#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 *) { USBHostMSD msd; int i = 0; FATFileSystem fs("usb"); int err; printf("wait for usb memory stick insertion\n"); while(1) { again: // try to connect a MSD device 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 file; err = file.open(&fs, "test1.txt", O_WRONLY | O_CREAT |O_APPEND); 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()) { Thread::wait(500); } 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); } }