demo

Dependencies:   mbed

Committer:
sjuzyz
Date:
Mon Apr 27 13:53:33 2020 +0000
Revision:
4:7743a7670ae7
demo

Who changed what in which revision?

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