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
Diff: accelestick.h
- Revision:
- 0:22bdcdc386df
- Child:
- 1:03d0f8a4a2d7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/accelestick.h Sat Mar 15 09:32:43 2014 +0000 @@ -0,0 +1,100 @@ +#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" + +#define SCALE_X 50 // g value has noise about 0.05 +#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 ---// + +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); + +typedef struct s_max_min { + float max_x, max_y, max_z; + float min_x, min_y, min_z; +} Max_min_t; + +typedef struct s_int_g { + int16_t x, y, z; +} G_int_t; + +typedef struct s_float_pos { + float x, y, z; +} G_float_t; + +typedef struct +{ + int16_t x, y, z; // x, y, z position + uint8_t button; // left button, 1 = left, 2 = center, 4 = middle + bool dc; // double click + 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 ---// + + // calibrate accelerometer + void calib_mma(G_int_t current); + + // 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(); + +#endif \ No newline at end of file