James Heavey / Mbed 2 deprecated 2665-Breakout-Game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

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