Ellis Blackford Stroud 201155309

Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Thu May 09 12:01:42 2019 +0000
Revision:
17:98127ac75195
Parent:
0:c6ceddb241df
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellisbhastroud 0:c6ceddb241df 1 #include "Gamepad.h"
ellisbhastroud 0:c6ceddb241df 2
ellisbhastroud 0:c6ceddb241df 3 #include "mbed.h"
ellisbhastroud 0:c6ceddb241df 4
ellisbhastroud 0:c6ceddb241df 5 //////////// constructor/destructor ////////////
ellisbhastroud 0:c6ceddb241df 6 Gamepad::Gamepad()
ellisbhastroud 0:c6ceddb241df 7 :
ellisbhastroud 0:c6ceddb241df 8 _led1(new PwmOut(PTA1)),
ellisbhastroud 0:c6ceddb241df 9 _led2(new PwmOut(PTA2)),
ellisbhastroud 0:c6ceddb241df 10 _led3(new PwmOut(PTC2)),
ellisbhastroud 0:c6ceddb241df 11 _led4(new PwmOut(PTC3)),
ellisbhastroud 0:c6ceddb241df 12 _led5(new PwmOut(PTC4)),
ellisbhastroud 0:c6ceddb241df 13 _led6(new PwmOut(PTD3)),
ellisbhastroud 0:c6ceddb241df 14
ellisbhastroud 0:c6ceddb241df 15 _button_A(new InterruptIn(PTB9)),
ellisbhastroud 0:c6ceddb241df 16 _button_B(new InterruptIn(PTD0)),
ellisbhastroud 0:c6ceddb241df 17 _button_X(new InterruptIn(PTC17)),
ellisbhastroud 0:c6ceddb241df 18 _button_Y(new InterruptIn(PTC12)),
ellisbhastroud 0:c6ceddb241df 19 _button_L(new InterruptIn(PTB18)),
ellisbhastroud 0:c6ceddb241df 20 _button_R(new InterruptIn(PTB3)),
ellisbhastroud 0:c6ceddb241df 21 _button_back(new InterruptIn(PTB19)),
ellisbhastroud 0:c6ceddb241df 22 _button_start(new InterruptIn(PTC5)),
ellisbhastroud 0:c6ceddb241df 23 _button_joystick(new InterruptIn(PTC16)),
ellisbhastroud 0:c6ceddb241df 24
ellisbhastroud 0:c6ceddb241df 25 _vert(new AnalogIn(PTB10)),
ellisbhastroud 0:c6ceddb241df 26 _horiz(new AnalogIn(PTB11)),
ellisbhastroud 0:c6ceddb241df 27
ellisbhastroud 0:c6ceddb241df 28 _buzzer(new PwmOut(PTC10)),
ellisbhastroud 0:c6ceddb241df 29 _pot(new AnalogIn(PTB2)),
ellisbhastroud 0:c6ceddb241df 30
ellisbhastroud 0:c6ceddb241df 31 _timeout(new Timeout()),
ellisbhastroud 0:c6ceddb241df 32
ellisbhastroud 0:c6ceddb241df 33 _event_state(0),
ellisbhastroud 0:c6ceddb241df 34
ellisbhastroud 0:c6ceddb241df 35 _x0(0),
ellisbhastroud 0:c6ceddb241df 36 _y0(0)
ellisbhastroud 0:c6ceddb241df 37 {}
ellisbhastroud 0:c6ceddb241df 38
ellisbhastroud 0:c6ceddb241df 39 Gamepad::~Gamepad()
ellisbhastroud 0:c6ceddb241df 40 {
ellisbhastroud 0:c6ceddb241df 41 delete _led1,_led2,_led3,_led4,_led5,_led6;
ellisbhastroud 0:c6ceddb241df 42 delete _button_A,_button_B,_button_joystick,_vert,_horiz;
ellisbhastroud 0:c6ceddb241df 43 delete _button_X,_button_Y,_button_back,_button_start;
ellisbhastroud 0:c6ceddb241df 44 delete _button_L,_button_R, _buzzer, _pot, _timeout;
ellisbhastroud 0:c6ceddb241df 45 }
ellisbhastroud 0:c6ceddb241df 46
ellisbhastroud 0:c6ceddb241df 47 ///////////////// public methods /////////////////
ellisbhastroud 0:c6ceddb241df 48
ellisbhastroud 0:c6ceddb241df 49 void Gamepad::init()
ellisbhastroud 0:c6ceddb241df 50 {
ellisbhastroud 0:c6ceddb241df 51 leds_off();
ellisbhastroud 0:c6ceddb241df 52 init_buttons();
ellisbhastroud 0:c6ceddb241df 53
ellisbhastroud 0:c6ceddb241df 54 // read centred values of joystick
ellisbhastroud 0:c6ceddb241df 55 _x0 = _horiz->read();
ellisbhastroud 0:c6ceddb241df 56 _y0 = _vert->read();
ellisbhastroud 0:c6ceddb241df 57
ellisbhastroud 0:c6ceddb241df 58 // clear all flags
ellisbhastroud 0:c6ceddb241df 59 _event_state = 0;
ellisbhastroud 0:c6ceddb241df 60 }
ellisbhastroud 0:c6ceddb241df 61
ellisbhastroud 0:c6ceddb241df 62 void Gamepad::leds_off()
ellisbhastroud 0:c6ceddb241df 63 {
ellisbhastroud 0:c6ceddb241df 64 leds(0.0);
ellisbhastroud 0:c6ceddb241df 65 }
ellisbhastroud 0:c6ceddb241df 66
ellisbhastroud 0:c6ceddb241df 67 void Gamepad::leds_on()
ellisbhastroud 0:c6ceddb241df 68 {
ellisbhastroud 0:c6ceddb241df 69 leds(1.0);
ellisbhastroud 0:c6ceddb241df 70 }
ellisbhastroud 0:c6ceddb241df 71
ellisbhastroud 0:c6ceddb241df 72 void Gamepad::leds(float val) const
ellisbhastroud 0:c6ceddb241df 73 {
ellisbhastroud 0:c6ceddb241df 74 if (val < 0.0f) {
ellisbhastroud 0:c6ceddb241df 75 val = 0.0f;
ellisbhastroud 0:c6ceddb241df 76 }
ellisbhastroud 0:c6ceddb241df 77 if (val > 1.0f) {
ellisbhastroud 0:c6ceddb241df 78 val = 1.0f;
ellisbhastroud 0:c6ceddb241df 79 }
ellisbhastroud 0:c6ceddb241df 80
ellisbhastroud 0:c6ceddb241df 81 // leds are active-low, so subtract from 1.0
ellisbhastroud 0:c6ceddb241df 82 // 0.0 corresponds to fully-off, 1.0 to fully-on
ellisbhastroud 0:c6ceddb241df 83 val = 1.0f - val;
ellisbhastroud 0:c6ceddb241df 84
ellisbhastroud 0:c6ceddb241df 85 _led1->write(val);
ellisbhastroud 0:c6ceddb241df 86 _led2->write(val);
ellisbhastroud 0:c6ceddb241df 87 _led3->write(val);
ellisbhastroud 0:c6ceddb241df 88 _led4->write(val);
ellisbhastroud 0:c6ceddb241df 89 _led5->write(val);
ellisbhastroud 0:c6ceddb241df 90 _led6->write(val);
ellisbhastroud 0:c6ceddb241df 91 }
ellisbhastroud 0:c6ceddb241df 92
ellisbhastroud 0:c6ceddb241df 93 void Gamepad::led(int n,float val) const
ellisbhastroud 0:c6ceddb241df 94 {
ellisbhastroud 0:c6ceddb241df 95 // ensure they are within vlaid range
ellisbhastroud 0:c6ceddb241df 96 if (val < 0.0f) {
ellisbhastroud 0:c6ceddb241df 97 val = 0.0f;
ellisbhastroud 0:c6ceddb241df 98 }
ellisbhastroud 0:c6ceddb241df 99 if (val > 1.0f) {
ellisbhastroud 0:c6ceddb241df 100 val = 1.0f;
ellisbhastroud 0:c6ceddb241df 101 }
ellisbhastroud 0:c6ceddb241df 102
ellisbhastroud 0:c6ceddb241df 103 switch (n) {
ellisbhastroud 0:c6ceddb241df 104
ellisbhastroud 0:c6ceddb241df 105 // check for valid LED number and set value
ellisbhastroud 0:c6ceddb241df 106
ellisbhastroud 0:c6ceddb241df 107 case 1:
ellisbhastroud 0:c6ceddb241df 108 _led1->write(1.0f-val); // active-low so subtract from 1
ellisbhastroud 0:c6ceddb241df 109 break;
ellisbhastroud 0:c6ceddb241df 110 case 2:
ellisbhastroud 0:c6ceddb241df 111 _led2->write(1.0f-val); // active-low so subtract from 1
ellisbhastroud 0:c6ceddb241df 112 break;
ellisbhastroud 0:c6ceddb241df 113 case 3:
ellisbhastroud 0:c6ceddb241df 114 _led3->write(1.0f-val); // active-low so subtract from 1
ellisbhastroud 0:c6ceddb241df 115 break;
ellisbhastroud 0:c6ceddb241df 116 case 4:
ellisbhastroud 0:c6ceddb241df 117 _led4->write(1.0f-val); // active-low so subtract from 1
ellisbhastroud 0:c6ceddb241df 118 break;
ellisbhastroud 0:c6ceddb241df 119 case 5:
ellisbhastroud 0:c6ceddb241df 120 _led5->write(1.0f-val); // active-low so subtract from 1
ellisbhastroud 0:c6ceddb241df 121 break;
ellisbhastroud 0:c6ceddb241df 122 case 6:
ellisbhastroud 0:c6ceddb241df 123 _led6->write(1.0f-val); // active-low so subtract from 1
ellisbhastroud 0:c6ceddb241df 124 break;
ellisbhastroud 0:c6ceddb241df 125
ellisbhastroud 0:c6ceddb241df 126 }
ellisbhastroud 0:c6ceddb241df 127 }
ellisbhastroud 0:c6ceddb241df 128
ellisbhastroud 0:c6ceddb241df 129 float Gamepad::read_pot() const
ellisbhastroud 0:c6ceddb241df 130 {
ellisbhastroud 0:c6ceddb241df 131 return _pot->read();
ellisbhastroud 0:c6ceddb241df 132 }
ellisbhastroud 0:c6ceddb241df 133
ellisbhastroud 0:c6ceddb241df 134 void Gamepad::tone(float frequency, float duration)
ellisbhastroud 0:c6ceddb241df 135 {
ellisbhastroud 0:c6ceddb241df 136 _buzzer->period(1.0f/frequency);
ellisbhastroud 0:c6ceddb241df 137 _buzzer->write(0.5); // 50% duty cycle - square wave
ellisbhastroud 0:c6ceddb241df 138 _timeout->attach(callback(this, &Gamepad::tone_off), duration );
ellisbhastroud 0:c6ceddb241df 139 }
ellisbhastroud 0:c6ceddb241df 140
ellisbhastroud 0:c6ceddb241df 141 bool Gamepad::check_event(GamepadEvent const id)
ellisbhastroud 0:c6ceddb241df 142 {
ellisbhastroud 0:c6ceddb241df 143 // Check whether event flag is set
ellisbhastroud 0:c6ceddb241df 144 if (_event_state[id]) {
ellisbhastroud 0:c6ceddb241df 145 _event_state.reset(id); // clear flag
ellisbhastroud 0:c6ceddb241df 146 return true;
ellisbhastroud 0:c6ceddb241df 147 } else {
ellisbhastroud 0:c6ceddb241df 148 return false;
ellisbhastroud 0:c6ceddb241df 149 }
ellisbhastroud 0:c6ceddb241df 150 }
ellisbhastroud 0:c6ceddb241df 151
ellisbhastroud 0:c6ceddb241df 152 // this method gets the magnitude of the joystick movement
ellisbhastroud 0:c6ceddb241df 153 float Gamepad::get_mag()
ellisbhastroud 0:c6ceddb241df 154 {
ellisbhastroud 0:c6ceddb241df 155 Polar p = get_polar();
ellisbhastroud 0:c6ceddb241df 156 return p.mag;
ellisbhastroud 0:c6ceddb241df 157 }
ellisbhastroud 0:c6ceddb241df 158
ellisbhastroud 0:c6ceddb241df 159 // this method gets the angle of joystick movement (0 to 360, 0 North)
ellisbhastroud 0:c6ceddb241df 160 float Gamepad::get_angle()
ellisbhastroud 0:c6ceddb241df 161 {
ellisbhastroud 0:c6ceddb241df 162 Polar p = get_polar();
ellisbhastroud 0:c6ceddb241df 163 return p.angle;
ellisbhastroud 0:c6ceddb241df 164 }
ellisbhastroud 0:c6ceddb241df 165
ellisbhastroud 0:c6ceddb241df 166 Direction Gamepad::get_direction()
ellisbhastroud 0:c6ceddb241df 167 {
ellisbhastroud 0:c6ceddb241df 168 float angle = get_angle(); // 0 to 360, -1 for centred
ellisbhastroud 0:c6ceddb241df 169
ellisbhastroud 0:c6ceddb241df 170 Direction d;
ellisbhastroud 0:c6ceddb241df 171 // partition 360 into segments and check which segment the angle is in
ellisbhastroud 0:c6ceddb241df 172 if (angle < 0.0f) {
ellisbhastroud 0:c6ceddb241df 173 d = CENTRE; // check for -1.0 angle
ellisbhastroud 0:c6ceddb241df 174 } else if (angle < 22.5f) { // then keep going in 45 degree increments
ellisbhastroud 0:c6ceddb241df 175 d = N;
ellisbhastroud 0:c6ceddb241df 176 } else if (angle < 67.5f) {
ellisbhastroud 0:c6ceddb241df 177 d = NE;
ellisbhastroud 0:c6ceddb241df 178 } else if (angle < 112.5f) {
ellisbhastroud 0:c6ceddb241df 179 d = E;
ellisbhastroud 0:c6ceddb241df 180 } else if (angle < 157.5f) {
ellisbhastroud 0:c6ceddb241df 181 d = SE;
ellisbhastroud 0:c6ceddb241df 182 } else if (angle < 202.5f) {
ellisbhastroud 0:c6ceddb241df 183 d = S;
ellisbhastroud 0:c6ceddb241df 184 } else if (angle < 247.5f) {
ellisbhastroud 0:c6ceddb241df 185 d = SW;
ellisbhastroud 0:c6ceddb241df 186 } else if (angle < 292.5f) {
ellisbhastroud 0:c6ceddb241df 187 d = W;
ellisbhastroud 0:c6ceddb241df 188 } else if (angle < 337.5f) {
ellisbhastroud 0:c6ceddb241df 189 d = NW;
ellisbhastroud 0:c6ceddb241df 190 } else {
ellisbhastroud 0:c6ceddb241df 191 d = N;
ellisbhastroud 0:c6ceddb241df 192 }
ellisbhastroud 0:c6ceddb241df 193
ellisbhastroud 0:c6ceddb241df 194 return d;
ellisbhastroud 0:c6ceddb241df 195 }
ellisbhastroud 0:c6ceddb241df 196
ellisbhastroud 0:c6ceddb241df 197 ///////////////////// private methods ////////////////////////
ellisbhastroud 0:c6ceddb241df 198
ellisbhastroud 0:c6ceddb241df 199 void Gamepad::tone_off()
ellisbhastroud 0:c6ceddb241df 200 {
ellisbhastroud 0:c6ceddb241df 201 // called after timeout
ellisbhastroud 0:c6ceddb241df 202 _buzzer->write(0.0);
ellisbhastroud 0:c6ceddb241df 203 }
ellisbhastroud 0:c6ceddb241df 204
ellisbhastroud 0:c6ceddb241df 205 void Gamepad::init_buttons()
ellisbhastroud 0:c6ceddb241df 206 {
ellisbhastroud 0:c6ceddb241df 207 // turn on pull-downs as other side of button is connected to 3V3
ellisbhastroud 0:c6ceddb241df 208 // button is 0 when not pressed and 1 when pressed
ellisbhastroud 0:c6ceddb241df 209 _button_A->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 210 _button_B->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 211 _button_X->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 212 _button_Y->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 213 _button_back->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 214 _button_start->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 215 _button_L->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 216 _button_R->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 217 _button_joystick->mode(PullDown);
ellisbhastroud 0:c6ceddb241df 218 // therefore setup rising edge interrupts
ellisbhastroud 0:c6ceddb241df 219 _button_A->rise(callback(this,&Gamepad::a_isr));
ellisbhastroud 0:c6ceddb241df 220 _button_B->rise(callback(this,&Gamepad::b_isr));
ellisbhastroud 0:c6ceddb241df 221 _button_X->rise(callback(this,&Gamepad::x_isr));
ellisbhastroud 0:c6ceddb241df 222 _button_Y->rise(callback(this,&Gamepad::y_isr));
ellisbhastroud 0:c6ceddb241df 223 _button_L->rise(callback(this,&Gamepad::l_isr));
ellisbhastroud 0:c6ceddb241df 224 _button_R->rise(callback(this,&Gamepad::r_isr));
ellisbhastroud 0:c6ceddb241df 225 _button_start->rise(callback(this,&Gamepad::start_isr));
ellisbhastroud 0:c6ceddb241df 226 _button_back->rise(callback(this,&Gamepad::back_isr));
ellisbhastroud 0:c6ceddb241df 227 _button_joystick->rise(callback(this,&Gamepad::joy_isr));
ellisbhastroud 0:c6ceddb241df 228 }
ellisbhastroud 0:c6ceddb241df 229
ellisbhastroud 0:c6ceddb241df 230 // button interrupts ISRs
ellisbhastroud 0:c6ceddb241df 231 // Each of these simply sets the appropriate event bit in the _event_state
ellisbhastroud 0:c6ceddb241df 232 // variable
ellisbhastroud 0:c6ceddb241df 233 void Gamepad::a_isr()
ellisbhastroud 0:c6ceddb241df 234 {
ellisbhastroud 0:c6ceddb241df 235 _event_state.set(A_PRESSED);
ellisbhastroud 0:c6ceddb241df 236 }
ellisbhastroud 0:c6ceddb241df 237 void Gamepad::b_isr()
ellisbhastroud 0:c6ceddb241df 238 {
ellisbhastroud 0:c6ceddb241df 239 _event_state.set(B_PRESSED);
ellisbhastroud 0:c6ceddb241df 240 }
ellisbhastroud 0:c6ceddb241df 241 void Gamepad::x_isr()
ellisbhastroud 0:c6ceddb241df 242 {
ellisbhastroud 0:c6ceddb241df 243 _event_state.set(X_PRESSED);
ellisbhastroud 0:c6ceddb241df 244 }
ellisbhastroud 0:c6ceddb241df 245 void Gamepad::y_isr()
ellisbhastroud 0:c6ceddb241df 246 {
ellisbhastroud 0:c6ceddb241df 247 _event_state.set(Y_PRESSED);
ellisbhastroud 0:c6ceddb241df 248 }
ellisbhastroud 0:c6ceddb241df 249 void Gamepad::l_isr()
ellisbhastroud 0:c6ceddb241df 250 {
ellisbhastroud 0:c6ceddb241df 251 _event_state.set(L_PRESSED);
ellisbhastroud 0:c6ceddb241df 252 }
ellisbhastroud 0:c6ceddb241df 253 void Gamepad::r_isr()
ellisbhastroud 0:c6ceddb241df 254 {
ellisbhastroud 0:c6ceddb241df 255 _event_state.set(R_PRESSED);
ellisbhastroud 0:c6ceddb241df 256 }
ellisbhastroud 0:c6ceddb241df 257 void Gamepad::back_isr()
ellisbhastroud 0:c6ceddb241df 258 {
ellisbhastroud 0:c6ceddb241df 259 _event_state.set(BACK_PRESSED);
ellisbhastroud 0:c6ceddb241df 260 }
ellisbhastroud 0:c6ceddb241df 261 void Gamepad::start_isr()
ellisbhastroud 0:c6ceddb241df 262 {
ellisbhastroud 0:c6ceddb241df 263 _event_state.set(START_PRESSED);
ellisbhastroud 0:c6ceddb241df 264 }
ellisbhastroud 0:c6ceddb241df 265 void Gamepad::joy_isr()
ellisbhastroud 0:c6ceddb241df 266 {
ellisbhastroud 0:c6ceddb241df 267 _event_state.set(JOY_PRESSED);
ellisbhastroud 0:c6ceddb241df 268 }
ellisbhastroud 0:c6ceddb241df 269
ellisbhastroud 0:c6ceddb241df 270 // get raw joystick coordinate in range -1 to 1
ellisbhastroud 0:c6ceddb241df 271 // Direction (x,y)
ellisbhastroud 0:c6ceddb241df 272 // North (0,1)
ellisbhastroud 0:c6ceddb241df 273 // East (1,0)
ellisbhastroud 0:c6ceddb241df 274 // South (0,-1)
ellisbhastroud 0:c6ceddb241df 275 // West (-1,0)
ellisbhastroud 0:c6ceddb241df 276 Vector2D Gamepad::get_coord()
ellisbhastroud 0:c6ceddb241df 277 {
ellisbhastroud 0:c6ceddb241df 278 // read() returns value in range 0.0 to 1.0 so is scaled and centre value
ellisbhastroud 0:c6ceddb241df 279 // substracted to get values in the range -1.0 to 1.0
ellisbhastroud 0:c6ceddb241df 280 float x = 2.0f*( _horiz->read() - _x0 );
ellisbhastroud 0:c6ceddb241df 281 float y = 2.0f*( _vert->read() - _y0 );
ellisbhastroud 0:c6ceddb241df 282
ellisbhastroud 0:c6ceddb241df 283 // Note: the x value here is inverted to ensure the positive x is to the
ellisbhastroud 0:c6ceddb241df 284 // right. This is simply due to how the potentiometer on the joystick
ellisbhastroud 0:c6ceddb241df 285 // I was using was connected up. It could have been corrected in hardware
ellisbhastroud 0:c6ceddb241df 286 // by swapping the power supply pins. Instead it is done in software so may
ellisbhastroud 0:c6ceddb241df 287 // need to be changed depending on your wiring setup
ellisbhastroud 0:c6ceddb241df 288
ellisbhastroud 0:c6ceddb241df 289 Vector2D coord = {-x,y};
ellisbhastroud 0:c6ceddb241df 290 return coord;
ellisbhastroud 0:c6ceddb241df 291 }
ellisbhastroud 0:c6ceddb241df 292
ellisbhastroud 0:c6ceddb241df 293 // This maps the raw x,y coord onto a circular grid.
ellisbhastroud 0:c6ceddb241df 294 // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html
ellisbhastroud 0:c6ceddb241df 295 Vector2D Gamepad::get_mapped_coord()
ellisbhastroud 0:c6ceddb241df 296 {
ellisbhastroud 0:c6ceddb241df 297 Vector2D coord = get_coord();
ellisbhastroud 0:c6ceddb241df 298
ellisbhastroud 0:c6ceddb241df 299 // do the transformation
ellisbhastroud 0:c6ceddb241df 300 float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f);
ellisbhastroud 0:c6ceddb241df 301 float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f);
ellisbhastroud 0:c6ceddb241df 302
ellisbhastroud 0:c6ceddb241df 303 Vector2D mapped_coord = {x,y};
ellisbhastroud 0:c6ceddb241df 304 return mapped_coord;
ellisbhastroud 0:c6ceddb241df 305 }
ellisbhastroud 0:c6ceddb241df 306
ellisbhastroud 0:c6ceddb241df 307 // this function converts the mapped coordinates into polar form
ellisbhastroud 0:c6ceddb241df 308 Polar Gamepad::get_polar()
ellisbhastroud 0:c6ceddb241df 309 {
ellisbhastroud 0:c6ceddb241df 310 // get the mapped coordinate
ellisbhastroud 0:c6ceddb241df 311 Vector2D coord = get_mapped_coord();
ellisbhastroud 0:c6ceddb241df 312
ellisbhastroud 0:c6ceddb241df 313 // at this point, 0 degrees (i.e. x-axis) will be defined to the East.
ellisbhastroud 0:c6ceddb241df 314 // We want 0 degrees to correspond to North and increase clockwise to 359
ellisbhastroud 0:c6ceddb241df 315 // like a compass heading, so we need to swap the axis and invert y
ellisbhastroud 0:c6ceddb241df 316 float x = coord.y;
ellisbhastroud 0:c6ceddb241df 317 float y = coord.x;
ellisbhastroud 0:c6ceddb241df 318
ellisbhastroud 0:c6ceddb241df 319 float mag = sqrt(x*x+y*y); // pythagoras
ellisbhastroud 0:c6ceddb241df 320 float angle = RAD2DEG*atan2(y,x);
ellisbhastroud 0:c6ceddb241df 321 // angle will be in range -180 to 180, so add 360 to negative angles to
ellisbhastroud 0:c6ceddb241df 322 // move to 0 to 360 range
ellisbhastroud 0:c6ceddb241df 323 if (angle < 0.0f) {
ellisbhastroud 0:c6ceddb241df 324 angle+=360.0f;
ellisbhastroud 0:c6ceddb241df 325 }
ellisbhastroud 0:c6ceddb241df 326
ellisbhastroud 0:c6ceddb241df 327 // the noise on the ADC causes the values of x and y to fluctuate slightly
ellisbhastroud 0:c6ceddb241df 328 // around the centred values. This causes the random angle values to get
ellisbhastroud 0:c6ceddb241df 329 // calculated when the joystick is centred and untouched. This is also when
ellisbhastroud 0:c6ceddb241df 330 // the magnitude is very small, so we can check for a small magnitude and then
ellisbhastroud 0:c6ceddb241df 331 // set the angle to -1. This will inform us when the angle is invalid and the
ellisbhastroud 0:c6ceddb241df 332 // joystick is centred
ellisbhastroud 0:c6ceddb241df 333
ellisbhastroud 0:c6ceddb241df 334 if (mag < TOL) {
ellisbhastroud 0:c6ceddb241df 335 mag = 0.0f;
ellisbhastroud 0:c6ceddb241df 336 angle = -1.0f;
ellisbhastroud 0:c6ceddb241df 337 }
ellisbhastroud 0:c6ceddb241df 338
ellisbhastroud 0:c6ceddb241df 339 Polar p = {mag,angle};
ellisbhastroud 0:c6ceddb241df 340 return p;
ellisbhastroud 0:c6ceddb241df 341 }