Josh Davy / Mbed OS Flip_OS_5

Dependencies:   el17jd

Committer:
joshdavy
Date:
Tue Mar 12 12:38:34 2019 +0000
Revision:
0:4916a63a6cbf
initial commit

Who changed what in which revision?

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