Zikang Qian / Mbed 2 deprecated el17z2q

Dependencies:   mbed

Fork of el17z2q by ELEC2645 (2017/18)

Committer:
yzjdxl
Date:
Tue Apr 17 08:43:38 2018 +0000
Revision:
0:f707ce1010fa
initial commmit

Who changed what in which revision?

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