N5110

Dependencies:   mbed

Committer:
aileen
Date:
Tue Apr 28 01:39:30 2020 +0000
Revision:
0:90b7f20b3a8d
N5110

Who changed what in which revision?

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