functions for reading the joystick
Dependents: SnakeProjectRev1 SnakeProjectRev1
Joystick.h@3:ae04f59e274a, 2016-05-05 (annotated)
- Committer:
- meurigp
- Date:
- Thu May 05 14:19:34 2016 +0000
- Revision:
- 3:ae04f59e274a
- Parent:
- 2:e444008676a4
Neater code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
meurigp | 0:18c9492be42e | 1 | #ifndef JOYSTICK_H |
meurigp | 0:18c9492be42e | 2 | #define JOYSTICK_H |
meurigp | 0:18c9492be42e | 3 | |
meurigp | 0:18c9492be42e | 4 | #include "mbed.h" |
meurigp | 0:18c9492be42e | 5 | |
meurigp | 0:18c9492be42e | 6 | // change this to alter tolerance of joystick direction |
meurigp | 0:18c9492be42e | 7 | #define DIRECTION_TOLERANCE 0.05 |
meurigp | 0:18c9492be42e | 8 | |
meurigp | 0:18c9492be42e | 9 | // connections for joystick |
meurigp | 2:e444008676a4 | 10 | InterruptIn button(PTB11); |
meurigp | 0:18c9492be42e | 11 | AnalogIn xPot(PTB2); |
meurigp | 0:18c9492be42e | 12 | AnalogIn yPot(PTB3); |
meurigp | 0:18c9492be42e | 13 | |
meurigp | 3:ae04f59e274a | 14 | Ticker pollJoystick; |
meurigp | 3:ae04f59e274a | 15 | |
meurigp | 0:18c9492be42e | 16 | // create enumerated type (0,1,2,3 etc. for direction) |
meurigp | 0:18c9492be42e | 17 | // could be extended for diagonals etc. |
meurigp | 0:18c9492be42e | 18 | enum DirectionName { |
meurigp | 0:18c9492be42e | 19 | UP, |
meurigp | 0:18c9492be42e | 20 | DOWN, |
meurigp | 0:18c9492be42e | 21 | LEFT, |
meurigp | 0:18c9492be42e | 22 | RIGHT, |
meurigp | 0:18c9492be42e | 23 | CENTRE, |
meurigp | 0:18c9492be42e | 24 | UNKNOWN |
meurigp | 0:18c9492be42e | 25 | }; |
meurigp | 0:18c9492be42e | 26 | |
meurigp | 0:18c9492be42e | 27 | // struct for Joystick |
meurigp | 0:18c9492be42e | 28 | typedef struct JoyStick Joystick; |
meurigp | 0:18c9492be42e | 29 | struct JoyStick { |
meurigp | 0:18c9492be42e | 30 | float x; // current x value |
meurigp | 0:18c9492be42e | 31 | float x0; // 'centred' x value |
meurigp | 0:18c9492be42e | 32 | float y; // current y value |
meurigp | 0:18c9492be42e | 33 | float y0; // 'centred' y value |
meurigp | 0:18c9492be42e | 34 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
meurigp | 0:18c9492be42e | 35 | DirectionName direction; // current direction |
meurigp | 0:18c9492be42e | 36 | }; |
meurigp | 0:18c9492be42e | 37 | // create struct variable |
meurigp | 0:18c9492be42e | 38 | Joystick joystick; |
meurigp | 0:18c9492be42e | 39 | |
meurigp | 0:18c9492be42e | 40 | int printFlag = 0; |
meurigp | 2:e444008676a4 | 41 | volatile int buttonFlag = 0; |
meurigp | 2:e444008676a4 | 42 | |
meurigp | 2:e444008676a4 | 43 | |
meurigp | 0:18c9492be42e | 44 | |
meurigp | 0:18c9492be42e | 45 | // function prototypes |
meurigp | 2:e444008676a4 | 46 | void buttonISR(); |
meurigp | 0:18c9492be42e | 47 | void calibrateJoystick(); |
meurigp | 0:18c9492be42e | 48 | void updateJoystick(); |
meurigp | 0:18c9492be42e | 49 | |
meurigp | 2:e444008676a4 | 50 | void buttonISR() |
meurigp | 2:e444008676a4 | 51 | { |
meurigp | 2:e444008676a4 | 52 | buttonFlag = 1; |
meurigp | 2:e444008676a4 | 53 | } |
meurigp | 0:18c9492be42e | 54 | |
meurigp | 0:18c9492be42e | 55 | // read default positions of the joystick to calibrate later readings |
meurigp | 0:18c9492be42e | 56 | void calibrateJoystick() |
meurigp | 0:18c9492be42e | 57 | { |
meurigp | 0:18c9492be42e | 58 | button.mode(PullDown); |
meurigp | 0:18c9492be42e | 59 | // must not move during calibration |
meurigp | 0:18c9492be42e | 60 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
meurigp | 0:18c9492be42e | 61 | joystick.y0 = yPot; |
meurigp | 0:18c9492be42e | 62 | } |
meurigp | 0:18c9492be42e | 63 | void updateJoystick() |
meurigp | 0:18c9492be42e | 64 | { |
meurigp | 0:18c9492be42e | 65 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
meurigp | 0:18c9492be42e | 66 | joystick.x = xPot - joystick.x0; |
meurigp | 0:18c9492be42e | 67 | joystick.y = yPot - joystick.y0; |
meurigp | 0:18c9492be42e | 68 | // read button state |
meurigp | 0:18c9492be42e | 69 | joystick.button = button; |
meurigp | 0:18c9492be42e | 70 | |
meurigp | 0:18c9492be42e | 71 | // calculate direction depending on x,y values |
meurigp | 0:18c9492be42e | 72 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
meurigp | 0:18c9492be42e | 73 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
meurigp | 0:18c9492be42e | 74 | joystick.direction = CENTRE; |
meurigp | 0:18c9492be42e | 75 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
meurigp | 0:18c9492be42e | 76 | joystick.direction = UP; |
meurigp | 0:18c9492be42e | 77 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
meurigp | 0:18c9492be42e | 78 | joystick.direction = DOWN; |
meurigp | 0:18c9492be42e | 79 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
meurigp | 0:18c9492be42e | 80 | joystick.direction = LEFT; |
meurigp | 0:18c9492be42e | 81 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
meurigp | 0:18c9492be42e | 82 | joystick.direction = RIGHT; |
meurigp | 0:18c9492be42e | 83 | } else { |
meurigp | 0:18c9492be42e | 84 | joystick.direction = UNKNOWN; |
meurigp | 0:18c9492be42e | 85 | } |
meurigp | 0:18c9492be42e | 86 | |
meurigp | 0:18c9492be42e | 87 | // set flag for printing |
meurigp | 0:18c9492be42e | 88 | printFlag = 1; |
meurigp | 0:18c9492be42e | 89 | } |
meurigp | 0:18c9492be42e | 90 | |
meurigp | 0:18c9492be42e | 91 | #endif |