Yang Zhenye 201199680

Dependencies:   mbed

Committer:
yangzhenye
Date:
Fri Apr 24 06:58:33 2020 +0000
Revision:
0:ac2868313b41
Roony's model

Who changed what in which revision?

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