test

Dependencies:   mbed FXOS8700CQ

Committer:
Neowless
Date:
Fri May 15 20:36:00 2020 +0000
Revision:
4:c7dc43515215
Parent:
1:48b0bf0bcda8
test;

Who changed what in which revision?

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