My Embedded Systems Project Ihsian Mulla (el14imfm@leeds.ac.uk 200839613
Diff: joystick.cpp
- Revision:
- 0:f79fe9dc65ee
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/joystick.cpp Wed May 04 13:01:09 2016 +0000 @@ -0,0 +1,49 @@ +/** Temperature Sensor Functions +* Used for printing and updating the temperature at 1Hz with unit conversion capabilities +@file joystick.cpp +@brief contains functions for joystick update/calibration and direction names with specification +@author Craig Evans +@date May 2016 +*/ + +#include "joystick.h" + +extern Joystick joystick; + +void calibrateJoystick() +{ + button.mode(PullDown);// additional pull down for external button + Jbutton.mode(PullDown); + // must not move during calibration + joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) + joystick.y0 = yPot; +} + +Joystick return_data(){ + return joystick; +} +void updateJoystick() +{ + // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) + joystick.x = xPot - joystick.x0; + joystick.y = yPot - joystick.y0; + // read button state + joystick.button = button; + + // calculate direction depending on x,y values + // tolerance allows a little lee-way in case joystick not exactly in the stated direction + if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = CENTRE; + } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = UP; + } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = DOWN; + } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { + joystick.direction = RIGHT; + } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { + joystick.direction = LEFT; + } else { + joystick.direction = UNKNOWN; + } + +} \ No newline at end of file