GR-PEACH USBHost sample

Dependencies:   USBHost mbed

Committer:
RyoheiHagimoto
Date:
Fri Feb 27 09:30:13 2015 +0000
Revision:
0:9d06c92f4693
GR-PEACH USBHost sample

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RyoheiHagimoto 0:9d06c92f4693 1 // 1:USBHostMouse_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 2 // 2:USBHostKeyboard_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 3 // 3:USBHostMSD_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 4 // 4:USBHostSerial_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 5 // 5:USBHostHub_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 6 #define USB_HOST_TEST 5
RyoheiHagimoto 0:9d06c92f4693 7
RyoheiHagimoto 0:9d06c92f4693 8 #if USB_HOST_TEST == 1
RyoheiHagimoto 0:9d06c92f4693 9 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 10 // 1:USBHostMouse_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 11 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 12 #include "mbed.h"
RyoheiHagimoto 0:9d06c92f4693 13 #include "USBHostMouse.h"
RyoheiHagimoto 0:9d06c92f4693 14
RyoheiHagimoto 0:9d06c92f4693 15 DigitalOut led(LED1);
RyoheiHagimoto 0:9d06c92f4693 16
RyoheiHagimoto 0:9d06c92f4693 17 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
RyoheiHagimoto 0:9d06c92f4693 18 printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
RyoheiHagimoto 0:9d06c92f4693 19 }
RyoheiHagimoto 0:9d06c92f4693 20
RyoheiHagimoto 0:9d06c92f4693 21 void mouse_task(void const *) {
RyoheiHagimoto 0:9d06c92f4693 22
RyoheiHagimoto 0:9d06c92f4693 23 USBHostMouse mouse;
RyoheiHagimoto 0:9d06c92f4693 24
RyoheiHagimoto 0:9d06c92f4693 25 while(1) {
RyoheiHagimoto 0:9d06c92f4693 26 // try to connect a USB mouse
RyoheiHagimoto 0:9d06c92f4693 27 while(!mouse.connect())
RyoheiHagimoto 0:9d06c92f4693 28 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 29
RyoheiHagimoto 0:9d06c92f4693 30 // when connected, attach handler called on mouse event
RyoheiHagimoto 0:9d06c92f4693 31 mouse.attachEvent(onMouseEvent);
RyoheiHagimoto 0:9d06c92f4693 32
RyoheiHagimoto 0:9d06c92f4693 33 // wait until the mouse is disconnected
RyoheiHagimoto 0:9d06c92f4693 34 while(mouse.connected())
RyoheiHagimoto 0:9d06c92f4693 35 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 36 }
RyoheiHagimoto 0:9d06c92f4693 37 }
RyoheiHagimoto 0:9d06c92f4693 38
RyoheiHagimoto 0:9d06c92f4693 39 int main() {
RyoheiHagimoto 0:9d06c92f4693 40 Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
RyoheiHagimoto 0:9d06c92f4693 41 while(1) {
RyoheiHagimoto 0:9d06c92f4693 42 led=!led;
RyoheiHagimoto 0:9d06c92f4693 43 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 44 }
RyoheiHagimoto 0:9d06c92f4693 45 }
RyoheiHagimoto 0:9d06c92f4693 46
RyoheiHagimoto 0:9d06c92f4693 47 #elif USB_HOST_TEST == 2
RyoheiHagimoto 0:9d06c92f4693 48 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 49 // 2:USBHostKeyboard_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 50 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 51 #include "mbed.h"
RyoheiHagimoto 0:9d06c92f4693 52 #include "USBHostKeyboard.h"
RyoheiHagimoto 0:9d06c92f4693 53
RyoheiHagimoto 0:9d06c92f4693 54 DigitalOut led(LED1);
RyoheiHagimoto 0:9d06c92f4693 55
RyoheiHagimoto 0:9d06c92f4693 56 void onKey(uint8_t key) {
RyoheiHagimoto 0:9d06c92f4693 57 printf("Key: %c\r\n", key);
RyoheiHagimoto 0:9d06c92f4693 58 }
RyoheiHagimoto 0:9d06c92f4693 59
RyoheiHagimoto 0:9d06c92f4693 60 void keyboard_task(void const *) {
RyoheiHagimoto 0:9d06c92f4693 61
RyoheiHagimoto 0:9d06c92f4693 62 USBHostKeyboard keyboard;
RyoheiHagimoto 0:9d06c92f4693 63
RyoheiHagimoto 0:9d06c92f4693 64 while(1) {
RyoheiHagimoto 0:9d06c92f4693 65 // try to connect a USB keyboard
RyoheiHagimoto 0:9d06c92f4693 66 while(!keyboard.connect())
RyoheiHagimoto 0:9d06c92f4693 67 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 68
RyoheiHagimoto 0:9d06c92f4693 69 // when connected, attach handler called on keyboard event
RyoheiHagimoto 0:9d06c92f4693 70 keyboard.attach(onKey);
RyoheiHagimoto 0:9d06c92f4693 71
RyoheiHagimoto 0:9d06c92f4693 72 // wait until the keyboard is disconnected
RyoheiHagimoto 0:9d06c92f4693 73 while(keyboard.connected())
RyoheiHagimoto 0:9d06c92f4693 74 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 75 }
RyoheiHagimoto 0:9d06c92f4693 76 }
RyoheiHagimoto 0:9d06c92f4693 77
RyoheiHagimoto 0:9d06c92f4693 78 int main() {
RyoheiHagimoto 0:9d06c92f4693 79 Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4);
RyoheiHagimoto 0:9d06c92f4693 80 while(1) {
RyoheiHagimoto 0:9d06c92f4693 81 led=!led;
RyoheiHagimoto 0:9d06c92f4693 82 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 83 }
RyoheiHagimoto 0:9d06c92f4693 84 }
RyoheiHagimoto 0:9d06c92f4693 85
RyoheiHagimoto 0:9d06c92f4693 86 #elif USB_HOST_TEST == 3
RyoheiHagimoto 0:9d06c92f4693 87 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 88 // 3:USBHostMSD_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 89 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 90 #include "mbed.h"
RyoheiHagimoto 0:9d06c92f4693 91 #include "USBHostMSD.h"
RyoheiHagimoto 0:9d06c92f4693 92
RyoheiHagimoto 0:9d06c92f4693 93 DigitalOut led(LED1);
RyoheiHagimoto 0:9d06c92f4693 94
RyoheiHagimoto 0:9d06c92f4693 95 void msd_task(void const *) {
RyoheiHagimoto 0:9d06c92f4693 96
RyoheiHagimoto 0:9d06c92f4693 97 USBHostMSD msd("usb");
RyoheiHagimoto 0:9d06c92f4693 98 int i = 0;
RyoheiHagimoto 0:9d06c92f4693 99
RyoheiHagimoto 0:9d06c92f4693 100 while(1) {
RyoheiHagimoto 0:9d06c92f4693 101
RyoheiHagimoto 0:9d06c92f4693 102 // try to connect a MSD device
RyoheiHagimoto 0:9d06c92f4693 103 while(!msd.connect()) {
RyoheiHagimoto 0:9d06c92f4693 104 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 105 }
RyoheiHagimoto 0:9d06c92f4693 106
RyoheiHagimoto 0:9d06c92f4693 107 // in a loop, append a file
RyoheiHagimoto 0:9d06c92f4693 108 // if the device is disconnected, we try to connect it again
RyoheiHagimoto 0:9d06c92f4693 109 while(1) {
RyoheiHagimoto 0:9d06c92f4693 110
RyoheiHagimoto 0:9d06c92f4693 111 // append a file
RyoheiHagimoto 0:9d06c92f4693 112 FILE * fp = fopen("/usb/test1.txt", "a");
RyoheiHagimoto 0:9d06c92f4693 113
RyoheiHagimoto 0:9d06c92f4693 114 if (fp != NULL) {
RyoheiHagimoto 0:9d06c92f4693 115 fprintf(fp, "Hello fun USB World: %d!\r\n", i++);
RyoheiHagimoto 0:9d06c92f4693 116 printf("Goodbye World!\r\n");
RyoheiHagimoto 0:9d06c92f4693 117 fclose(fp);
RyoheiHagimoto 0:9d06c92f4693 118 } else {
RyoheiHagimoto 0:9d06c92f4693 119 printf("FILE == NULL\r\n");
RyoheiHagimoto 0:9d06c92f4693 120 }
RyoheiHagimoto 0:9d06c92f4693 121
RyoheiHagimoto 0:9d06c92f4693 122 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 123
RyoheiHagimoto 0:9d06c92f4693 124 // if device disconnected, try to connect again
RyoheiHagimoto 0:9d06c92f4693 125 if (!msd.connected())
RyoheiHagimoto 0:9d06c92f4693 126 break;
RyoheiHagimoto 0:9d06c92f4693 127 }
RyoheiHagimoto 0:9d06c92f4693 128
RyoheiHagimoto 0:9d06c92f4693 129 }
RyoheiHagimoto 0:9d06c92f4693 130 }
RyoheiHagimoto 0:9d06c92f4693 131
RyoheiHagimoto 0:9d06c92f4693 132
RyoheiHagimoto 0:9d06c92f4693 133 int main() {
RyoheiHagimoto 0:9d06c92f4693 134 Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
RyoheiHagimoto 0:9d06c92f4693 135 while(1) {
RyoheiHagimoto 0:9d06c92f4693 136 led=!led;
RyoheiHagimoto 0:9d06c92f4693 137 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 138 }
RyoheiHagimoto 0:9d06c92f4693 139 }
RyoheiHagimoto 0:9d06c92f4693 140
RyoheiHagimoto 0:9d06c92f4693 141 #elif USB_HOST_TEST == 4
RyoheiHagimoto 0:9d06c92f4693 142 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 143 // 4:USBHostSerial_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 144 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 145 #include "mbed.h"
RyoheiHagimoto 0:9d06c92f4693 146 #include "USBHostSerial.h"
RyoheiHagimoto 0:9d06c92f4693 147
RyoheiHagimoto 0:9d06c92f4693 148 DigitalOut led(LED1);
RyoheiHagimoto 0:9d06c92f4693 149 Serial pc(USBTX, USBRX);
RyoheiHagimoto 0:9d06c92f4693 150
RyoheiHagimoto 0:9d06c92f4693 151 void serial_task(void const*) {
RyoheiHagimoto 0:9d06c92f4693 152 USBHostSerial serial;
RyoheiHagimoto 0:9d06c92f4693 153
RyoheiHagimoto 0:9d06c92f4693 154 while(1) {
RyoheiHagimoto 0:9d06c92f4693 155
RyoheiHagimoto 0:9d06c92f4693 156 // try to connect a serial device
RyoheiHagimoto 0:9d06c92f4693 157 while(!serial.connect())
RyoheiHagimoto 0:9d06c92f4693 158 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 159
RyoheiHagimoto 0:9d06c92f4693 160 serial.baud(9600);
RyoheiHagimoto 0:9d06c92f4693 161 // in a loop, print all characters received
RyoheiHagimoto 0:9d06c92f4693 162 // if the device is disconnected, we try to connect it again
RyoheiHagimoto 0:9d06c92f4693 163 while (1) {
RyoheiHagimoto 0:9d06c92f4693 164 // print characters received
RyoheiHagimoto 0:9d06c92f4693 165 while (serial.available()) {
RyoheiHagimoto 0:9d06c92f4693 166 printf("%c", serial.getc());
RyoheiHagimoto 0:9d06c92f4693 167 }
RyoheiHagimoto 0:9d06c92f4693 168
RyoheiHagimoto 0:9d06c92f4693 169 Thread::wait(50);
RyoheiHagimoto 0:9d06c92f4693 170 }
RyoheiHagimoto 0:9d06c92f4693 171 }
RyoheiHagimoto 0:9d06c92f4693 172 }
RyoheiHagimoto 0:9d06c92f4693 173
RyoheiHagimoto 0:9d06c92f4693 174 int main() {
RyoheiHagimoto 0:9d06c92f4693 175 Thread serialTask(serial_task, NULL, osPriorityNormal, 256 * 4);
RyoheiHagimoto 0:9d06c92f4693 176 while(1) {
RyoheiHagimoto 0:9d06c92f4693 177 led=!led;
RyoheiHagimoto 0:9d06c92f4693 178 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 179 }
RyoheiHagimoto 0:9d06c92f4693 180 }
RyoheiHagimoto 0:9d06c92f4693 181 #elif USB_HOST_TEST == 5
RyoheiHagimoto 0:9d06c92f4693 182 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 183 // 5:USBHostHub_HelloWorld
RyoheiHagimoto 0:9d06c92f4693 184 // *******************************************************************
RyoheiHagimoto 0:9d06c92f4693 185 #include "mbed.h"
RyoheiHagimoto 0:9d06c92f4693 186 #include "USBHostKeyboard.h"
RyoheiHagimoto 0:9d06c92f4693 187 #include "USBHostMouse.h"
RyoheiHagimoto 0:9d06c92f4693 188
RyoheiHagimoto 0:9d06c92f4693 189 DigitalOut led(LED1);
RyoheiHagimoto 0:9d06c92f4693 190
RyoheiHagimoto 0:9d06c92f4693 191 void onKey(uint8_t key) {
RyoheiHagimoto 0:9d06c92f4693 192 printf("Key: %c\r\n", key);
RyoheiHagimoto 0:9d06c92f4693 193 }
RyoheiHagimoto 0:9d06c92f4693 194
RyoheiHagimoto 0:9d06c92f4693 195 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
RyoheiHagimoto 0:9d06c92f4693 196 printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
RyoheiHagimoto 0:9d06c92f4693 197 }
RyoheiHagimoto 0:9d06c92f4693 198
RyoheiHagimoto 0:9d06c92f4693 199 void keyboard_task(void const *) {
RyoheiHagimoto 0:9d06c92f4693 200
RyoheiHagimoto 0:9d06c92f4693 201 USBHostKeyboard keyboard;
RyoheiHagimoto 0:9d06c92f4693 202
RyoheiHagimoto 0:9d06c92f4693 203 while(1) {
RyoheiHagimoto 0:9d06c92f4693 204 // try to connect a USB keyboard
RyoheiHagimoto 0:9d06c92f4693 205 while(!keyboard.connect())
RyoheiHagimoto 0:9d06c92f4693 206 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 207
RyoheiHagimoto 0:9d06c92f4693 208 // when connected, attach handler called on keyboard event
RyoheiHagimoto 0:9d06c92f4693 209 keyboard.attach(onKey);
RyoheiHagimoto 0:9d06c92f4693 210
RyoheiHagimoto 0:9d06c92f4693 211 // wait until the keyboard is disconnected
RyoheiHagimoto 0:9d06c92f4693 212 while(keyboard.connected())
RyoheiHagimoto 0:9d06c92f4693 213 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 214 }
RyoheiHagimoto 0:9d06c92f4693 215 }
RyoheiHagimoto 0:9d06c92f4693 216
RyoheiHagimoto 0:9d06c92f4693 217 void mouse_task(void const *) {
RyoheiHagimoto 0:9d06c92f4693 218
RyoheiHagimoto 0:9d06c92f4693 219 USBHostMouse mouse;
RyoheiHagimoto 0:9d06c92f4693 220
RyoheiHagimoto 0:9d06c92f4693 221 while(1) {
RyoheiHagimoto 0:9d06c92f4693 222 // try to connect a USB mouse
RyoheiHagimoto 0:9d06c92f4693 223 while(!mouse.connect())
RyoheiHagimoto 0:9d06c92f4693 224 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 225
RyoheiHagimoto 0:9d06c92f4693 226 // when connected, attach handler called on mouse event
RyoheiHagimoto 0:9d06c92f4693 227 mouse.attachEvent(onMouseEvent);
RyoheiHagimoto 0:9d06c92f4693 228
RyoheiHagimoto 0:9d06c92f4693 229 // wait until the mouse is disconnected
RyoheiHagimoto 0:9d06c92f4693 230 while(mouse.connected())
RyoheiHagimoto 0:9d06c92f4693 231 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 232 }
RyoheiHagimoto 0:9d06c92f4693 233 }
RyoheiHagimoto 0:9d06c92f4693 234
RyoheiHagimoto 0:9d06c92f4693 235 int main() {
RyoheiHagimoto 0:9d06c92f4693 236 Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4);
RyoheiHagimoto 0:9d06c92f4693 237 Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
RyoheiHagimoto 0:9d06c92f4693 238 while(1) {
RyoheiHagimoto 0:9d06c92f4693 239 led=!led;
RyoheiHagimoto 0:9d06c92f4693 240 Thread::wait(500);
RyoheiHagimoto 0:9d06c92f4693 241 }
RyoheiHagimoto 0:9d06c92f4693 242 }
RyoheiHagimoto 0:9d06c92f4693 243
RyoheiHagimoto 0:9d06c92f4693 244 #endif