A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Sun May 05 18:04:43 2019 +0000
Revision:
30:ec915d24d3e9
Parent:
Gamepad/Gamepad.cpp@22:7abf4581bc9b
Title Screen done;

Who changed what in which revision?

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