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, committed 2014-03-28
- Comitter:
- thlu
- Date:
- Fri Mar 28 06:08:33 2014 +0000
- Parent:
- 1:03d0f8a4a2d7
- Commit message:
- Fixed filter_noise comparison logic.
Changed in this revision
accelestick.cpp | Show annotated file Show diff for this revision Revisions of this file |
accelestick.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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; } }
--- a/accelestick.h Thu Mar 27 23:55:18 2014 +0000 +++ b/accelestick.h Fri Mar 28 06:08:33 2014 +0000 @@ -14,9 +14,10 @@ #ifndef ACCELESTICK_H #define ACCELESTICK_H -#define SCALE_X 50 // float to int conversion: g value has noise about 0.02 +#define SCALE_X 50 // float to int conversion factor #define SCALE_Y SCALE_X #define SCALE_Z SCALE_X +#define NOISE_FLOOR 0.10*SCALE_X // zero out any integer g value less than NOISE_FLOOR #define ACCEL_MIN -1.5 #define ACCEL_MAX 1.5 @@ -70,8 +71,8 @@ // print debug messages to USB serial void print_debug_msg(); - // detect if accelestick is at rest - void detect_mma_rest(); + // filter out accelestick noise + int16_t filter_noise(int16_t const); // flash specified LED. Needed 2 copies because Ticker cannot attach functions with parameters void flash_led2();