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