A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Thu Apr 11 22:27:30 2019 +0000
Revision:
0:8e92b66a0755
Child:
9:304079450898
Start of project

Who changed what in which revision?

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