Li Ruofan 201199450

Dependencies:   mbed Gamepad Joystick

Committer:
DannyLee
Date:
Fri May 15 19:57:40 2020 +0000
Revision:
5:e3a9f0548922
need to debug

Who changed what in which revision?

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