You are viewing an older revision! See the latest version

USBMouse

Beta only!

This library is in beta, and only works with the betamode compiler and the beta libraries.

To use these, ensure you have enabled /betamode for the compiler, and that you import these examples as the basis for your experiments to ensure the beta mbed library is pulled in.

The USBMouse interface is used to emulate a mouse over the USB port.

USB pins are available on p31 (D+) and p32 (D-)

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 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         mouse.move(x, y);
00016         angle += 3;
00017         wait(0.001);
00018     }
00019 }

API

[Not converted]

Details

Here is a video showing the Hello World in action:

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);
   }
 }

USBHID
USBKeyboard
USBMouseKeyboard


All wikipages