James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Apr 16 18:58:18 2019 +0000
Revision:
0:7d4d2023ed9c
Child:
133:05bb8de3c630
Game changed to breakout form space invaders (new folder) template initialised, edited paddle, edited direction of movement, edited collision boundaries

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 0:7d4d2023ed9c 152 // this method gets the magnitude of the joystick movement
jamesheavey 0:7d4d2023ed9c 153 float Gamepad::get_mag()
jamesheavey 0:7d4d2023ed9c 154 {
jamesheavey 0:7d4d2023ed9c 155 Polar p = get_polar();
jamesheavey 0:7d4d2023ed9c 156 return p.mag;
jamesheavey 0:7d4d2023ed9c 157 }
jamesheavey 0:7d4d2023ed9c 158
jamesheavey 0:7d4d2023ed9c 159 // this method gets the angle of joystick movement (0 to 360, 0 North)
jamesheavey 0:7d4d2023ed9c 160 float Gamepad::get_angle()
jamesheavey 0:7d4d2023ed9c 161 {
jamesheavey 0:7d4d2023ed9c 162 Polar p = get_polar();
jamesheavey 0:7d4d2023ed9c 163 return p.angle;
jamesheavey 0:7d4d2023ed9c 164 }
jamesheavey 0:7d4d2023ed9c 165
jamesheavey 0:7d4d2023ed9c 166 Direction Gamepad::get_direction()
jamesheavey 0:7d4d2023ed9c 167 {
jamesheavey 0:7d4d2023ed9c 168 float angle = get_angle(); // 0 to 360, -1 for centred
jamesheavey 0:7d4d2023ed9c 169
jamesheavey 0:7d4d2023ed9c 170 Direction d;
jamesheavey 0:7d4d2023ed9c 171 // partition 360 into segments and check which segment the angle is in
jamesheavey 0:7d4d2023ed9c 172 if (angle < 0.0f) {
jamesheavey 0:7d4d2023ed9c 173 d = CENTRE; // check for -1.0 angle
jamesheavey 0:7d4d2023ed9c 174 } else if (angle < 22.5f) { // then keep going in 45 degree increments
jamesheavey 0:7d4d2023ed9c 175 d = N;
jamesheavey 0:7d4d2023ed9c 176 } else if (angle < 67.5f) {
jamesheavey 0:7d4d2023ed9c 177 d = NE;
jamesheavey 0:7d4d2023ed9c 178 } else if (angle < 112.5f) {
jamesheavey 0:7d4d2023ed9c 179 d = E;
jamesheavey 0:7d4d2023ed9c 180 } else if (angle < 157.5f) {
jamesheavey 0:7d4d2023ed9c 181 d = SE;
jamesheavey 0:7d4d2023ed9c 182 } else if (angle < 202.5f) {
jamesheavey 0:7d4d2023ed9c 183 d = S;
jamesheavey 0:7d4d2023ed9c 184 } else if (angle < 247.5f) {
jamesheavey 0:7d4d2023ed9c 185 d = SW;
jamesheavey 0:7d4d2023ed9c 186 } else if (angle < 292.5f) {
jamesheavey 0:7d4d2023ed9c 187 d = W;
jamesheavey 0:7d4d2023ed9c 188 } else if (angle < 337.5f) {
jamesheavey 0:7d4d2023ed9c 189 d = NW;
jamesheavey 0:7d4d2023ed9c 190 } else {
jamesheavey 0:7d4d2023ed9c 191 d = N;
jamesheavey 0:7d4d2023ed9c 192 }
jamesheavey 0:7d4d2023ed9c 193
jamesheavey 0:7d4d2023ed9c 194 return d;
jamesheavey 0:7d4d2023ed9c 195 }
jamesheavey 0:7d4d2023ed9c 196
jamesheavey 0:7d4d2023ed9c 197 ///////////////////// private methods ////////////////////////
jamesheavey 0:7d4d2023ed9c 198
jamesheavey 0:7d4d2023ed9c 199 void Gamepad::tone_off()
jamesheavey 0:7d4d2023ed9c 200 {
jamesheavey 0:7d4d2023ed9c 201 // called after timeout
jamesheavey 0:7d4d2023ed9c 202 _buzzer->write(0.0);
jamesheavey 0:7d4d2023ed9c 203 }
jamesheavey 0:7d4d2023ed9c 204
jamesheavey 0:7d4d2023ed9c 205 void Gamepad::init_buttons()
jamesheavey 0:7d4d2023ed9c 206 {
jamesheavey 0:7d4d2023ed9c 207 // turn on pull-downs as other side of button is connected to 3V3
jamesheavey 0:7d4d2023ed9c 208 // button is 0 when not pressed and 1 when pressed
jamesheavey 0:7d4d2023ed9c 209 _button_A->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 210 _button_B->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 211 _button_X->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 212 _button_Y->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 213 _button_back->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 214 _button_start->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 215 _button_L->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 216 _button_R->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 217 _button_joystick->mode(PullDown);
jamesheavey 0:7d4d2023ed9c 218 // therefore setup rising edge interrupts
jamesheavey 0:7d4d2023ed9c 219 _button_A->rise(callback(this,&Gamepad::a_isr));
jamesheavey 0:7d4d2023ed9c 220 _button_B->rise(callback(this,&Gamepad::b_isr));
jamesheavey 0:7d4d2023ed9c 221 _button_X->rise(callback(this,&Gamepad::x_isr));
jamesheavey 0:7d4d2023ed9c 222 _button_Y->rise(callback(this,&Gamepad::y_isr));
jamesheavey 0:7d4d2023ed9c 223 _button_L->rise(callback(this,&Gamepad::l_isr));
jamesheavey 0:7d4d2023ed9c 224 _button_R->rise(callback(this,&Gamepad::r_isr));
jamesheavey 0:7d4d2023ed9c 225 _button_start->rise(callback(this,&Gamepad::start_isr));
jamesheavey 0:7d4d2023ed9c 226 _button_back->rise(callback(this,&Gamepad::back_isr));
jamesheavey 0:7d4d2023ed9c 227 _button_joystick->rise(callback(this,&Gamepad::joy_isr));
jamesheavey 0:7d4d2023ed9c 228 }
jamesheavey 0:7d4d2023ed9c 229
jamesheavey 0:7d4d2023ed9c 230 // button interrupts ISRs
jamesheavey 0:7d4d2023ed9c 231 // Each of these simply sets the appropriate event bit in the _event_state
jamesheavey 0:7d4d2023ed9c 232 // variable
jamesheavey 0:7d4d2023ed9c 233 void Gamepad::a_isr()
jamesheavey 0:7d4d2023ed9c 234 {
jamesheavey 0:7d4d2023ed9c 235 _event_state.set(A_PRESSED);
jamesheavey 0:7d4d2023ed9c 236 }
jamesheavey 0:7d4d2023ed9c 237 void Gamepad::b_isr()
jamesheavey 0:7d4d2023ed9c 238 {
jamesheavey 0:7d4d2023ed9c 239 _event_state.set(B_PRESSED);
jamesheavey 0:7d4d2023ed9c 240 }
jamesheavey 0:7d4d2023ed9c 241 void Gamepad::x_isr()
jamesheavey 0:7d4d2023ed9c 242 {
jamesheavey 0:7d4d2023ed9c 243 _event_state.set(X_PRESSED);
jamesheavey 0:7d4d2023ed9c 244 }
jamesheavey 0:7d4d2023ed9c 245 void Gamepad::y_isr()
jamesheavey 0:7d4d2023ed9c 246 {
jamesheavey 0:7d4d2023ed9c 247 _event_state.set(Y_PRESSED);
jamesheavey 0:7d4d2023ed9c 248 }
jamesheavey 0:7d4d2023ed9c 249 void Gamepad::l_isr()
jamesheavey 0:7d4d2023ed9c 250 {
jamesheavey 0:7d4d2023ed9c 251 _event_state.set(L_PRESSED);
jamesheavey 0:7d4d2023ed9c 252 }
jamesheavey 0:7d4d2023ed9c 253 void Gamepad::r_isr()
jamesheavey 0:7d4d2023ed9c 254 {
jamesheavey 0:7d4d2023ed9c 255 _event_state.set(R_PRESSED);
jamesheavey 0:7d4d2023ed9c 256 }
jamesheavey 0:7d4d2023ed9c 257 void Gamepad::back_isr()
jamesheavey 0:7d4d2023ed9c 258 {
jamesheavey 0:7d4d2023ed9c 259 _event_state.set(BACK_PRESSED);
jamesheavey 0:7d4d2023ed9c 260 }
jamesheavey 0:7d4d2023ed9c 261 void Gamepad::start_isr()
jamesheavey 0:7d4d2023ed9c 262 {
jamesheavey 0:7d4d2023ed9c 263 _event_state.set(START_PRESSED);
jamesheavey 0:7d4d2023ed9c 264 }
jamesheavey 0:7d4d2023ed9c 265 void Gamepad::joy_isr()
jamesheavey 0:7d4d2023ed9c 266 {
jamesheavey 0:7d4d2023ed9c 267 _event_state.set(JOY_PRESSED);
jamesheavey 0:7d4d2023ed9c 268 }
jamesheavey 0:7d4d2023ed9c 269
jamesheavey 0:7d4d2023ed9c 270 // get raw joystick coordinate in range -1 to 1
jamesheavey 0:7d4d2023ed9c 271 // Direction (x,y)
jamesheavey 0:7d4d2023ed9c 272 // North (0,1)
jamesheavey 0:7d4d2023ed9c 273 // East (1,0)
jamesheavey 0:7d4d2023ed9c 274 // South (0,-1)
jamesheavey 0:7d4d2023ed9c 275 // West (-1,0)
jamesheavey 0:7d4d2023ed9c 276 Vector2D Gamepad::get_coord()
jamesheavey 0:7d4d2023ed9c 277 {
jamesheavey 0:7d4d2023ed9c 278 // read() returns value in range 0.0 to 1.0 so is scaled and centre value
jamesheavey 0:7d4d2023ed9c 279 // substracted to get values in the range -1.0 to 1.0
jamesheavey 0:7d4d2023ed9c 280 float x = 2.0f*( _horiz->read() - _x0 );
jamesheavey 0:7d4d2023ed9c 281 float y = 2.0f*( _vert->read() - _y0 );
jamesheavey 0:7d4d2023ed9c 282
jamesheavey 0:7d4d2023ed9c 283 // Note: the x value here is inverted to ensure the positive x is to the
jamesheavey 0:7d4d2023ed9c 284 // right. This is simply due to how the potentiometer on the joystick
jamesheavey 0:7d4d2023ed9c 285 // I was using was connected up. It could have been corrected in hardware
jamesheavey 0:7d4d2023ed9c 286 // by swapping the power supply pins. Instead it is done in software so may
jamesheavey 0:7d4d2023ed9c 287 // need to be changed depending on your wiring setup
jamesheavey 0:7d4d2023ed9c 288
jamesheavey 0:7d4d2023ed9c 289 Vector2D coord = {-x,y};
jamesheavey 0:7d4d2023ed9c 290 return coord;
jamesheavey 0:7d4d2023ed9c 291 }
jamesheavey 0:7d4d2023ed9c 292
jamesheavey 0:7d4d2023ed9c 293 // This maps the raw x,y coord onto a circular grid.
jamesheavey 0:7d4d2023ed9c 294 // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html
jamesheavey 0:7d4d2023ed9c 295 Vector2D Gamepad::get_mapped_coord()
jamesheavey 0:7d4d2023ed9c 296 {
jamesheavey 0:7d4d2023ed9c 297 Vector2D coord = get_coord();
jamesheavey 0:7d4d2023ed9c 298
jamesheavey 0:7d4d2023ed9c 299 // do the transformation
jamesheavey 0:7d4d2023ed9c 300 float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f);
jamesheavey 0:7d4d2023ed9c 301 float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f);
jamesheavey 0:7d4d2023ed9c 302
jamesheavey 0:7d4d2023ed9c 303 Vector2D mapped_coord = {x,y};
jamesheavey 0:7d4d2023ed9c 304 return mapped_coord;
jamesheavey 0:7d4d2023ed9c 305 }
jamesheavey 0:7d4d2023ed9c 306
jamesheavey 0:7d4d2023ed9c 307 // this function converts the mapped coordinates into polar form
jamesheavey 0:7d4d2023ed9c 308 Polar Gamepad::get_polar()
jamesheavey 0:7d4d2023ed9c 309 {
jamesheavey 0:7d4d2023ed9c 310 // get the mapped coordinate
jamesheavey 0:7d4d2023ed9c 311 Vector2D coord = get_mapped_coord();
jamesheavey 0:7d4d2023ed9c 312
jamesheavey 0:7d4d2023ed9c 313 // at this point, 0 degrees (i.e. x-axis) will be defined to the East.
jamesheavey 0:7d4d2023ed9c 314 // We want 0 degrees to correspond to North and increase clockwise to 359
jamesheavey 0:7d4d2023ed9c 315 // like a compass heading, so we need to swap the axis and invert y
jamesheavey 0:7d4d2023ed9c 316 float x = coord.y;
jamesheavey 0:7d4d2023ed9c 317 float y = coord.x;
jamesheavey 0:7d4d2023ed9c 318
jamesheavey 0:7d4d2023ed9c 319 float mag = sqrt(x*x+y*y); // pythagoras
jamesheavey 0:7d4d2023ed9c 320 float angle = RAD2DEG*atan2(y,x);
jamesheavey 0:7d4d2023ed9c 321 // angle will be in range -180 to 180, so add 360 to negative angles to
jamesheavey 0:7d4d2023ed9c 322 // move to 0 to 360 range
jamesheavey 0:7d4d2023ed9c 323 if (angle < 0.0f) {
jamesheavey 0:7d4d2023ed9c 324 angle+=360.0f;
jamesheavey 0:7d4d2023ed9c 325 }
jamesheavey 0:7d4d2023ed9c 326
jamesheavey 0:7d4d2023ed9c 327 // the noise on the ADC causes the values of x and y to fluctuate slightly
jamesheavey 0:7d4d2023ed9c 328 // around the centred values. This causes the random angle values to get
jamesheavey 0:7d4d2023ed9c 329 // calculated when the joystick is centred and untouched. This is also when
jamesheavey 0:7d4d2023ed9c 330 // the magnitude is very small, so we can check for a small magnitude and then
jamesheavey 0:7d4d2023ed9c 331 // set the angle to -1. This will inform us when the angle is invalid and the
jamesheavey 0:7d4d2023ed9c 332 // joystick is centred
jamesheavey 0:7d4d2023ed9c 333
jamesheavey 0:7d4d2023ed9c 334 if (mag < TOL) {
jamesheavey 0:7d4d2023ed9c 335 mag = 0.0f;
jamesheavey 0:7d4d2023ed9c 336 angle = -1.0f;
jamesheavey 0:7d4d2023ed9c 337 }
jamesheavey 0:7d4d2023ed9c 338
jamesheavey 0:7d4d2023ed9c 339 Polar p = {mag,angle};
jamesheavey 0:7d4d2023ed9c 340 return p;
jamesheavey 0:7d4d2023ed9c 341 }