USBMouse

This content relates to a deprecated version of Mbed

Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.

The USBMouse interface is used to emulate a mouse over the USB port. You can choose relative or absolute co-ordinates, and send clicks, button state and scroll wheel movements.

The USB connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Hello World

Import program

00001 #include "mbed.h"
00002 #include "USBMouse.h"
00003 
00004 USBMouse mouse;
00005 
00006 int main() {
00007     int16_t x = 0;
00008     int16_t y = 0;
00009     int32_t radius = 10;
00010     int32_t angle = 0;
00011 
00012     while (1) {
00013         x = cos((double)angle*3.14/180.0)*radius;
00014         y = sin((double)angle*3.14/180.0)*radius;
00015         
00016         mouse.move(x, y);
00017         angle += 3;
00018         wait(0.001);
00019     }
00020 }

API

Import library

Public Member Functions

USBMouse (MOUSE_TYPE mouse_type=REL_MOUSE, uint16_t vendor_id=0x1234, uint16_t product_id=0x0001, uint16_t product_release=0x0001)
Constructor.
bool update (int16_t x, int16_t y, uint8_t buttons, int8_t z)
Write a state of the mouse.
bool move (int16_t x, int16_t y)
Move the cursor to (x, y)
bool press (uint8_t button)
Press one or several buttons.
bool release (uint8_t button)
Release one or several buttons.
bool doubleClick ()
Double click (MOUSE_LEFT)
bool click (uint8_t button)
Click.
bool scroll (int8_t z)
Scrolling.
bool send (HID_REPORT *report)
Send a Report.
bool sendNB (HID_REPORT *report)
Send a Report.
bool read (HID_REPORT *report)
Read a report: blocking.
bool readNB (HID_REPORT *report)
Read a report: non blocking.

Details

You can choose either a relative mouse or an absolute mouse. By default, a USBMouse is a relative mouse. For instance, you can use an absolute mouse to draw a circle:

 #include "mbed.h"
 #include "USBMouse.h"

 USBMouse mouse(ABS_MOUSE);

 #include <math.h>

 int main(void)
 {
   uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
   uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
   uint16_t x_screen = 0;
   uint16_t y_screen = 0;
   
   uint32_t x_origin = x_center;
   uint32_t y_origin = y_center;
   uint32_t radius = 5000;
   uint32_t angle = 0;

   while (1)
   {
       x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
       y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
       
       mouse.move(x_screen, y_screen);
       angle += 3;
       wait(0.01);
   }
 }

All wikipages