bird

Dependencies:   mbed

Committer:
1013373474
Date:
Fri May 03 17:51:38 2019 +0000
Revision:
0:3887ebee1b37
fly bird

Who changed what in which revision?

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