Yang Hongxiao 201199678

Dependencies:   mbed

Committer:
YHX
Date:
Sun Apr 26 13:17:59 2020 +0000
Revision:
0:4b02786450c0
initial commit

Who changed what in which revision?

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