Project Submission (late)

Dependencies:   mbed

Committer:
el17tc
Date:
Fri May 10 14:52:28 2019 +0000
Revision:
3:83e79d31930c
Parent:
0:72f372170a73
final commit, API is added.; I'm not sure if there is a specific statement of academic integrity wanted but- I declare that this work is 100% my own, I have not plagiarised any work or attempted to fabricate any part of it for extra marks.

Who changed what in which revision?

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