Implement basic functions

Dependencies:   mbed N5110

Committer:
Wuuu
Date:
Fri May 03 13:05:24 2019 +0000
Revision:
0:64bd9f996099
Flappy Bird, which with header programme

Who changed what in which revision?

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