HaoZhang SID: 201199702

Dependencies:   mbed

Committer:
zh870524589
Date:
Thu May 14 17:45:05 2020 +0000
Revision:
2:867fdea920c1
Parent:
0:45ce241d316b
final

Who changed what in which revision?

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