This Class allows the joystick, buttons and petiometers to be implemented easily and coherently in other classes

Fork of Gamepad by Craig Evans

Committer:
domkay97
Date:
Sun Apr 16 21:18:08 2017 +0000
Revision:
26:ccdf299b26c4
Parent:
24:7b149f5d4bef
Child:
27:d51c87ced56e
Created Players seelction

Who changed what in which revision?

UserRevisionLine numberNew contents of line
domkay97 21:99ec20695524 1 #include "Controller.h"
domkay97 21:99ec20695524 2 #include "mbed.h"
domkay97 21:99ec20695524 3
domkay97 21:99ec20695524 4 //////////// constructor/destructor ////////////
domkay97 21:99ec20695524 5 Controller::Controller()
domkay97 21:99ec20695524 6 :
domkay97 21:99ec20695524 7 _led1(new PwmOut(PTA1)),
domkay97 21:99ec20695524 8 _led2(new PwmOut(PTA2)),
domkay97 21:99ec20695524 9 _led3(new PwmOut(PTC2)),
domkay97 21:99ec20695524 10 _led4(new PwmOut(PTC3)),
domkay97 21:99ec20695524 11 _led5(new PwmOut(PTC4)),
domkay97 21:99ec20695524 12 _led6(new PwmOut(PTD3)),
domkay97 21:99ec20695524 13
domkay97 21:99ec20695524 14 _button_A(new InterruptIn(PTB9)),
domkay97 21:99ec20695524 15 _button_B(new InterruptIn(PTD0)),
domkay97 21:99ec20695524 16 _button_X(new InterruptIn(PTC17)),
domkay97 21:99ec20695524 17 _button_Y(new InterruptIn(PTC12)),
domkay97 21:99ec20695524 18 _button_L(new InterruptIn(PTB18)),
domkay97 21:99ec20695524 19 _button_R(new InterruptIn(PTB3)),
domkay97 21:99ec20695524 20 _button_back(new InterruptIn(PTB19)),
domkay97 21:99ec20695524 21 _button_start(new InterruptIn(PTC5)),
domkay97 21:99ec20695524 22 _button_joystick(new InterruptIn(PTC16)),
domkay97 21:99ec20695524 23
domkay97 21:99ec20695524 24 _vert(new AnalogIn(PTB10)),
domkay97 21:99ec20695524 25 _horiz(new AnalogIn(PTB11)),
domkay97 21:99ec20695524 26
domkay97 21:99ec20695524 27 _buzzer(new PwmOut(PTC10)),
domkay97 21:99ec20695524 28 _pot(new AnalogIn(PTB2)),
domkay97 21:99ec20695524 29
domkay97 21:99ec20695524 30 _timeout(new Timeout()),
domkay97 21:99ec20695524 31
domkay97 26:ccdf299b26c4 32 _event_state(0),
domkay97 26:ccdf299b26c4 33 _x0(0)
domkay97 26:ccdf299b26c4 34
domkay97 21:99ec20695524 35 {}
domkay97 21:99ec20695524 36
domkay97 21:99ec20695524 37 Controller::~Controller()
domkay97 21:99ec20695524 38 {
domkay97 21:99ec20695524 39 delete _led1,_led2,_led3,_led4,_led5,_led6;
domkay97 21:99ec20695524 40 delete _button_A,_button_B,_button_joystick,_vert,_horiz;
domkay97 21:99ec20695524 41 delete _button_X,_button_Y,_button_back,_button_start;
domkay97 21:99ec20695524 42 delete _button_L,_button_R, _buzzer, _pot, _timeout;
domkay97 21:99ec20695524 43 }
domkay97 21:99ec20695524 44
domkay97 21:99ec20695524 45 ///////////////// public methods /////////////////
domkay97 21:99ec20695524 46
domkay97 21:99ec20695524 47 void Controller::init()
domkay97 21:99ec20695524 48 {
domkay97 24:7b149f5d4bef 49 ledsOFF();
domkay97 21:99ec20695524 50 init_buttons();
domkay97 21:99ec20695524 51
domkay97 21:99ec20695524 52 // clear all flags
domkay97 21:99ec20695524 53 _event_state = 0;
domkay97 26:ccdf299b26c4 54 // read joystick
domkay97 26:ccdf299b26c4 55 _x0 = _horiz->read();
domkay97 21:99ec20695524 56 }
domkay97 21:99ec20695524 57
domkay97 24:7b149f5d4bef 58 void Controller::ledsOFF()
domkay97 21:99ec20695524 59 {
domkay97 21:99ec20695524 60 leds(0.0);
domkay97 21:99ec20695524 61 }
domkay97 21:99ec20695524 62
domkay97 24:7b149f5d4bef 63 void Controller::ledsON()
domkay97 21:99ec20695524 64 {
domkay97 21:99ec20695524 65 leds(1.0);
domkay97 21:99ec20695524 66 }
domkay97 21:99ec20695524 67
domkay97 21:99ec20695524 68 void Controller::leds(float val) const
domkay97 21:99ec20695524 69 {
domkay97 21:99ec20695524 70 if (val < 0.0f) {
domkay97 21:99ec20695524 71 val = 0.0f;
domkay97 21:99ec20695524 72 }
domkay97 21:99ec20695524 73 if (val > 1.0f) {
domkay97 21:99ec20695524 74 val = 1.0f;
domkay97 21:99ec20695524 75 }
domkay97 24:7b149f5d4bef 76
domkay97 21:99ec20695524 77 // leds are active-low, so subtract from 1.0
domkay97 21:99ec20695524 78 // 0.0 corresponds to fully-off, 1.0 to fully-on
domkay97 21:99ec20695524 79 val = 1.0f - val;
domkay97 21:99ec20695524 80
domkay97 21:99ec20695524 81 _led1->write(val);
domkay97 21:99ec20695524 82 _led2->write(val);
domkay97 21:99ec20695524 83 _led3->write(val);
domkay97 21:99ec20695524 84 _led4->write(val);
domkay97 21:99ec20695524 85 _led5->write(val);
domkay97 21:99ec20695524 86 _led6->write(val);
domkay97 21:99ec20695524 87 }
domkay97 21:99ec20695524 88
domkay97 21:99ec20695524 89 void Controller::led(int n,float val) const
domkay97 21:99ec20695524 90 {
domkay97 21:99ec20695524 91 // ensure they are within vlaid range
domkay97 21:99ec20695524 92 if (val < 0.0f) {
domkay97 21:99ec20695524 93 val = 0.0f;
domkay97 21:99ec20695524 94 }
domkay97 21:99ec20695524 95 if (val > 1.0f) {
domkay97 21:99ec20695524 96 val = 1.0f;
domkay97 21:99ec20695524 97 }
domkay97 21:99ec20695524 98
domkay97 21:99ec20695524 99 switch (n) {
domkay97 21:99ec20695524 100
domkay97 21:99ec20695524 101 // check for valid LED number and set value
domkay97 21:99ec20695524 102
domkay97 21:99ec20695524 103 case 1:
domkay97 21:99ec20695524 104 _led1->write(1.0f-val); // active-low so subtract from 1
domkay97 21:99ec20695524 105 break;
domkay97 21:99ec20695524 106 case 2:
domkay97 21:99ec20695524 107 _led2->write(1.0f-val); // active-low so subtract from 1
domkay97 21:99ec20695524 108 break;
domkay97 21:99ec20695524 109 case 3:
domkay97 21:99ec20695524 110 _led3->write(1.0f-val); // active-low so subtract from 1
domkay97 21:99ec20695524 111 break;
domkay97 21:99ec20695524 112 case 4:
domkay97 21:99ec20695524 113 _led4->write(1.0f-val); // active-low so subtract from 1
domkay97 21:99ec20695524 114 break;
domkay97 21:99ec20695524 115 case 5:
domkay97 21:99ec20695524 116 _led5->write(1.0f-val); // active-low so subtract from 1
domkay97 21:99ec20695524 117 break;
domkay97 21:99ec20695524 118 case 6:
domkay97 21:99ec20695524 119 _led6->write(1.0f-val); // active-low so subtract from 1
domkay97 21:99ec20695524 120 break;
domkay97 21:99ec20695524 121
domkay97 21:99ec20695524 122 }
domkay97 21:99ec20695524 123 }
domkay97 21:99ec20695524 124
domkay97 24:7b149f5d4bef 125 float Controller::pot_value() const
domkay97 21:99ec20695524 126 {
domkay97 21:99ec20695524 127 return _pot->read();
domkay97 21:99ec20695524 128 }
domkay97 21:99ec20695524 129
domkay97 24:7b149f5d4bef 130 void Controller::sound(float frequency, float duration)
domkay97 21:99ec20695524 131 {
domkay97 21:99ec20695524 132 _buzzer->period(1.0f/frequency);
domkay97 21:99ec20695524 133 _buzzer->write(0.5); // 50% duty cycle - square wave
domkay97 24:7b149f5d4bef 134 _timeout->attach(callback(this, &Controller::sound_off), duration );
domkay97 21:99ec20695524 135 }
domkay97 21:99ec20695524 136
domkay97 21:99ec20695524 137 bool Controller::check_event(ControllerEvent const id)
domkay97 21:99ec20695524 138 {
domkay97 21:99ec20695524 139 // Check whether event flag is set
domkay97 21:99ec20695524 140 if (_event_state[id]) {
domkay97 21:99ec20695524 141 _event_state.reset(id); // clear flag
domkay97 21:99ec20695524 142 return true;
domkay97 21:99ec20695524 143 } else {
domkay97 21:99ec20695524 144 return false;
domkay97 21:99ec20695524 145 }
domkay97 21:99ec20695524 146 }
domkay97 21:99ec20695524 147
domkay97 21:99ec20695524 148 ///////////////////// private methods ////////////////////////
domkay97 21:99ec20695524 149
domkay97 24:7b149f5d4bef 150 void Controller::sound_off()
domkay97 21:99ec20695524 151 {
domkay97 21:99ec20695524 152 // called after timeout
domkay97 21:99ec20695524 153 _buzzer->write(0.0);
domkay97 21:99ec20695524 154 }
domkay97 21:99ec20695524 155
domkay97 21:99ec20695524 156 void Controller::init_buttons()
domkay97 21:99ec20695524 157 {
domkay97 21:99ec20695524 158 // turn on pull-downs as other side of button is connected to 3V3
domkay97 21:99ec20695524 159 // button is 0 when not pressed and 1 when pressed
domkay97 21:99ec20695524 160 _button_A->mode(PullDown);
domkay97 21:99ec20695524 161 _button_B->mode(PullDown);
domkay97 21:99ec20695524 162 _button_X->mode(PullDown);
domkay97 21:99ec20695524 163 _button_Y->mode(PullDown);
domkay97 21:99ec20695524 164 _button_back->mode(PullDown);
domkay97 21:99ec20695524 165 _button_start->mode(PullDown);
domkay97 21:99ec20695524 166 _button_L->mode(PullDown);
domkay97 21:99ec20695524 167 _button_R->mode(PullDown);
domkay97 21:99ec20695524 168 _button_joystick->mode(PullDown);
domkay97 21:99ec20695524 169 // therefore setup rising edge interrupts
domkay97 21:99ec20695524 170 _button_A->rise(callback(this,&Controller::a_isr));
domkay97 21:99ec20695524 171 _button_B->rise(callback(this,&Controller::b_isr));
domkay97 21:99ec20695524 172 _button_X->rise(callback(this,&Controller::x_isr));
domkay97 21:99ec20695524 173 _button_Y->rise(callback(this,&Controller::y_isr));
domkay97 21:99ec20695524 174 _button_L->rise(callback(this,&Controller::l_isr));
domkay97 21:99ec20695524 175 _button_R->rise(callback(this,&Controller::r_isr));
domkay97 21:99ec20695524 176 _button_start->rise(callback(this,&Controller::start_isr));
domkay97 21:99ec20695524 177 _button_back->rise(callback(this,&Controller::back_isr));
domkay97 21:99ec20695524 178 _button_joystick->rise(callback(this,&Controller::joy_isr));
domkay97 21:99ec20695524 179 }
domkay97 21:99ec20695524 180
domkay97 21:99ec20695524 181 // button interrupts ISRs
domkay97 21:99ec20695524 182 // Each of these simply sets the appropriate event bit in the _event_state
domkay97 21:99ec20695524 183 // variable
domkay97 21:99ec20695524 184 void Controller::a_isr()
domkay97 21:99ec20695524 185 {
domkay97 21:99ec20695524 186 _event_state.set(A_PRESSED);
domkay97 21:99ec20695524 187 }
domkay97 21:99ec20695524 188 void Controller::b_isr()
domkay97 21:99ec20695524 189 {
domkay97 21:99ec20695524 190 _event_state.set(B_PRESSED);
domkay97 21:99ec20695524 191 }
domkay97 21:99ec20695524 192 void Controller::x_isr()
domkay97 21:99ec20695524 193 {
domkay97 21:99ec20695524 194 _event_state.set(X_PRESSED);
domkay97 21:99ec20695524 195 }
domkay97 21:99ec20695524 196 void Controller::y_isr()
domkay97 21:99ec20695524 197 {
domkay97 21:99ec20695524 198 _event_state.set(Y_PRESSED);
domkay97 21:99ec20695524 199 }
domkay97 21:99ec20695524 200 void Controller::l_isr()
domkay97 21:99ec20695524 201 {
domkay97 21:99ec20695524 202 _event_state.set(L_PRESSED);
domkay97 21:99ec20695524 203 }
domkay97 21:99ec20695524 204 void Controller::r_isr()
domkay97 21:99ec20695524 205 {
domkay97 21:99ec20695524 206 _event_state.set(R_PRESSED);
domkay97 21:99ec20695524 207 }
domkay97 21:99ec20695524 208 void Controller::back_isr()
domkay97 21:99ec20695524 209 {
domkay97 21:99ec20695524 210 _event_state.set(BACK_PRESSED);
domkay97 21:99ec20695524 211 }
domkay97 21:99ec20695524 212 void Controller::start_isr()
domkay97 21:99ec20695524 213 {
domkay97 21:99ec20695524 214 _event_state.set(START_PRESSED);
domkay97 21:99ec20695524 215 }
domkay97 21:99ec20695524 216 void Controller::joy_isr()
domkay97 21:99ec20695524 217 {
domkay97 21:99ec20695524 218 _event_state.set(JOY_PRESSED);
domkay97 21:99ec20695524 219 }
domkay97 21:99ec20695524 220
domkay97 26:ccdf299b26c4 221
domkay97 26:ccdf299b26c4 222
domkay97 26:ccdf299b26c4 223 // get raw joystick coordinate for x in range -1 to 1
domkay97 26:ccdf299b26c4 224 // North (0,1)
domkay97 26:ccdf299b26c4 225 // South (0,-1)
domkay97 26:ccdf299b26c4 226
domkay97 26:ccdf299b26c4 227 float Controller::get_joy()
domkay97 26:ccdf299b26c4 228 {
domkay97 26:ccdf299b26c4 229 // read() returns value in range 0.0 to 1.0 so is scaled and centre value
domkay97 26:ccdf299b26c4 230 // substracted to get values in the range -1.0 to 1.0
domkay97 26:ccdf299b26c4 231 float x = 2.0f*( _horiz->read() - _x0 );
domkay97 26:ccdf299b26c4 232 return x;
domkay97 26:ccdf299b26c4 233 }
domkay97 26:ccdf299b26c4 234
domkay97 26:ccdf299b26c4 235
domkay97 21:99ec20695524 236 int Controller::check_for_buttons() {
domkay97 21:99ec20695524 237 if (check_event(A_PRESSED)) {
domkay97 21:99ec20695524 238 return 1; }
domkay97 21:99ec20695524 239 else if (check_event(B_PRESSED)) {
domkay97 21:99ec20695524 240 return 2; }
domkay97 21:99ec20695524 241 else if (check_event(X_PRESSED)) {
domkay97 21:99ec20695524 242 return 3; }
domkay97 21:99ec20695524 243 else if (check_event(Y_PRESSED)) {
domkay97 21:99ec20695524 244 return 4; }
domkay97 22:2f5a009a82a6 245 else if (check_event(L_PRESSED)) {
domkay97 22:2f5a009a82a6 246 return 5; }
domkay97 22:2f5a009a82a6 247 else if (check_event(R_PRESSED)) {
domkay97 22:2f5a009a82a6 248 return 6; }
domkay97 22:2f5a009a82a6 249 else if (check_event(JOY_PRESSED)) {
domkay97 22:2f5a009a82a6 250 return 7; }
domkay97 21:99ec20695524 251 else {
domkay97 21:99ec20695524 252 return 0; }
domkay97 21:99ec20695524 253 }