Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Fri May 15 09:03:36 2020 +0000
Revision:
39:0debc17bad29
Parent:
1:ed745421d8c4
the final version

Who changed what in which revision?

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