My Embedded Systems Project Ihsian Mulla (el14imfm@leeds.ac.uk 200839613
joystick.cpp@0:f79fe9dc65ee, 2016-05-04 (annotated)
- Committer:
- Ihsianmulla
- Date:
- Wed May 04 13:01:09 2016 +0000
- Revision:
- 0:f79fe9dc65ee
Joystick Functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ihsianmulla | 0:f79fe9dc65ee | 1 | /** Temperature Sensor Functions |
Ihsianmulla | 0:f79fe9dc65ee | 2 | * Used for printing and updating the temperature at 1Hz with unit conversion capabilities |
Ihsianmulla | 0:f79fe9dc65ee | 3 | @file joystick.cpp |
Ihsianmulla | 0:f79fe9dc65ee | 4 | @brief contains functions for joystick update/calibration and direction names with specification |
Ihsianmulla | 0:f79fe9dc65ee | 5 | @author Craig Evans |
Ihsianmulla | 0:f79fe9dc65ee | 6 | @date May 2016 |
Ihsianmulla | 0:f79fe9dc65ee | 7 | */ |
Ihsianmulla | 0:f79fe9dc65ee | 8 | |
Ihsianmulla | 0:f79fe9dc65ee | 9 | #include "joystick.h" |
Ihsianmulla | 0:f79fe9dc65ee | 10 | |
Ihsianmulla | 0:f79fe9dc65ee | 11 | extern Joystick joystick; |
Ihsianmulla | 0:f79fe9dc65ee | 12 | |
Ihsianmulla | 0:f79fe9dc65ee | 13 | void calibrateJoystick() |
Ihsianmulla | 0:f79fe9dc65ee | 14 | { |
Ihsianmulla | 0:f79fe9dc65ee | 15 | button.mode(PullDown);// additional pull down for external button |
Ihsianmulla | 0:f79fe9dc65ee | 16 | Jbutton.mode(PullDown); |
Ihsianmulla | 0:f79fe9dc65ee | 17 | // must not move during calibration |
Ihsianmulla | 0:f79fe9dc65ee | 18 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
Ihsianmulla | 0:f79fe9dc65ee | 19 | joystick.y0 = yPot; |
Ihsianmulla | 0:f79fe9dc65ee | 20 | } |
Ihsianmulla | 0:f79fe9dc65ee | 21 | |
Ihsianmulla | 0:f79fe9dc65ee | 22 | Joystick return_data(){ |
Ihsianmulla | 0:f79fe9dc65ee | 23 | return joystick; |
Ihsianmulla | 0:f79fe9dc65ee | 24 | } |
Ihsianmulla | 0:f79fe9dc65ee | 25 | void updateJoystick() |
Ihsianmulla | 0:f79fe9dc65ee | 26 | { |
Ihsianmulla | 0:f79fe9dc65ee | 27 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
Ihsianmulla | 0:f79fe9dc65ee | 28 | joystick.x = xPot - joystick.x0; |
Ihsianmulla | 0:f79fe9dc65ee | 29 | joystick.y = yPot - joystick.y0; |
Ihsianmulla | 0:f79fe9dc65ee | 30 | // read button state |
Ihsianmulla | 0:f79fe9dc65ee | 31 | joystick.button = button; |
Ihsianmulla | 0:f79fe9dc65ee | 32 | |
Ihsianmulla | 0:f79fe9dc65ee | 33 | // calculate direction depending on x,y values |
Ihsianmulla | 0:f79fe9dc65ee | 34 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
Ihsianmulla | 0:f79fe9dc65ee | 35 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
Ihsianmulla | 0:f79fe9dc65ee | 36 | joystick.direction = CENTRE; |
Ihsianmulla | 0:f79fe9dc65ee | 37 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
Ihsianmulla | 0:f79fe9dc65ee | 38 | joystick.direction = UP; |
Ihsianmulla | 0:f79fe9dc65ee | 39 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
Ihsianmulla | 0:f79fe9dc65ee | 40 | joystick.direction = DOWN; |
Ihsianmulla | 0:f79fe9dc65ee | 41 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
Ihsianmulla | 0:f79fe9dc65ee | 42 | joystick.direction = RIGHT; |
Ihsianmulla | 0:f79fe9dc65ee | 43 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
Ihsianmulla | 0:f79fe9dc65ee | 44 | joystick.direction = LEFT; |
Ihsianmulla | 0:f79fe9dc65ee | 45 | } else { |
Ihsianmulla | 0:f79fe9dc65ee | 46 | joystick.direction = UNKNOWN; |
Ihsianmulla | 0:f79fe9dc65ee | 47 | } |
Ihsianmulla | 0:f79fe9dc65ee | 48 | |
Ihsianmulla | 0:f79fe9dc65ee | 49 | } |