Table of Contents

  1. Hello World
  2. API
  3. Related

mbed OS 2

This is the handbook for mbed OS 2.

The USBHostMouse interface is used to communicate with a USB mouse.

Library in Beta!

This library is in Beta. If you have any problems using the USBHost library, please send a bug report at

The USB Host connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768
  • add two 15k resistors tied to GND on D+ and D-

Hello World

Import program

00001 #include "mbed.h"
00002 #include "USBHostMouse.h"
00004 DigitalOut led(LED1);
00006 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
00007     printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
00008 }
00010 void mouse_task(void const *) {
00012     USBHostMouse mouse;
00014     while(1) {
00015         // try to connect a USB mouse
00016         while(!mouse.connect())
00017             Thread::wait(500);
00019         // when connected, attach handler called on mouse event
00020         mouse.attachEvent(onMouseEvent);
00022         // wait until the mouse is disconnected
00023         while(mouse.connected())
00024             Thread::wait(500);
00025     }
00026 }
00028 int main() {
00029     Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
00030     while(1) {
00031         led=!led;
00032         Thread::wait(500);
00033     }
00034 }


Import library

Public Member Functions

  USBHostMouse ()
bool  connect ()
  Try to connect a mouse device.
bool  connected ()
  Check if a mouse is connected.
void  attachEvent (void(*ptr)(uint8_t buttons, int8_t x, int8_t y, int8_t z))
  Attach a callback called when a mouse event is received.
void  attachButtonEvent (void(*ptr)(uint8_t buttons))
  Attach a callback called when the button state changes.
void  attachXEvent (void(*ptr)(int8_t x))
  Attach a callback called when the X axis value changes.
void  attachYEvent (void(*ptr)(int8_t y))
  Attach a callback called when the Y axis value changes.
void  attachZEvent (void(*ptr)(int8_t z))
  Attach a callback called when the Z axis value changes (scrolling)


If your mbed board is automatically resetted when you plug a USB device, you should consider to use an external power supply