You are viewing an older revision! See the latest version

USBMouse

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. You can also connect the USB power to VIN to power the mbed when connected.

Hello World

USBMouse Hello World!

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

USBMouse mouse;

int main() {
    int16_t x = 0;
    int16_t y = 0;
    int32_t radius = 10;
    int32_t angle = 0;

    while (1) {
        x = cos((double)angle*3.14/180.0)*radius;
        y = sin((double)angle*3.14/180.0)*radius;
        mouse.move(x, y);
        angle += 3;
        wait(0.001);
    }
}

Import programUSBMouse_HelloWorld

USBMouse Hello World

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