James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 09:02:23 2019 +0000
Revision:
133:05bb8de3c630
Parent:
0:7d4d2023ed9c
Added reset_flags function to gamepad to fix the problem of button inputs carrying through to different menus

Who changed what in which revision?

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