Yudong Xiao
/
pokemon
This is test version of Pokemongo game. ELEC 2645 final project.
pokeball/Joystick.cpp@0:819c2d6a69ac, 2021-04-15 (annotated)
- Committer:
- shalwego
- Date:
- Thu Apr 15 15:35:12 2021 +0000
- Revision:
- 0:819c2d6a69ac
Issue about music playing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shalwego | 0:819c2d6a69ac | 1 | #include "Joystick.h" |
shalwego | 0:819c2d6a69ac | 2 | |
shalwego | 0:819c2d6a69ac | 3 | Joystick::Joystick(PinName vertPin,PinName horizPin) |
shalwego | 0:819c2d6a69ac | 4 | { |
shalwego | 0:819c2d6a69ac | 5 | vert = new AnalogIn(vertPin); |
shalwego | 0:819c2d6a69ac | 6 | horiz = new AnalogIn(horizPin); |
shalwego | 0:819c2d6a69ac | 7 | } |
shalwego | 0:819c2d6a69ac | 8 | |
shalwego | 0:819c2d6a69ac | 9 | void Joystick::init() |
shalwego | 0:819c2d6a69ac | 10 | { |
shalwego | 0:819c2d6a69ac | 11 | // read centred values of joystick |
shalwego | 0:819c2d6a69ac | 12 | _x0 = horiz->read(); |
shalwego | 0:819c2d6a69ac | 13 | _y0 = vert->read(); |
shalwego | 0:819c2d6a69ac | 14 | |
shalwego | 0:819c2d6a69ac | 15 | // this assumes that the joystick is centred when the init function is called |
shalwego | 0:819c2d6a69ac | 16 | // if perfectly centred, the pots should read 0.5, but this may |
shalwego | 0:819c2d6a69ac | 17 | // not be the case and x0 and y0 will be used to calibrate readings |
shalwego | 0:819c2d6a69ac | 18 | } |
shalwego | 0:819c2d6a69ac | 19 | |
shalwego | 0:819c2d6a69ac | 20 | Direction Joystick::get_direction() |
shalwego | 0:819c2d6a69ac | 21 | { |
shalwego | 0:819c2d6a69ac | 22 | float angle = get_angle(); // 0 to 360, -1 for centred |
shalwego | 0:819c2d6a69ac | 23 | |
shalwego | 0:819c2d6a69ac | 24 | Direction d; |
shalwego | 0:819c2d6a69ac | 25 | // partition 360 into segments and check which segment the angle is in |
shalwego | 0:819c2d6a69ac | 26 | if (angle < 0.0f) { |
shalwego | 0:819c2d6a69ac | 27 | d = CENTRE; // check for -1.0 angle |
shalwego | 0:819c2d6a69ac | 28 | } else if (angle < 22.5f) { // then keep going in 45 degree increments |
shalwego | 0:819c2d6a69ac | 29 | d = N; |
shalwego | 0:819c2d6a69ac | 30 | } else if (angle < 67.5f) { |
shalwego | 0:819c2d6a69ac | 31 | d = NE; |
shalwego | 0:819c2d6a69ac | 32 | } else if (angle < 112.5f) { |
shalwego | 0:819c2d6a69ac | 33 | d = E; |
shalwego | 0:819c2d6a69ac | 34 | } else if (angle < 157.5f) { |
shalwego | 0:819c2d6a69ac | 35 | d = SE; |
shalwego | 0:819c2d6a69ac | 36 | } else if (angle < 202.5f) { |
shalwego | 0:819c2d6a69ac | 37 | d = S; |
shalwego | 0:819c2d6a69ac | 38 | } else if (angle < 247.5f) { |
shalwego | 0:819c2d6a69ac | 39 | d = SW; |
shalwego | 0:819c2d6a69ac | 40 | } else if (angle < 292.5f) { |
shalwego | 0:819c2d6a69ac | 41 | d = W; |
shalwego | 0:819c2d6a69ac | 42 | } else if (angle < 337.5f) { |
shalwego | 0:819c2d6a69ac | 43 | d = NW; |
shalwego | 0:819c2d6a69ac | 44 | } else { |
shalwego | 0:819c2d6a69ac | 45 | d = N; |
shalwego | 0:819c2d6a69ac | 46 | } |
shalwego | 0:819c2d6a69ac | 47 | |
shalwego | 0:819c2d6a69ac | 48 | return d; |
shalwego | 0:819c2d6a69ac | 49 | } |
shalwego | 0:819c2d6a69ac | 50 | |
shalwego | 0:819c2d6a69ac | 51 | // this method gets the magnitude of the joystick movement |
shalwego | 0:819c2d6a69ac | 52 | float Joystick::get_mag() { |
shalwego | 0:819c2d6a69ac | 53 | Polar p = get_polar(); |
shalwego | 0:819c2d6a69ac | 54 | return p.mag; |
shalwego | 0:819c2d6a69ac | 55 | } |
shalwego | 0:819c2d6a69ac | 56 | |
shalwego | 0:819c2d6a69ac | 57 | // this method gets the angle of joystick movement (0 to 360, 0 North) |
shalwego | 0:819c2d6a69ac | 58 | float Joystick::get_angle() |
shalwego | 0:819c2d6a69ac | 59 | { |
shalwego | 0:819c2d6a69ac | 60 | Polar p = get_polar(); |
shalwego | 0:819c2d6a69ac | 61 | return p.angle; |
shalwego | 0:819c2d6a69ac | 62 | } |
shalwego | 0:819c2d6a69ac | 63 | |
shalwego | 0:819c2d6a69ac | 64 | // get raw joystick coordinate in range -1 to 1 |
shalwego | 0:819c2d6a69ac | 65 | // Direction (x,y) |
shalwego | 0:819c2d6a69ac | 66 | // North (0,1) |
shalwego | 0:819c2d6a69ac | 67 | // East (1,0) |
shalwego | 0:819c2d6a69ac | 68 | // South (0,-1) |
shalwego | 0:819c2d6a69ac | 69 | // West (-1,0) |
shalwego | 0:819c2d6a69ac | 70 | Vector2D Joystick::get_coord() |
shalwego | 0:819c2d6a69ac | 71 | { |
shalwego | 0:819c2d6a69ac | 72 | // read() returns value in range 0.0 to 1.0 so is scaled and centre value |
shalwego | 0:819c2d6a69ac | 73 | // substracted to get values in the range -1.0 to 1.0 |
shalwego | 0:819c2d6a69ac | 74 | float x = 2.0f*( horiz->read() - _x0 ); |
shalwego | 0:819c2d6a69ac | 75 | float y = 2.0f*( vert->read() - _y0 ); |
shalwego | 0:819c2d6a69ac | 76 | |
shalwego | 0:819c2d6a69ac | 77 | // Note: the values are negated so positive is up and right. |
shalwego | 0:819c2d6a69ac | 78 | Vector2D coord = {-x,y}; |
shalwego | 0:819c2d6a69ac | 79 | return coord; |
shalwego | 0:819c2d6a69ac | 80 | } |
shalwego | 0:819c2d6a69ac | 81 | |
shalwego | 0:819c2d6a69ac | 82 | // This maps the raw x,y coord onto a circular grid. |
shalwego | 0:819c2d6a69ac | 83 | // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html |
shalwego | 0:819c2d6a69ac | 84 | Vector2D Joystick::get_mapped_coord() |
shalwego | 0:819c2d6a69ac | 85 | { |
shalwego | 0:819c2d6a69ac | 86 | Vector2D coord = get_coord(); |
shalwego | 0:819c2d6a69ac | 87 | |
shalwego | 0:819c2d6a69ac | 88 | // do the transformation |
shalwego | 0:819c2d6a69ac | 89 | float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); |
shalwego | 0:819c2d6a69ac | 90 | float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); |
shalwego | 0:819c2d6a69ac | 91 | |
shalwego | 0:819c2d6a69ac | 92 | Vector2D mapped_coord = {x,y}; |
shalwego | 0:819c2d6a69ac | 93 | return mapped_coord; |
shalwego | 0:819c2d6a69ac | 94 | } |
shalwego | 0:819c2d6a69ac | 95 | |
shalwego | 0:819c2d6a69ac | 96 | // this function converts the mapped coordinates into polar form |
shalwego | 0:819c2d6a69ac | 97 | Polar Joystick::get_polar() |
shalwego | 0:819c2d6a69ac | 98 | { |
shalwego | 0:819c2d6a69ac | 99 | // get the mapped coordinate |
shalwego | 0:819c2d6a69ac | 100 | Vector2D coord = get_mapped_coord(); |
shalwego | 0:819c2d6a69ac | 101 | |
shalwego | 0:819c2d6a69ac | 102 | // at this point, 0 degrees (i.e. x-axis) will be defined to the East. |
shalwego | 0:819c2d6a69ac | 103 | // We want 0 degrees to correspond to North and increase clockwise to 359 |
shalwego | 0:819c2d6a69ac | 104 | // like a compass heading, so we need to swap the axis and invert y |
shalwego | 0:819c2d6a69ac | 105 | float x = coord.y; |
shalwego | 0:819c2d6a69ac | 106 | float y = coord.x; |
shalwego | 0:819c2d6a69ac | 107 | |
shalwego | 0:819c2d6a69ac | 108 | float mag = sqrt(x*x+y*y); // pythagoras |
shalwego | 0:819c2d6a69ac | 109 | float angle = RAD2DEG*atan2(y,x); |
shalwego | 0:819c2d6a69ac | 110 | // angle will be in range -180 to 180, so add 360 to negative angles to |
shalwego | 0:819c2d6a69ac | 111 | // move to 0 to 360 range |
shalwego | 0:819c2d6a69ac | 112 | if (angle < 0.0f) { |
shalwego | 0:819c2d6a69ac | 113 | angle+=360.0f; |
shalwego | 0:819c2d6a69ac | 114 | } |
shalwego | 0:819c2d6a69ac | 115 | |
shalwego | 0:819c2d6a69ac | 116 | // the noise on the ADC causes the values of x and y to fluctuate slightly |
shalwego | 0:819c2d6a69ac | 117 | // around the centred values. This causes the random angle values to get |
shalwego | 0:819c2d6a69ac | 118 | // calculated when the joystick is centred and untouched. This is also when |
shalwego | 0:819c2d6a69ac | 119 | // the magnitude is very small, so we can check for a small magnitude and then |
shalwego | 0:819c2d6a69ac | 120 | // set the angle to -1. This will inform us when the angle is invalid and the |
shalwego | 0:819c2d6a69ac | 121 | // joystick is centred |
shalwego | 0:819c2d6a69ac | 122 | |
shalwego | 0:819c2d6a69ac | 123 | if (mag < TOL) { |
shalwego | 0:819c2d6a69ac | 124 | mag = 0.0f; |
shalwego | 0:819c2d6a69ac | 125 | angle = -1.0f; |
shalwego | 0:819c2d6a69ac | 126 | } |
shalwego | 0:819c2d6a69ac | 127 | |
shalwego | 0:819c2d6a69ac | 128 | Polar p = {mag,angle}; |
shalwego | 0:819c2d6a69ac | 129 | return p; |
shalwego | 0:819c2d6a69ac | 130 | } |