publish my project

Dependencies:   mbed

Committer:
dongyuhu
Date:
Wed Apr 29 10:51:02 2020 +0000
Revision:
7:4b92c1ee6231
Parent:
0:9e95a5ef2659
publish

Who changed what in which revision?

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