USBHost example : USBHostMSD, HID mouse and keyboard , support hub.

Dependencies:   USBHOST

Committer:
jamike
Date:
Fri Apr 28 08:03:04 2017 +0000
Revision:
4:2ca820a42fd5
Parent:
3:a2c477c9da16
fix build issue on some platform

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamike 0:0282e73ca55b 1 #include "mbed.h"
jamike 0:0282e73ca55b 2 #include "USBHostMSD.h"
jamike 2:107b7b44bd35 3 #include "USBHostMouse.h"
jamike 2:107b7b44bd35 4 #include "USBHostKeyboard.h"
jamike 2:107b7b44bd35 5 #include "FATFileSystem.h"
jamike 2:107b7b44bd35 6 #include <stdlib.h>
jamike 2:107b7b44bd35 7
jamike 2:107b7b44bd35 8
jamike 0:0282e73ca55b 9 DigitalOut led(LED1);
jamike 2:107b7b44bd35 10 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
jamike 2:107b7b44bd35 11 printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
jamike 2:107b7b44bd35 12 }
jamike 2:107b7b44bd35 13
jamike 2:107b7b44bd35 14 void mouse_task(void const *) {
jamike 2:107b7b44bd35 15
jamike 2:107b7b44bd35 16 USBHostMouse mouse;
jamike 2:107b7b44bd35 17 printf("mouse started\n");
jamike 2:107b7b44bd35 18 while(1) {
jamike 2:107b7b44bd35 19
jamike 2:107b7b44bd35 20 // try to connect a USB mouse
jamike 2:107b7b44bd35 21 while(!mouse.connect())
jamike 2:107b7b44bd35 22 Thread::wait(500);
jamike 2:107b7b44bd35 23
jamike 2:107b7b44bd35 24 // when connected, attach handler called on mouse event
jamike 2:107b7b44bd35 25 mouse.attachEvent(onMouseEvent);
jamike 2:107b7b44bd35 26
jamike 2:107b7b44bd35 27 // wait until the mouse is disconnected
jamike 2:107b7b44bd35 28 while(mouse.connected())
jamike 2:107b7b44bd35 29 Thread::wait(500);
jamike 2:107b7b44bd35 30 printf("mouse seen disconnected\n");
jamike 2:107b7b44bd35 31 }
jamike 2:107b7b44bd35 32 }
jamike 2:107b7b44bd35 33 void onKey(uint8_t key) {
jamike 2:107b7b44bd35 34 printf("Key: %c\r\n", key);
jamike 2:107b7b44bd35 35 }
jamike 2:107b7b44bd35 36 void keyboard_task(void const *) {
jamike 2:107b7b44bd35 37
jamike 2:107b7b44bd35 38 USBHostKeyboard keyboard;
jamike 2:107b7b44bd35 39
jamike 2:107b7b44bd35 40 while(1) {
jamike 2:107b7b44bd35 41 // try to connect a USB keyboard
jamike 2:107b7b44bd35 42 while(!keyboard.connect())
jamike 2:107b7b44bd35 43 Thread::wait(500);
jamike 2:107b7b44bd35 44
jamike 2:107b7b44bd35 45 // when connected, attach handler called on keyboard event
jamike 2:107b7b44bd35 46 keyboard.attach(onKey);
jamike 2:107b7b44bd35 47
jamike 2:107b7b44bd35 48 // wait until the keyboard is disconnected
jamike 2:107b7b44bd35 49 while(keyboard.connected())
jamike 2:107b7b44bd35 50 Thread::wait(500);
jamike 2:107b7b44bd35 51 }
jamike 2:107b7b44bd35 52 }
jamike 2:107b7b44bd35 53
jamike 2:107b7b44bd35 54
jamike 2:107b7b44bd35 55
jamike 0:0282e73ca55b 56 void msd_task(void const *) {
jamike 2:107b7b44bd35 57
jamike 2:107b7b44bd35 58 USBHostMSD msd;
jamike 0:0282e73ca55b 59 int i = 0;
jamike 2:107b7b44bd35 60 FATFileSystem fs("usb");
jamike 2:107b7b44bd35 61 int err;
jamike 0:0282e73ca55b 62 printf("wait for usb memory stick insertion\n");
jamike 0:0282e73ca55b 63 while(1) {
jamike 2:107b7b44bd35 64
jamike 0:0282e73ca55b 65 // try to connect a MSD device
jamike 2:107b7b44bd35 66 while(!msd.connect()) {
jamike 0:0282e73ca55b 67 Thread::wait(500);
jamike 0:0282e73ca55b 68 }
jamike 3:a2c477c9da16 69 if (fs.mount(&msd) != 0) continue;
jamike 2:107b7b44bd35 70 else
jamike 2:107b7b44bd35 71 printf("file system mounted\n");
jamike 2:107b7b44bd35 72
jamike 2:107b7b44bd35 73 if (!msd.connect()) {
jamike 3:a2c477c9da16 74 continue;
jamike 2:107b7b44bd35 75 }
jamike 0:0282e73ca55b 76
jamike 0:0282e73ca55b 77 // in a loop, append a file
jamike 0:0282e73ca55b 78 // if the device is disconnected, we try to connect it again
jamike 0:0282e73ca55b 79
jamike 0:0282e73ca55b 80 // append a file
jamike 2:107b7b44bd35 81 File file;
jamike 2:107b7b44bd35 82 err = file.open(&fs, "test1.txt", O_WRONLY | O_CREAT |O_APPEND);
jamike 0:0282e73ca55b 83
jamike 2:107b7b44bd35 84 if (err == 0) {
jamike 2:107b7b44bd35 85 char tmp[100];
jamike 2:107b7b44bd35 86 sprintf(tmp,"Hello fun USB stick World: %d!\r\n", i++);
jamike 2:107b7b44bd35 87 file.write(tmp,strlen(tmp));
jamike 2:107b7b44bd35 88 sprintf(tmp,"Goodbye World!\r\n");
jamike 2:107b7b44bd35 89 file.write(tmp,strlen(tmp));
jamike 2:107b7b44bd35 90 file.close();
jamike 0:0282e73ca55b 91 } else {
jamike 0:0282e73ca55b 92 printf("FILE == NULL\r\n");
jamike 0:0282e73ca55b 93 }
jamike 0:0282e73ca55b 94 Thread::wait(500);
jamike 0:0282e73ca55b 95 printf("again\n");
jamike 0:0282e73ca55b 96 // if device disconnected, try to connect again
jamike 2:107b7b44bd35 97 while (msd.connected()) {
jamike 0:0282e73ca55b 98 Thread::wait(500);
jamike 0:0282e73ca55b 99 }
jamike 3:a2c477c9da16 100 while (fs.unmount() < 0) {
jamike 2:107b7b44bd35 101 Thread::wait(500);
jamike 2:107b7b44bd35 102 printf("unmount\n");
jamike 2:107b7b44bd35 103 }
jamike 0:0282e73ca55b 104 }
jamike 0:0282e73ca55b 105 }
jamike 0:0282e73ca55b 106
jamike 0:0282e73ca55b 107 int main() {
jamike 0:0282e73ca55b 108 Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
jamike 2:107b7b44bd35 109 Thread mouseTask(mouse_task, NULL, osPriorityNormal, 1024* 4);
jamike 2:107b7b44bd35 110 Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 1024 * 4);
jamike 2:107b7b44bd35 111
jamike 0:0282e73ca55b 112 while(1) {
jamike 0:0282e73ca55b 113 led=!led;
jamike 0:0282e73ca55b 114 Thread::wait(500);
jamike 0:0282e73ca55b 115 }
jamike 0:0282e73ca55b 116 }