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