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