Working Menu, additions to be made

Dependencies:   mbed

Committer:
jackmcgarley
Date:
Wed Aug 24 18:48:25 2022 +0000
Revision:
15:61d9a4e63b99
Parent:
0:d4d7e882c87d
Working menu, additions to be made;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
25574069 0:d4d7e882c87d 1 #include "Joystick.h"
25574069 0:d4d7e882c87d 2
25574069 0:d4d7e882c87d 3 Joystick::Joystick(PinName vertPin,PinName horizPin,PinName clickPin)
25574069 0:d4d7e882c87d 4 {
25574069 0:d4d7e882c87d 5 vert = new AnalogIn(vertPin);
25574069 0:d4d7e882c87d 6 horiz = new AnalogIn(horizPin);
25574069 0:d4d7e882c87d 7 click = new InterruptIn(clickPin);
25574069 0:d4d7e882c87d 8 }
25574069 0:d4d7e882c87d 9
25574069 0:d4d7e882c87d 10 void Joystick::init()
25574069 0:d4d7e882c87d 11 {
25574069 0:d4d7e882c87d 12 // read centred values of joystick
25574069 0:d4d7e882c87d 13 _x0 = horiz->read();
25574069 0:d4d7e882c87d 14 _y0 = vert->read();
25574069 0:d4d7e882c87d 15
25574069 0:d4d7e882c87d 16 // this assumes that the joystick is centred when the init function is called
25574069 0:d4d7e882c87d 17 // if perfectly centred, the pots should read 0.5, but this may
25574069 0:d4d7e882c87d 18 // not be the case and x0 and y0 will be used to calibrate readings
25574069 0:d4d7e882c87d 19
25574069 0:d4d7e882c87d 20 // turn on pull-down for button -> this assumes the other side of the button
25574069 0:d4d7e882c87d 21 // is connected to +3V3 so we read 1 when pressed and 0 when not pressed
25574069 0:d4d7e882c87d 22 click->mode(PullDown);
25574069 0:d4d7e882c87d 23 // we therefore need to fire the interrupt on a rising edge
25574069 0:d4d7e882c87d 24 click->rise(callback(this,&Joystick::click_isr));
25574069 0:d4d7e882c87d 25 // need to use a callback since mbed-os5 - basically tells it to look in this class for the ISR
25574069 0:d4d7e882c87d 26 _click_flag = 0;
25574069 0:d4d7e882c87d 27
25574069 0:d4d7e882c87d 28 }
25574069 0:d4d7e882c87d 29
25574069 0:d4d7e882c87d 30 Direction Joystick::get_direction()
25574069 0:d4d7e882c87d 31 {
25574069 0:d4d7e882c87d 32 float angle = get_angle(); // 0 to 360, -1 for centred
25574069 0:d4d7e882c87d 33
25574069 0:d4d7e882c87d 34 Direction d;
25574069 0:d4d7e882c87d 35 // partition 360 into segments and check which segment the angle is in
25574069 0:d4d7e882c87d 36 if (angle < 0.0f) {
25574069 0:d4d7e882c87d 37 d = CENTRE; // check for -1.0 angle
25574069 0:d4d7e882c87d 38 } else if (angle < 22.5f) { // then keep going in 45 degree increments
25574069 0:d4d7e882c87d 39 d = N;
25574069 0:d4d7e882c87d 40 } else if (angle < 67.5f) {
25574069 0:d4d7e882c87d 41 d = NE;
25574069 0:d4d7e882c87d 42 } else if (angle < 112.5f) {
25574069 0:d4d7e882c87d 43 d = E;
25574069 0:d4d7e882c87d 44 } else if (angle < 157.5f) {
25574069 0:d4d7e882c87d 45 d = SE;
25574069 0:d4d7e882c87d 46 } else if (angle < 202.5f) {
25574069 0:d4d7e882c87d 47 d = S;
25574069 0:d4d7e882c87d 48 } else if (angle < 247.5f) {
25574069 0:d4d7e882c87d 49 d = SW;
25574069 0:d4d7e882c87d 50 } else if (angle < 292.5f) {
25574069 0:d4d7e882c87d 51 d = W;
25574069 0:d4d7e882c87d 52 } else if (angle < 337.5f) {
25574069 0:d4d7e882c87d 53 d = NW;
25574069 0:d4d7e882c87d 54 } else {
25574069 0:d4d7e882c87d 55 d = N;
25574069 0:d4d7e882c87d 56 }
25574069 0:d4d7e882c87d 57
25574069 0:d4d7e882c87d 58 return d;
25574069 0:d4d7e882c87d 59 }
25574069 0:d4d7e882c87d 60
25574069 0:d4d7e882c87d 61 // this method gets the magnitude of the joystick movement
25574069 0:d4d7e882c87d 62 float Joystick::get_mag()
25574069 0:d4d7e882c87d 63 {
25574069 0:d4d7e882c87d 64 Polar p = get_polar();
25574069 0:d4d7e882c87d 65 return p.mag;
25574069 0:d4d7e882c87d 66 }
25574069 0:d4d7e882c87d 67
25574069 0:d4d7e882c87d 68 // this method gets the angle of joystick movement (0 to 360, 0 North)
25574069 0:d4d7e882c87d 69 float Joystick::get_angle()
25574069 0:d4d7e882c87d 70 {
25574069 0:d4d7e882c87d 71 Polar p = get_polar();
25574069 0:d4d7e882c87d 72 return p.angle;
25574069 0:d4d7e882c87d 73 }
25574069 0:d4d7e882c87d 74
25574069 0:d4d7e882c87d 75 // get raw joystick coordinate in range -1 to 1
25574069 0:d4d7e882c87d 76 // Direction (x,y)
25574069 0:d4d7e882c87d 77 // North (0,1)
25574069 0:d4d7e882c87d 78 // East (1,0)
25574069 0:d4d7e882c87d 79 // South (0,-1)
25574069 0:d4d7e882c87d 80 // West (-1,0)
25574069 0:d4d7e882c87d 81 Vector2D Joystick::get_coord()
25574069 0:d4d7e882c87d 82 {
25574069 0:d4d7e882c87d 83 // read() returns value in range 0.0 to 1.0 so is scaled and centre value
25574069 0:d4d7e882c87d 84 // substracted to get values in the range -1.0 to 1.0
25574069 0:d4d7e882c87d 85 float x = 2.0f*( horiz->read() - _x0 );
25574069 0:d4d7e882c87d 86 float y = 2.0f*( vert->read() - _y0 );
25574069 0:d4d7e882c87d 87
25574069 0:d4d7e882c87d 88 // Note: the x value here is inverted to ensure the positive x is to the
25574069 0:d4d7e882c87d 89 // right. This is simply due to how the potentiometer on the joystick
25574069 0:d4d7e882c87d 90 // I was using was connected up. It could have been corrected in hardware
25574069 0:d4d7e882c87d 91 // by swapping the power supply pins. Instead it is done in software so may
25574069 0:d4d7e882c87d 92 // need to be changed depending on your wiring setup
25574069 0:d4d7e882c87d 93
25574069 0:d4d7e882c87d 94 Vector2D coord = {-x,y};
25574069 0:d4d7e882c87d 95 return coord;
25574069 0:d4d7e882c87d 96 }
25574069 0:d4d7e882c87d 97
25574069 0:d4d7e882c87d 98 // This maps the raw x,y coord onto a circular grid.
25574069 0:d4d7e882c87d 99 // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html
25574069 0:d4d7e882c87d 100 Vector2D Joystick::get_mapped_coord()
25574069 0:d4d7e882c87d 101 {
25574069 0:d4d7e882c87d 102 Vector2D coord = get_coord();
25574069 0:d4d7e882c87d 103
25574069 0:d4d7e882c87d 104 // do the transformation
25574069 0:d4d7e882c87d 105 float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f);
25574069 0:d4d7e882c87d 106 float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f);
25574069 0:d4d7e882c87d 107
25574069 0:d4d7e882c87d 108 Vector2D mapped_coord = {x,y};
25574069 0:d4d7e882c87d 109 return mapped_coord;
25574069 0:d4d7e882c87d 110 }
25574069 0:d4d7e882c87d 111
25574069 0:d4d7e882c87d 112 // this function converts the mapped coordinates into polar form
25574069 0:d4d7e882c87d 113 Polar Joystick::get_polar()
25574069 0:d4d7e882c87d 114 {
25574069 0:d4d7e882c87d 115 // get the mapped coordinate
25574069 0:d4d7e882c87d 116 Vector2D coord = get_mapped_coord();
25574069 0:d4d7e882c87d 117
25574069 0:d4d7e882c87d 118 // at this point, 0 degrees (i.e. x-axis) will be defined to the East.
25574069 0:d4d7e882c87d 119 // We want 0 degrees to correspond to North and increase clockwise to 359
25574069 0:d4d7e882c87d 120 // like a compass heading, so we need to swap the axis and invert y
25574069 0:d4d7e882c87d 121 float x = coord.y;
25574069 0:d4d7e882c87d 122 float y = coord.x;
25574069 0:d4d7e882c87d 123
25574069 0:d4d7e882c87d 124 float mag = sqrt(x*x+y*y); // pythagoras
25574069 0:d4d7e882c87d 125 float angle = RAD2DEG*atan2(y,x);
25574069 0:d4d7e882c87d 126 // angle will be in range -180 to 180, so add 360 to negative angles to
25574069 0:d4d7e882c87d 127 // move to 0 to 360 range
25574069 0:d4d7e882c87d 128 if (angle < 0.0f) {
25574069 0:d4d7e882c87d 129 angle+=360.0f;
25574069 0:d4d7e882c87d 130 }
25574069 0:d4d7e882c87d 131
25574069 0:d4d7e882c87d 132 // the noise on the ADC causes the values of x and y to fluctuate slightly
25574069 0:d4d7e882c87d 133 // around the centred values. This causes the random angle values to get
25574069 0:d4d7e882c87d 134 // calculated when the joystick is centred and untouched. This is also when
25574069 0:d4d7e882c87d 135 // the magnitude is very small, so we can check for a small magnitude and then
25574069 0:d4d7e882c87d 136 // set the angle to -1. This will inform us when the angle is invalid and the
25574069 0:d4d7e882c87d 137 // joystick is centred
25574069 0:d4d7e882c87d 138
25574069 0:d4d7e882c87d 139 if (mag < TOL) {
25574069 0:d4d7e882c87d 140 mag = 0.0f;
25574069 0:d4d7e882c87d 141 angle = -1.0f;
25574069 0:d4d7e882c87d 142 }
25574069 0:d4d7e882c87d 143
25574069 0:d4d7e882c87d 144 Polar p = {mag,angle};
25574069 0:d4d7e882c87d 145 return p;
25574069 0:d4d7e882c87d 146 }
25574069 0:d4d7e882c87d 147
25574069 0:d4d7e882c87d 148 bool Joystick::button_pressed()
25574069 0:d4d7e882c87d 149 {
25574069 0:d4d7e882c87d 150 // ISR must have been triggered
25574069 0:d4d7e882c87d 151 if (_click_flag) {
25574069 0:d4d7e882c87d 152 _click_flag = 0; // clear flag
25574069 0:d4d7e882c87d 153 return true;
25574069 0:d4d7e882c87d 154 } else {
25574069 0:d4d7e882c87d 155 return false;
25574069 0:d4d7e882c87d 156 }
25574069 0:d4d7e882c87d 157 }
25574069 0:d4d7e882c87d 158
25574069 0:d4d7e882c87d 159 void Joystick::click_isr()
25574069 0:d4d7e882c87d 160 {
25574069 0:d4d7e882c87d 161 _click_flag = 1;
25574069 0:d4d7e882c87d 162 }
25574069 0:d4d7e882c87d 163