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