Mistake on this page?
Report an issue in GitHub or email us

USBMouse

USBMouse class hierarchy

You can use the USBMouse interface to emulate a mouse over the USB port. You can choose relative or absolute coordinates and send clicks, button state and scroll wheel movements. If you need keyboard or keyboard and mouse functionality, please see the USBKeyboard and USBMouseKeyboard classes.

USBMouse class reference

Public Member Functions
 USBMouse (bool connect_blocking=true, MOUSE_TYPE mouse_type=REL_MOUSE, uint16_t vendor_id=0x1234, uint16_t product_id=0x0001, uint16_t product_release=0x0001)
 Basic constructor. More...
 USBMouse (USBPhy *phy, MOUSE_TYPE mouse_type=REL_MOUSE, uint16_t vendor_id=0x1234, uint16_t product_id=0x0001, uint16_t product_release=0x0001)
 Fully featured constructor. More...
virtual ~USBMouse ()
 Destroy this object. More...
bool update (int16_t x, int16_t y, uint8_t buttons, int8_t z)
 Write a state of the mouse. More...
bool move (int16_t x, int16_t y)
 Move the cursor to (x, y) More...
bool press (uint8_t button)
 Press one or several buttons. More...
bool release (uint8_t button)
 Release one or several buttons. More...
bool double_click ()
 Double click (MOUSE_LEFT) More...
bool click (uint8_t button)
 Click. More...
bool scroll (int8_t z)
 Scrolling. More...
bool ready ()
 Check if this class is ready. More...
void wait_ready ()
 Block until this HID device is in the configured state. More...
bool send (const HID_REPORT *report)
 Send a Report. More...
bool send_nb (const HID_REPORT *report)
 Send a Report. More...
bool read (HID_REPORT *report)
 Read a report: blocking. More...
bool read_nb (HID_REPORT *report)
 Read a report: non blocking. More...
void init ()
 Initialize this instance. More...
void deinit ()
 Power down this instance. More...
bool configured ()
 Check if the device is configured. More...
void connect ()
 Connect a device. More...
void disconnect ()
 Disconnect a device. More...
void sof_enable ()
 Enable the start of frame interrupt. More...
void sof_disable ()
 Disable the start of frame interrupt. More...
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, ep_cb_t callback=NULL)
 Add an endpoint. More...
template<typename T >
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void(T::*callback)())
 Add an endpoint. More...
void endpoint_remove (usb_ep_t endpoint)
 Remove an endpoint. More...
void endpoint_remove_all ()
 Remove all non-zero endpoints. More...
void endpoint_stall (usb_ep_t endpoint)
 Stall an endpoint. More...
void endpoint_unstall (usb_ep_t endpoint)
 Unstall an endpoint. More...
uint32_t endpoint_max_packet_size (usb_ep_t endpoint)
 Get the current maximum size for this endpoint. More...
void endpoint_abort (usb_ep_t endpoint)
 Abort the current transfer on this endpoint. More...
bool read_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 start a read on the given endpoint More...
uint32_t read_finish (usb_ep_t endpoint)
 Get the status of a read. More...
bool write_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 Write a data to the given endpoint. More...
uint32_t write_finish (usb_ep_t endpoint)
 Get the status of a write. More...
Protected Member Functions
virtual void callback_state_change (DeviceState new_state)
 Called when USB changes state. More...
virtual void callback_request (const setup_packet_t *setup)
 Called by USBDevice on Endpoint0 request. More...
virtual void callback_request_xfer_done (const setup_packet_t *setup, bool aborted)
 Called by USBDevice on data stage completion. More...
virtual void callback_power (bool powered)
 Called by USBDevice layer on power state change. More...
virtual void callback_sof (int frame_number)
 Called by USBDevice layer on each new USB frame. More...
virtual void callback_reset ()
 Called by USBDevice layer on bus reset. More...
void complete_request (RequestResult result, uint8_t *data=NULL, uint32_t size=0)
 Called to complete the setup stage of a callback request. More...
void complete_request_xfer_done (bool success)
 Called to complete the data stage of a callback request. More...
void complete_set_configuration (bool success)
 Called to complete a set configuration command. More...
void complete_set_interface (bool success)
 Called to complete a set interface command. More...
uint8_t * find_descriptor (uint8_t descriptor_type, uint8_t index=0)
 Find a descriptor type inside the configuration descriptor. More...
const usb_ep_table_tendpoint_table ()
 Get the endpoint table of this device. More...
virtual void start_process ()
 Callback called to indicate the USB processing needs to be done. More...
virtual void lock ()
 Acquire exclusive access to this instance USBDevice. More...
virtual void unlock ()
 Release exclusive access to this instance USBDevice. More...
virtual void assert_locked ()
 Assert that the current thread of execution holds the lock. More...

USBMouse example

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

//create mouse object
USBMouse mouse;

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

    while (1) {
        //will cause mouse to move in a circle
        x = cos((double)angle*3.14/180.0)*radius;
        y = sin((double)angle*3.14/180.0)*radius;

        //will move mouse x, y away from its previous position on the screen
        mouse.move(x, y);
        angle += 3;
        wait(0.001);
    }
}

You can choose either a relative mouse or an absolute mouse. With a relative mouse, the USBMouse move function will move the mouse x, y pixels away from its previous position on the screen. For an absolute mouse, the USBMouse move function will move the mouse to coordinates x, y on the screen. By default, a USBMouse is a relative mouse. For example, you can use an absolute mouse to draw a circle:

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

//setup mouse to be absolute
USBMouse mouse(true, 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;
  printf("Booted\r\n");

  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;

      //x_screen, y_screen will be the mouse's position on the screen
      mouse.move(x_screen, y_screen);
      angle += 3;
      wait(0.01);
  }
}

USBMouse Joystick example

This example uses a Grove - Thumb Joystick to act as a mouse. Use the joystick to move the mouse around the screen and click the joystick down to do a mouse left click.

//Grove - Thumb Joystick mouse
//Constructor is blocking, so main will not run if the target's USB is not connected
#include "mbed.h"
#include "USBMouse.h"

USBMouse mouse;
// x and y axis of the joystick
AnalogIn   ainx(A0);
AnalogIn   ainy(A1);
int16_t a_inx;
int16_t a_iny;

int main() {
    int16_t x = 0;
    int16_t y = 0;

    while (1) {
        // reads x and y values from the joystick
        a_inx = (int16_t)(ainy.read() * 100.0);
        a_iny = (int16_t)(ainx.read() * 100.0);

        // prints x and y values to serial monitor
        printf("X: %u\r\n", a_inx);
        printf("Y: %u\r\n", a_iny);

        // move position of mouse right
        if(a_inx > 52){
            x = (x - (50 - a_inx)) / 3;
        }
        // move position of mouse left
        else if(a_inx < 47){
            x = (a_inx - 50) / 2;
        }
        // keeps mouse stationary in x-axis
        else{
            x = 0;
        }
        // move position of mouse down
        if(a_iny > 52 && a_iny != 99){
            y = (y - (50 - a_iny)) / 3;
        }
        // move position of mouse up
        else if(a_iny < 47){
            y = (a_iny - 50) / 2;
        }
        // keeps mouse stationary in y-axis
        else{
            y = 0;
        }
        // if button is pressed, a_iny will be 99
        // performs mouse left click
        if(a_iny == 99){
            //click
            mouse.click(MOUSE_LEFT);
            wait(0.4);
        }
        // moves mouse to specified x, y coordinates on screen
        mouse.move(x, y);
        wait(0.001);
    }
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.