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

Committer:
thlu
Date:
Sat Mar 15 09:32:43 2014 +0000
Revision:
0:22bdcdc386df
Child:
1:03d0f8a4a2d7
Got calibration working using x,y, and z-axes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thlu 0:22bdcdc386df 1 #include <stdlib.h>
thlu 0:22bdcdc386df 2 #include "mbed.h"
thlu 0:22bdcdc386df 3
thlu 0:22bdcdc386df 4 #ifndef ACCELESTICK_H
thlu 0:22bdcdc386df 5 #define ACCELESTICK_H
thlu 0:22bdcdc386df 6
thlu 0:22bdcdc386df 7 #include "MMA7660.h" // Accelerometer API
thlu 0:22bdcdc386df 8 #include "C12832_lcd.h" // LCD API
thlu 0:22bdcdc386df 9 #include "USBMouseKeyboard.h"
thlu 0:22bdcdc386df 10
thlu 0:22bdcdc386df 11 #define SCALE_X 50 // g value has noise about 0.05
thlu 0:22bdcdc386df 12 #define SCALE_Y SCALE_X
thlu 0:22bdcdc386df 13 #define SCALE_Z SCALE_X
thlu 0:22bdcdc386df 14 #define ACCEL_MIN -1.5
thlu 0:22bdcdc386df 15 #define ACCEL_MAX 1.5
thlu 0:22bdcdc386df 16
thlu 0:22bdcdc386df 17 #define CALIB_SMPLS 16 // number of samples used for calibrating accelerometer
thlu 0:22bdcdc386df 18
thlu 0:22bdcdc386df 19 // Joystick button values based on joyb declaration
thlu 0:22bdcdc386df 20 #define JS_NONE 0
thlu 0:22bdcdc386df 21 #define JS_DOWN 1
thlu 0:22bdcdc386df 22 #define JS_LEFT 2
thlu 0:22bdcdc386df 23 #define JS_CENTER 4
thlu 0:22bdcdc386df 24 #define JS_UP 8
thlu 0:22bdcdc386df 25 #define JS_RIGHT 16
thlu 0:22bdcdc386df 26
thlu 0:22bdcdc386df 27 //--- Global Variables declaration ---//
thlu 0:22bdcdc386df 28
thlu 0:22bdcdc386df 29 C12832_LCD lcd; // for debugging
thlu 0:22bdcdc386df 30 MMA7660 mma(p28, p27); // I2C Accelerometer
thlu 0:22bdcdc386df 31 USBMouseKeyboard mouse;
thlu 0:22bdcdc386df 32 DigitalOut led_alive(LED4);
thlu 0:22bdcdc386df 33 DigitalOut leds[] = {LED1, LED2, LED3};
thlu 0:22bdcdc386df 34
thlu 0:22bdcdc386df 35 DigitalIn jd = p12;
thlu 0:22bdcdc386df 36 DigitalIn jl = p13;
thlu 0:22bdcdc386df 37 DigitalIn jc = p14;
thlu 0:22bdcdc386df 38 DigitalIn ju = p15;
thlu 0:22bdcdc386df 39 DigitalIn jr = p16;
thlu 0:22bdcdc386df 40 BusIn joyb(p12, p13, p14, p15, p16);
thlu 0:22bdcdc386df 41
thlu 0:22bdcdc386df 42 typedef struct s_max_min {
thlu 0:22bdcdc386df 43 float max_x, max_y, max_z;
thlu 0:22bdcdc386df 44 float min_x, min_y, min_z;
thlu 0:22bdcdc386df 45 } Max_min_t;
thlu 0:22bdcdc386df 46
thlu 0:22bdcdc386df 47 typedef struct s_int_g {
thlu 0:22bdcdc386df 48 int16_t x, y, z;
thlu 0:22bdcdc386df 49 } G_int_t;
thlu 0:22bdcdc386df 50
thlu 0:22bdcdc386df 51 typedef struct s_float_pos {
thlu 0:22bdcdc386df 52 float x, y, z;
thlu 0:22bdcdc386df 53 } G_float_t;
thlu 0:22bdcdc386df 54
thlu 0:22bdcdc386df 55 typedef struct
thlu 0:22bdcdc386df 56 {
thlu 0:22bdcdc386df 57 int16_t x, y, z; // x, y, z position
thlu 0:22bdcdc386df 58 uint8_t button; // left button, 1 = left, 2 = center, 4 = middle
thlu 0:22bdcdc386df 59 bool dc; // double click
thlu 0:22bdcdc386df 60 int8_t scroll; // <0 to go up, >0 to go down
thlu 0:22bdcdc386df 61 } Mouse_state_t;
thlu 0:22bdcdc386df 62
thlu 0:22bdcdc386df 63 Max_min_t peaks;
thlu 0:22bdcdc386df 64 Mouse_state_t mouse_info;
thlu 0:22bdcdc386df 65 G_int_t offset; // calibration offset value
thlu 0:22bdcdc386df 66
thlu 0:22bdcdc386df 67 bool calib_on = 0;
thlu 0:22bdcdc386df 68 bool debug_on = 0;
thlu 0:22bdcdc386df 69 uint8_t tm_joystick_running = 0, debug_sel;
thlu 0:22bdcdc386df 70
thlu 0:22bdcdc386df 71 G_float_t mma_g; // g values from accelerometer
thlu 0:22bdcdc386df 72
thlu 0:22bdcdc386df 73 Ticker tk_led_alive; // Ticker for flashing LED as program alive beacon
thlu 0:22bdcdc386df 74 Ticker tk_print_debug;// Ticker for printing debug information
thlu 0:22bdcdc386df 75 Timer tm_joystick_dc; // Timer for joystick double-click detection
thlu 0:22bdcdc386df 76 RawSerial debug(USBTX, USBRX);
thlu 0:22bdcdc386df 77
thlu 0:22bdcdc386df 78 // End of Global Variable Declaration
thlu 0:22bdcdc386df 79
thlu 0:22bdcdc386df 80
thlu 0:22bdcdc386df 81 //--- Functions Declaration ---//
thlu 0:22bdcdc386df 82
thlu 0:22bdcdc386df 83 // calibrate accelerometer
thlu 0:22bdcdc386df 84 void calib_mma(G_int_t current);
thlu 0:22bdcdc386df 85
thlu 0:22bdcdc386df 86 // convert floating MMA g value into integer
thlu 0:22bdcdc386df 87 int16_t scale_g(int8_t g);
thlu 0:22bdcdc386df 88 G_int_t conv_g2int(G_float_t mma_g);
thlu 0:22bdcdc386df 89
thlu 0:22bdcdc386df 90 // detect MMA at rest
thlu 0:22bdcdc386df 91 void detect_mma_rest();
thlu 0:22bdcdc386df 92
thlu 0:22bdcdc386df 93 // Sample joystick for button press
thlu 0:22bdcdc386df 94 void get_joystick_input();
thlu 0:22bdcdc386df 95
thlu 0:22bdcdc386df 96 inline void reset_tm_joystick_dc();
thlu 0:22bdcdc386df 97 int8_t init_accelestick();
thlu 0:22bdcdc386df 98 int8_t run_accelestick();
thlu 0:22bdcdc386df 99
thlu 0:22bdcdc386df 100 #endif