Doxyjen of the Bird.h

Dependencies:   mbed N5110

Committer:
Wuuu
Date:
Sun May 05 16:27:50 2019 +0000
Revision:
0:0aea7b9ba421
Doxyjen of the Bird.h

Who changed what in which revision?

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