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
accelestick.h@2:3d012df30652, 2014-03-28 (annotated)
- Committer:
- thlu
- Date:
- Fri Mar 28 06:08:33 2014 +0000
- Revision:
- 2:3d012df30652
- Parent:
- 1:03d0f8a4a2d7
Fixed filter_noise comparison logic.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thlu | 1:03d0f8a4a2d7 | 1 | // Name: T.H. Lu |
thlu | 1:03d0f8a4a2d7 | 2 | // Date: 03/27/2014 |
thlu | 1:03d0f8a4a2d7 | 3 | // Description: |
thlu | 1:03d0f8a4a2d7 | 4 | // Header file for accelestick. Contains all defines and function declarations. |
thlu | 1:03d0f8a4a2d7 | 5 | // |
thlu | 1:03d0f8a4a2d7 | 6 | |
thlu | 0:22bdcdc386df | 7 | #include <stdlib.h> |
thlu | 0:22bdcdc386df | 8 | #include "mbed.h" |
thlu | 0:22bdcdc386df | 9 | |
thlu | 1:03d0f8a4a2d7 | 10 | #include "MMA7660.h" // Accelerometer API |
thlu | 1:03d0f8a4a2d7 | 11 | #include "C12832_lcd.h" // LCD API |
thlu | 1:03d0f8a4a2d7 | 12 | #include "USBMouseKeyboard.h"// USB Mouse API |
thlu | 0:22bdcdc386df | 13 | |
thlu | 1:03d0f8a4a2d7 | 14 | #ifndef ACCELESTICK_H |
thlu | 1:03d0f8a4a2d7 | 15 | #define ACCELESTICK_H |
thlu | 0:22bdcdc386df | 16 | |
thlu | 2:3d012df30652 | 17 | #define SCALE_X 50 // float to int conversion factor |
thlu | 0:22bdcdc386df | 18 | #define SCALE_Y SCALE_X |
thlu | 0:22bdcdc386df | 19 | #define SCALE_Z SCALE_X |
thlu | 2:3d012df30652 | 20 | #define NOISE_FLOOR 0.10*SCALE_X // zero out any integer g value less than NOISE_FLOOR |
thlu | 1:03d0f8a4a2d7 | 21 | |
thlu | 0:22bdcdc386df | 22 | #define ACCEL_MIN -1.5 |
thlu | 0:22bdcdc386df | 23 | #define ACCEL_MAX 1.5 |
thlu | 0:22bdcdc386df | 24 | |
thlu | 0:22bdcdc386df | 25 | #define CALIB_SMPLS 16 // number of samples used for calibrating accelerometer |
thlu | 0:22bdcdc386df | 26 | |
thlu | 0:22bdcdc386df | 27 | // Joystick button values based on joyb declaration |
thlu | 1:03d0f8a4a2d7 | 28 | enum {JS_NONE=0, JS_DOWN=1, JS_LEFT=2, JS_CENTER=4, JS_UP=8, JS_RIGHT=16}; |
thlu | 1:03d0f8a4a2d7 | 29 | enum {LED_LOCK=0, LED_DEBUG, LED_CALIB, LED_ALIVE}; |
thlu | 0:22bdcdc386df | 30 | |
thlu | 1:03d0f8a4a2d7 | 31 | // structure to store max and min g values for debugging |
thlu | 0:22bdcdc386df | 32 | typedef struct s_max_min { |
thlu | 0:22bdcdc386df | 33 | float max_x, max_y, max_z; |
thlu | 0:22bdcdc386df | 34 | float min_x, min_y, min_z; |
thlu | 0:22bdcdc386df | 35 | } Max_min_t; |
thlu | 0:22bdcdc386df | 36 | |
thlu | 1:03d0f8a4a2d7 | 37 | // structure for accelerometer int g values |
thlu | 0:22bdcdc386df | 38 | typedef struct s_int_g { |
thlu | 0:22bdcdc386df | 39 | int16_t x, y, z; |
thlu | 0:22bdcdc386df | 40 | } G_int_t; |
thlu | 0:22bdcdc386df | 41 | |
thlu | 1:03d0f8a4a2d7 | 42 | // structure for accelerometer float g values |
thlu | 0:22bdcdc386df | 43 | typedef struct s_float_pos { |
thlu | 0:22bdcdc386df | 44 | float x, y, z; |
thlu | 0:22bdcdc386df | 45 | } G_float_t; |
thlu | 0:22bdcdc386df | 46 | |
thlu | 1:03d0f8a4a2d7 | 47 | // structure to store all mouse related data |
thlu | 0:22bdcdc386df | 48 | typedef struct |
thlu | 0:22bdcdc386df | 49 | { |
thlu | 0:22bdcdc386df | 50 | int16_t x, y, z; // x, y, z position |
thlu | 0:22bdcdc386df | 51 | uint8_t button; // left button, 1 = left, 2 = center, 4 = middle |
thlu | 0:22bdcdc386df | 52 | bool dc; // double click |
thlu | 0:22bdcdc386df | 53 | int8_t scroll; // <0 to go up, >0 to go down |
thlu | 0:22bdcdc386df | 54 | } Mouse_state_t; |
thlu | 0:22bdcdc386df | 55 | |
thlu | 0:22bdcdc386df | 56 | |
thlu | 0:22bdcdc386df | 57 | //--- Functions Declaration ---// |
thlu | 0:22bdcdc386df | 58 | |
thlu | 1:03d0f8a4a2d7 | 59 | // scan joystick for mouse buttons and scroll |
thlu | 1:03d0f8a4a2d7 | 60 | void get_joystick_input(); |
thlu | 1:03d0f8a4a2d7 | 61 | |
thlu | 1:03d0f8a4a2d7 | 62 | // sample accelerometer values for mouse movement |
thlu | 1:03d0f8a4a2d7 | 63 | void sample_mma(); |
thlu | 1:03d0f8a4a2d7 | 64 | |
thlu | 0:22bdcdc386df | 65 | // calibrate accelerometer |
thlu | 0:22bdcdc386df | 66 | void calib_mma(G_int_t current); |
thlu | 0:22bdcdc386df | 67 | |
thlu | 1:03d0f8a4a2d7 | 68 | // update mouse position and buttons |
thlu | 1:03d0f8a4a2d7 | 69 | void update_mouse(); |
thlu | 1:03d0f8a4a2d7 | 70 | |
thlu | 1:03d0f8a4a2d7 | 71 | // print debug messages to USB serial |
thlu | 1:03d0f8a4a2d7 | 72 | void print_debug_msg(); |
thlu | 1:03d0f8a4a2d7 | 73 | |
thlu | 2:3d012df30652 | 74 | // filter out accelestick noise |
thlu | 2:3d012df30652 | 75 | int16_t filter_noise(int16_t const); |
thlu | 1:03d0f8a4a2d7 | 76 | |
thlu | 1:03d0f8a4a2d7 | 77 | // flash specified LED. Needed 2 copies because Ticker cannot attach functions with parameters |
thlu | 1:03d0f8a4a2d7 | 78 | void flash_led2(); |
thlu | 1:03d0f8a4a2d7 | 79 | void flash_led3(); |
thlu | 1:03d0f8a4a2d7 | 80 | |
thlu | 0:22bdcdc386df | 81 | // convert floating MMA g value into integer |
thlu | 0:22bdcdc386df | 82 | G_int_t conv_g2int(G_float_t mma_g); |
thlu | 0:22bdcdc386df | 83 | |
thlu | 0:22bdcdc386df | 84 | int8_t init_accelestick(); |
thlu | 1:03d0f8a4a2d7 | 85 | void run_accelestick(); |
thlu | 0:22bdcdc386df | 86 | |
thlu | 0:22bdcdc386df | 87 | #endif |