GR-PEACH_producer_meeting
/
GR-PEACH_USBHost_sample
GR-PEACH USBHost sample
main.cpp
- Committer:
- RyoheiHagimoto
- Date:
- 2015-02-27
- Revision:
- 1:61a53da3a8bc
- Parent:
- 0:9d06c92f4693
File content as of revision 1:61a53da3a8bc:
// 1:USBHostMouse_HelloWorld // 2:USBHostKeyboard_HelloWorld // 3:USBHostMSD_HelloWorld // 4:USBHostSerial_HelloWorld // 5:USBHostHub_HelloWorld #define USB_HOST_TEST 5 #if USB_HOST_TEST == 1 // ******************************************************************* // 1:USBHostMouse_HelloWorld // ******************************************************************* #include "mbed.h" #include "USBHostMouse.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; 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); } } int main() { Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4); while(1) { led=!led; Thread::wait(500); } } #elif USB_HOST_TEST == 2 // ******************************************************************* // 2:USBHostKeyboard_HelloWorld // ******************************************************************* #include "mbed.h" #include "USBHostKeyboard.h" DigitalOut led(LED1); 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); } } int main() { Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); while(1) { led=!led; Thread::wait(500); } } #elif USB_HOST_TEST == 3 // ******************************************************************* // 3:USBHostMSD_HelloWorld // ******************************************************************* #include "mbed.h" #include "USBHostMSD.h" DigitalOut led(LED1); void msd_task(void const *) { USBHostMSD msd("usb"); int i = 0; while(1) { // try to connect a MSD device while(!msd.connect()) { Thread::wait(500); } // in a loop, append a file // if the device is disconnected, we try to connect it again while(1) { // append a file FILE * fp = fopen("/usb/test1.txt", "a"); if (fp != NULL) { fprintf(fp, "Hello fun USB World: %d!\r\n", i++); printf("Goodbye World!\r\n"); fclose(fp); } else { printf("FILE == NULL\r\n"); } Thread::wait(500); // if device disconnected, try to connect again if (!msd.connected()) break; } } } int main() { Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4); while(1) { led=!led; Thread::wait(500); } } #elif USB_HOST_TEST == 4 // ******************************************************************* // 4:USBHostSerial_HelloWorld // ******************************************************************* #include "mbed.h" #include "USBHostSerial.h" DigitalOut led(LED1); Serial pc(USBTX, USBRX); void serial_task(void const*) { USBHostSerial serial; while(1) { // try to connect a serial device while(!serial.connect()) Thread::wait(500); serial.baud(9600); // in a loop, print all characters received // if the device is disconnected, we try to connect it again while (1) { // print characters received while (serial.available()) { printf("%c", serial.getc()); } Thread::wait(50); } } } int main() { Thread serialTask(serial_task, NULL, osPriorityNormal, 256 * 4); while(1) { led=!led; Thread::wait(500); } } #elif USB_HOST_TEST == 5 // ******************************************************************* // 5:USBHostHub_HelloWorld // ******************************************************************* #include "mbed.h" #include "USBHostKeyboard.h" #include "USBHostMouse.h" DigitalOut led(LED1); void onKey(uint8_t key) { printf("Key: %c\r\n", key); } 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 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 mouse_task(void const *) { USBHostMouse mouse; 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); } } int main() { Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4); while(1) { led=!led; Thread::wait(500); } } #endif