Chen Shuochen / Mbed 2 deprecated Battleship_2

Dependencies:   mbed Battleship

Committer:
mztkb
Date:
Mon May 06 10:20:32 2019 +0000
Revision:
1:e78a6db441d0
Parent:
0:340fb82a9149
add mbed lib

Who changed what in which revision?

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