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