200943373MAZE

Dependencies:   mbed

Fork of 200943373MAZE by hongyun AHN

Committer:
hongyunAHN
Date:
Thu May 04 14:08:31 2017 +0000
Revision:
1:bd92ef8d00ac
200943373  hongyun AHN

Who changed what in which revision?

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