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:
1:03d0f8a4a2d7
Parent:
0:22bdcdc386df
Child:
2:3d012df30652
--- a/accelestick.h	Sat Mar 15 09:32:43 2014 +0000
+++ b/accelestick.h	Thu Mar 27 23:55:18 2014 +0000
@@ -1,57 +1,49 @@
+// Name: T.H. Lu
+// Date: 03/27/2014
+// Description:
+//   Header file for accelestick. Contains all defines and function declarations.
+//
+
 #include <stdlib.h>
 #include "mbed.h"
 
-#ifndef ACCELESTICK_H
-#define ACCELESTICK_H
+#include "MMA7660.h"         // Accelerometer API
+#include "C12832_lcd.h"      // LCD API
+#include "USBMouseKeyboard.h"// USB Mouse API
 
-#include "MMA7660.h"     // Accelerometer API
-#include "C12832_lcd.h"  // LCD API
-#include "USBMouseKeyboard.h"
+#ifndef ACCELESTICK_H
+  #define ACCELESTICK_H
  
-#define SCALE_X 50        // g value has noise about 0.05
+#define SCALE_X 50       // float to int conversion: g value has noise about 0.02
 #define SCALE_Y SCALE_X 
 #define SCALE_Z SCALE_X
+
 #define ACCEL_MIN -1.5
 #define ACCEL_MAX 1.5
 
 #define CALIB_SMPLS 16   // number of samples used for calibrating accelerometer
 
 // Joystick button values based on joyb declaration
-#define JS_NONE 0
-#define JS_DOWN 1
-#define JS_LEFT 2
-#define JS_CENTER 4
-#define JS_UP 8
-#define JS_RIGHT 16
-
-//--- Global Variables declaration ---//
+enum {JS_NONE=0, JS_DOWN=1, JS_LEFT=2, JS_CENTER=4, JS_UP=8, JS_RIGHT=16};
+enum {LED_LOCK=0, LED_DEBUG, LED_CALIB, LED_ALIVE};
 
-C12832_LCD lcd;         // for debugging
-MMA7660 mma(p28, p27);  // I2C Accelerometer
-USBMouseKeyboard mouse;
-DigitalOut led_alive(LED4);
-DigitalOut leds[] = {LED1, LED2, LED3};
-
-DigitalIn jd = p12;
-DigitalIn jl = p13;
-DigitalIn jc = p14;
-DigitalIn ju = p15;
-DigitalIn jr = p16;
-BusIn joyb(p12, p13, p14, p15, p16);
-
+// structure to store max and min g values for debugging
 typedef struct s_max_min {
     float max_x, max_y, max_z;
     float min_x, min_y, min_z;
 } Max_min_t;
 
+// structure for accelerometer int g values
 typedef struct s_int_g {
     int16_t x, y, z;
 } G_int_t;
 
+// structure for accelerometer float g values
 typedef struct s_float_pos {
     float x, y, z;
 } G_float_t;
 
+// structure to store all mouse related data
 typedef struct 
 {
     int16_t x, y, z;  // x, y, z position
@@ -60,41 +52,35 @@
     int8_t scroll;    // <0 to go up, >0 to go down
 } Mouse_state_t;
 
-Max_min_t peaks;
-Mouse_state_t mouse_info;
-G_int_t offset;       // calibration offset value
-
-bool calib_on = 0;
-bool debug_on = 0;
-uint8_t tm_joystick_running = 0, debug_sel;
-
-G_float_t mma_g;      // g values from accelerometer
-
-Ticker tk_led_alive;  // Ticker for flashing LED as program alive beacon
-Ticker tk_print_debug;// Ticker for printing debug information
-Timer tm_joystick_dc; // Timer for joystick double-click detection
-RawSerial debug(USBTX, USBRX);
-
-// End of Global Variable Declaration
-
 
 //--- Functions Declaration ---//
 
+  // scan joystick for mouse buttons and scroll
+  void get_joystick_input();
+  
+  // sample accelerometer values for mouse movement
+  void sample_mma();
+
   // calibrate accelerometer
   void calib_mma(G_int_t current);
   
+  // update mouse position and buttons
+  void update_mouse();
+  
+  // print debug messages to USB serial
+  void print_debug_msg();
+  
+  // detect if accelestick is at rest
+  void detect_mma_rest();
+  
+  // flash specified LED. Needed 2 copies because Ticker cannot attach functions with parameters
+  void flash_led2();
+  void flash_led3();
+  
   // convert floating MMA g value into integer
-  int16_t scale_g(int8_t g);
   G_int_t conv_g2int(G_float_t mma_g);
   
-  // detect MMA at rest
-  void detect_mma_rest();
-
-  // Sample joystick for button press
-  void get_joystick_input();
-  
-  inline void reset_tm_joystick_dc();
   int8_t init_accelestick();
-  int8_t run_accelestick();
+  void run_accelestick();
   
 #endif
\ No newline at end of file