Guide
1/12/2018 mbed Starter Kit Experiment Guide - learn.sparkfun.com
https://learn.sparkfun.com/tutorials/mbed-starter-kit-experiment-guide/all 42/65
In order to make the mbed act like a USB mouse for this walkthrough, we will rely on mbed’s USBDevice library. This library contains all the necessary functions
to enumerate as a USB device to a computer and function as a mouse.
Libraries
Navigate to the mbed.org, login, and navigate to your Compiler.
Create a new program with the “Blinky LED Hello World” template. Name it something like “usb_device.”
Navigate to the following pages and import each library into your “usb_device” program.
USBDevice
The mbed library should already be imported if you used the “Blinky” template.
Program
Click on “main.cpp” in your project, remove the template code, and copy in the following code.
// USB Device demo - control mouse pointer with buttons
#include "mbed.h"
#include "USBMouse.h"
// USB Mouse object
USBMouse mouse;
// Define buttons
DigitalIn button_up(p5);
DigitalIn button_down(p6);
DigitalIn button_left(p7);
DigitalIn button_right(p8);
DigitalOut myled(LED1);
int main() {
int x = 0;
int y = 0;
while (1) {
// Determine mouse pointer horizontal direction
x = button_left ^ button_right;
if ( button_right ) {
x = -1 * x;
}
// Determine mouse pointer vertical direction
y = button_up ^ button_down;
if ( button_down ) {
y = -1 * y;
}
// Move mouse
mouse.move(x, y);
// Wait for next cycle
wait(0.001);
}
}
Run