This is a mouse which uses the integrated accelerometer

Dependencies:   MMA8451Q USBDevice mbed

Fork of FRDM_MMA8451Q by mbed official

Files at this revision

API Documentation at this revision

Comitter:
efracb94
Date:
Wed Sep 24 22:33:41 2014 +0000
Parent:
7:70775be9f474
Commit message:
Mouse con acelerometro

Changed in this revision

USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Wed Sep 24 22:33:41 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#5bf05f9b3c7b
--- a/main.cpp	Tue Feb 19 23:46:45 2013 +0000
+++ b/main.cpp	Wed Sep 24 22:33:41 2014 +0000
@@ -1,18 +1,67 @@
 #include "mbed.h"
+#include "USBMouse.h"
 #include "MMA8451Q.h"
 
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
+MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
+USBMouse mouse;
+DigitalIn b1(PTC7);
+Timer timer;
+
 int main(void) {
-    MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
-    PwmOut rled(LED_RED);
-    PwmOut gled(LED_GREEN);
-    PwmOut bled(LED_BLUE);
+    
+    int16_t x = 0;
+    int16_t y = 0;
+    uint8_t button=0;
+    int cswitch=0;
 
     while (true) {
-        rled = 1.0 - abs(acc.getAccX());
-        gled = 1.0 - abs(acc.getAccY());
-        bled = 1.0 - abs(acc.getAccZ());
-        wait(0.1);
+        
+        x=-(acc.getAccX()*10);
+        y=acc.getAccY()*10;
+        
+        if(b1==1)
+        {
+            button=1;
+            mouse.click(button);
+            wait(.05);
+        }
+        else
+        {button=0;}
+        
+       
+        if((cswitch==1)&&(x==0)&&(y==0))
+           {
+                timer.start();
+                while((cswitch==1)&&(timer.read()<=2))
+                {
+                        x=-(acc.getAccX()*10);
+                        y=acc.getAccY()*10;
+                        if((x!=0)||(y!=0))
+                            {
+                                cswitch=0;
+                                timer.stop();
+                            }
+                }
+                if(timer.read()>=2)
+                    {
+                        mouse.click(1);
+                        wait(.05);
+                        mouse.click(1);
+                        timer.stop();
+                        timer.reset();
+                        cswitch=0;
+                    }
+            }
+        
+        if((x!=0)||(y!=0))
+            {cswitch=1;}
+
+        x=-(acc.getAccX()*10);
+        y=acc.getAccY()*10;
+        mouse.move(x, y);
+
+        wait(0.005);
     }
 }