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