Similar to the example code.

Dependencies:   mbed N5110

Committer:
2016110307
Date:
Tue Apr 23 16:04:18 2019 +0000
Revision:
0:97418ec4c37d
Fundamental functions have achieved.

Who changed what in which revision?

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