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:
Sat Apr 15 15:39:47 2017 +0000
Revision:
23:34da21dfdfd6
Parent:
22:2f5a009a82a6
Child:
24:7b149f5d4bef
Removed redundant code

Who changed what in which revision?

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