USBMouse

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 "Accelerometer.h"
00004 #include "TouchSensor.h"
00005  
00006 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00007  
00008 USBMouse mouse;
00009 Accelerometer acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
00010 TouchSensor tsi;
00011  
00012 typedef enum {NONE = 0, LEFT, RIGHT} Click;
00013  
00014 int main() {
00015     int16_t x = 0, y = 0;
00016     float t;
00017     Click in_click = NONE;
00018  
00019     while (1) {
00020         t = acc.Acc_X();
00021         t *= 10/1.5;
00022         y = (int16_t) t;
00023         
00024         t = acc.Acc_Y();
00025         t *= 10/1.5;
00026         x = - (int16_t) t;
00027         
00028         t = tsi.readPercentage();
00029         
00030         if (in_click == NONE) {
00031             if (t > 0.6) {
00032                 mouse.press(MOUSE_LEFT);
00033                 in_click = LEFT;
00034             } else if (t > 0.1) {
00035                 mouse.press(MOUSE_RIGHT);
00036                 in_click = RIGHT;
00037             }
00038         } else if (in_click == LEFT) {
00039             if (t <= 0.6) {
00040                 mouse.release(MOUSE_LEFT);
00041                 in_click = NONE;
00042             }
00043         } else {
00044             if (t > 0.6 || t <= 0.1) {
00045                 mouse.release(MOUSE_RIGHT);
00046                 in_click = NONE;
00047             }
00048         }
00049         
00050         mouse.move(x, y);
00051         wait(0.001);
00052     }
00053 }