Example of usb mouse on smt32 platform using a analog 2-axis joystick

Dependencies:   USBDEVICE mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBMouse.h"
00003 #include <math.h>
00004 
00005 USBMouse mouse(ABS_MOUSE);
00006 DigitalOut myled(LED1);
00007 
00008 AnalogIn ex(PC_2);
00009 AnalogIn ey(PC_3);
00010 DigitalIn click(PB_7, PullUp);
00011 
00012 int main(void) {
00013     unsigned int x_center = (X_MAX_ABS - X_MIN_ABS)/2;
00014     unsigned int y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
00015     uint16_t x_screen = x_center;
00016     uint16_t y_screen = y_center;
00017     uint16_t px;
00018     uint16_t py;
00019     bool sw = 0;
00020     bool swOld = 0;
00021     myled = 0;
00022 
00023     while (1) {
00024         myled = !myled;
00025         px = ex.read_u16();
00026         py = ey.read_u16();
00027         sw = !click.read();
00028         
00029         if(px > 40000 || px < 25000) {
00030             x_screen += (px - 32768)/250;
00031         }
00032         if(py > 40000 || py < 25000) {
00033             y_screen += (py - 32768)/250;
00034         }         
00035         mouse.move(x_screen, y_screen);
00036         
00037         if(!swOld && sw) {
00038             mouse.update(x_screen, y_screen,1,0);
00039             swOld = 1;
00040         } else if (swOld && !sw){
00041             swOld = 0;
00042             mouse.update(x_screen, y_screen,0,0);
00043         }
00044         
00045         wait(0.007);
00046     }
00047 }