Use Accelerometer and Joystick to mimic a mouse. This is for a class project. It is done for educational purpose. It is not practical to real world use.

Dependencies:   C12832_lcd MMA7660 USBDevice mbed

Revision:
2:3d012df30652
Parent:
1:03d0f8a4a2d7
--- a/accelestick.cpp	Thu Mar 27 23:55:18 2014 +0000
+++ b/accelestick.cpp	Fri Mar 28 06:08:33 2014 +0000
@@ -17,7 +17,7 @@
 //     3. push and hold left joystick for 1 second to initiate left mouse button
 //        press and hold feature. LED1 will lit up. Push left joystick again to
 //        release
-//     4. press joystick center position down twice to initiate accelestick 
+//     4. press joystick center position down twice to initiate accelestick
 //          calibration. This will reduce cursor movement at rest position. LED3
 //          will flash rapidly and then remain lit when done
 //     5. push right joystick twice to enable debug mode printing. Debug messages
@@ -225,10 +225,9 @@
     if (calib_on) {
         calib_mma(current);
     }
-    mouse_info.x = current.x - offset.x;
-    mouse_info.y = current.y - offset.y;
-    mouse_info.z = current.z - offset.z;
-    detect_mma_rest();
+    mouse_info.x = filter_noise(current.x - offset.x);
+    mouse_info.y = filter_noise(current.y - offset.y);
+    mouse_info.z = filter_noise(current.z - offset.z);
 }
 
 void update_mouse()
@@ -329,14 +328,15 @@
     }
 }
 
-// use calibrated mouse x,y,z values to detect rest
-void detect_mma_rest ()
+//
+int16_t filter_noise (int16_t const num)
 {
-    if ((abs(mouse_info.z) <= 0.06*SCALE_Z) &&
-            (abs(mouse_info.x) <= 0.10*SCALE_X) &&
-            (abs(mouse_info.y) <= 0.10*SCALE_Y)) {
-        mouse_info.x = 0;
-        mouse_info.y = 0;
+    if (abs(num) < NOISE_FLOOR) {
+        return 0;
+    } else if (num < 0) {
+        return num + NOISE_FLOOR;
+    } else {
+        return num - NOISE_FLOOR;
     }
 }