contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Fri May 03 16:57:52 2019 +0000
Revision:
39:822b66b1c935
Final Submission. I have read and agreed with Statement of Academic Integrity

Who changed what in which revision?

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