![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is a mouse which uses the integrated accelerometer
Dependencies: MMA8451Q USBDevice mbed
Fork of FRDM_MMA8451Q by
main.cpp@8:c156421f0523, 2014-09-24 (annotated)
- Committer:
- efracb94
- Date:
- Wed Sep 24 22:33:41 2014 +0000
- Revision:
- 8:c156421f0523
- Parent:
- 5:bf5becf7469c
Mouse con acelerometro
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 2:41db78380a6e | 1 | #include "mbed.h" |
efracb94 | 8:c156421f0523 | 2 | #include "USBMouse.h" |
chris | 2:41db78380a6e | 3 | #include "MMA8451Q.h" |
chris | 2:41db78380a6e | 4 | |
chris | 2:41db78380a6e | 5 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
chris | 2:41db78380a6e | 6 | |
efracb94 | 8:c156421f0523 | 7 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
efracb94 | 8:c156421f0523 | 8 | USBMouse mouse; |
efracb94 | 8:c156421f0523 | 9 | DigitalIn b1(PTC7); |
efracb94 | 8:c156421f0523 | 10 | Timer timer; |
efracb94 | 8:c156421f0523 | 11 | |
chris | 4:367de1084ea9 | 12 | int main(void) { |
efracb94 | 8:c156421f0523 | 13 | |
efracb94 | 8:c156421f0523 | 14 | int16_t x = 0; |
efracb94 | 8:c156421f0523 | 15 | int16_t y = 0; |
efracb94 | 8:c156421f0523 | 16 | uint8_t button=0; |
efracb94 | 8:c156421f0523 | 17 | int cswitch=0; |
chris | 4:367de1084ea9 | 18 | |
emilmont | 5:bf5becf7469c | 19 | while (true) { |
efracb94 | 8:c156421f0523 | 20 | |
efracb94 | 8:c156421f0523 | 21 | x=-(acc.getAccX()*10); |
efracb94 | 8:c156421f0523 | 22 | y=acc.getAccY()*10; |
efracb94 | 8:c156421f0523 | 23 | |
efracb94 | 8:c156421f0523 | 24 | if(b1==1) |
efracb94 | 8:c156421f0523 | 25 | { |
efracb94 | 8:c156421f0523 | 26 | button=1; |
efracb94 | 8:c156421f0523 | 27 | mouse.click(button); |
efracb94 | 8:c156421f0523 | 28 | wait(.05); |
efracb94 | 8:c156421f0523 | 29 | } |
efracb94 | 8:c156421f0523 | 30 | else |
efracb94 | 8:c156421f0523 | 31 | {button=0;} |
efracb94 | 8:c156421f0523 | 32 | |
efracb94 | 8:c156421f0523 | 33 | |
efracb94 | 8:c156421f0523 | 34 | if((cswitch==1)&&(x==0)&&(y==0)) |
efracb94 | 8:c156421f0523 | 35 | { |
efracb94 | 8:c156421f0523 | 36 | timer.start(); |
efracb94 | 8:c156421f0523 | 37 | while((cswitch==1)&&(timer.read()<=2)) |
efracb94 | 8:c156421f0523 | 38 | { |
efracb94 | 8:c156421f0523 | 39 | x=-(acc.getAccX()*10); |
efracb94 | 8:c156421f0523 | 40 | y=acc.getAccY()*10; |
efracb94 | 8:c156421f0523 | 41 | if((x!=0)||(y!=0)) |
efracb94 | 8:c156421f0523 | 42 | { |
efracb94 | 8:c156421f0523 | 43 | cswitch=0; |
efracb94 | 8:c156421f0523 | 44 | timer.stop(); |
efracb94 | 8:c156421f0523 | 45 | } |
efracb94 | 8:c156421f0523 | 46 | } |
efracb94 | 8:c156421f0523 | 47 | if(timer.read()>=2) |
efracb94 | 8:c156421f0523 | 48 | { |
efracb94 | 8:c156421f0523 | 49 | mouse.click(1); |
efracb94 | 8:c156421f0523 | 50 | wait(.05); |
efracb94 | 8:c156421f0523 | 51 | mouse.click(1); |
efracb94 | 8:c156421f0523 | 52 | timer.stop(); |
efracb94 | 8:c156421f0523 | 53 | timer.reset(); |
efracb94 | 8:c156421f0523 | 54 | cswitch=0; |
efracb94 | 8:c156421f0523 | 55 | } |
efracb94 | 8:c156421f0523 | 56 | } |
efracb94 | 8:c156421f0523 | 57 | |
efracb94 | 8:c156421f0523 | 58 | if((x!=0)||(y!=0)) |
efracb94 | 8:c156421f0523 | 59 | {cswitch=1;} |
efracb94 | 8:c156421f0523 | 60 | |
efracb94 | 8:c156421f0523 | 61 | x=-(acc.getAccX()*10); |
efracb94 | 8:c156421f0523 | 62 | y=acc.getAccY()*10; |
efracb94 | 8:c156421f0523 | 63 | mouse.move(x, y); |
efracb94 | 8:c156421f0523 | 64 | |
efracb94 | 8:c156421f0523 | 65 | wait(0.005); |
chris | 2:41db78380a6e | 66 | } |
chris | 2:41db78380a6e | 67 | } |