In-air mouse using Freedom board
Dependencies: MMA8451Q TSI USBDevice mbed
Fork of USBMouse_HelloWorld by
First project using (and testing out) the Freescale Freedom board.
Plug the USB (not SDA) connector into your machine.
Tilt the board to move the cursor, and use the touch sensor for left and right click.
main.cpp@5:46a260b843b9, 2013-03-03 (annotated)
- Committer:
- User_4574
- Date:
- Sun Mar 03 16:01:33 2013 +0000
- Revision:
- 5:46a260b843b9
- Parent:
- 3:b8caa902d79e
First Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 2:e7b766501add | 1 | #include "mbed.h" |
samux | 2:e7b766501add | 2 | #include "USBMouse.h" |
User_4574 | 5:46a260b843b9 | 3 | #include "MMA8451Q.h" |
User_4574 | 5:46a260b843b9 | 4 | #include "TSISensor.h" |
User_4574 | 5:46a260b843b9 | 5 | |
User_4574 | 5:46a260b843b9 | 6 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
samux | 2:e7b766501add | 7 | |
samux | 2:e7b766501add | 8 | USBMouse mouse; |
User_4574 | 5:46a260b843b9 | 9 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
User_4574 | 5:46a260b843b9 | 10 | TSISensor tsi; |
User_4574 | 5:46a260b843b9 | 11 | |
User_4574 | 5:46a260b843b9 | 12 | typedef enum {NONE = 0, LEFT, RIGHT} Click; |
samux | 2:e7b766501add | 13 | |
samux | 2:e7b766501add | 14 | int main() { |
User_4574 | 5:46a260b843b9 | 15 | int16_t x = 0, y = 0; |
User_4574 | 5:46a260b843b9 | 16 | float t; |
User_4574 | 5:46a260b843b9 | 17 | Click in_click = NONE; |
samux | 2:e7b766501add | 18 | |
samux | 2:e7b766501add | 19 | while (1) { |
User_4574 | 5:46a260b843b9 | 20 | t = acc.getAccX(); |
User_4574 | 5:46a260b843b9 | 21 | t *= 10/1.5; |
User_4574 | 5:46a260b843b9 | 22 | y = (int16_t) t; |
User_4574 | 5:46a260b843b9 | 23 | |
User_4574 | 5:46a260b843b9 | 24 | t = acc.getAccY(); |
User_4574 | 5:46a260b843b9 | 25 | t *= 10/1.5; |
User_4574 | 5:46a260b843b9 | 26 | x = - (int16_t) t; |
User_4574 | 5:46a260b843b9 | 27 | |
User_4574 | 5:46a260b843b9 | 28 | t = tsi.readPercentage(); |
User_4574 | 5:46a260b843b9 | 29 | |
User_4574 | 5:46a260b843b9 | 30 | if (in_click == NONE) { |
User_4574 | 5:46a260b843b9 | 31 | if (t > 0.6) { |
User_4574 | 5:46a260b843b9 | 32 | mouse.press(MOUSE_LEFT); |
User_4574 | 5:46a260b843b9 | 33 | in_click = LEFT; |
User_4574 | 5:46a260b843b9 | 34 | } else if (t > 0.1) { |
User_4574 | 5:46a260b843b9 | 35 | mouse.press(MOUSE_RIGHT); |
User_4574 | 5:46a260b843b9 | 36 | in_click = RIGHT; |
User_4574 | 5:46a260b843b9 | 37 | } |
User_4574 | 5:46a260b843b9 | 38 | } else if (in_click == LEFT) { |
User_4574 | 5:46a260b843b9 | 39 | if (t <= 0.6) { |
User_4574 | 5:46a260b843b9 | 40 | mouse.release(MOUSE_LEFT); |
User_4574 | 5:46a260b843b9 | 41 | in_click = NONE; |
User_4574 | 5:46a260b843b9 | 42 | } |
User_4574 | 5:46a260b843b9 | 43 | } else { |
User_4574 | 5:46a260b843b9 | 44 | if (t > 0.6 || t <= 0.1) { |
User_4574 | 5:46a260b843b9 | 45 | mouse.release(MOUSE_RIGHT); |
User_4574 | 5:46a260b843b9 | 46 | in_click = NONE; |
User_4574 | 5:46a260b843b9 | 47 | } |
User_4574 | 5:46a260b843b9 | 48 | } |
samux | 3:b8caa902d79e | 49 | |
samux | 2:e7b766501add | 50 | mouse.move(x, y); |
samux | 2:e7b766501add | 51 | wait(0.001); |
samux | 2:e7b766501add | 52 | } |
samux | 0:48fd0c31cef5 | 53 | } |