Library for ELEC2645 Gamepad PCB. University of Leeds Version 2 January 2020
Dependents: ELEC2645_Ticker_WAV ELEC2645_Project_username ELEC2645_Project_el18vgt ELEC2645_Project_el17oc11 ... more
Diff: Gamepad.cpp
- Revision:
- 11:ff86b2ffce01
- Parent:
- 10:a13d2f9d8a14
- Child:
- 12:1b0b6355da4f
--- a/Gamepad.cpp Tue Feb 07 11:53:37 2017 +0000 +++ b/Gamepad.cpp Thu Mar 02 18:34:42 2017 +0000 @@ -10,32 +10,32 @@ _led5 = new PwmOut(PTC4); _led6 = new PwmOut(PTD3); - button_A = new InterruptIn(PTB9); - button_B = new InterruptIn(PTD0); - button_X = new InterruptIn(PTC17); - button_Y = new InterruptIn(PTC12); - button_back = new InterruptIn(PTB19); - button_start = new InterruptIn(PTC5); - button_L = new InterruptIn(PTB18); - button_R = new InterruptIn(PTB3); - button_joystick = new InterruptIn(PTC16); + _button_A = new InterruptIn(PTB9); + _button_B = new InterruptIn(PTD0); + _button_X = new InterruptIn(PTC17); + _button_Y = new InterruptIn(PTC12); + _button_back = new InterruptIn(PTB19); + _button_start = new InterruptIn(PTC5); + _button_L = new InterruptIn(PTB18); + _button_R = new InterruptIn(PTB3); + _button_joystick = new InterruptIn(PTC16); - vert = new AnalogIn(PTB10); - horiz = new AnalogIn(PTB11); + _vert = new AnalogIn(PTB10); + _horiz = new AnalogIn(PTB11); - buzzer = new PwmOut(PTC10); - pot = new AnalogIn(PTB2); + _buzzer = new PwmOut(PTC10); + _pot = new AnalogIn(PTB2); - timeout = new Timeout(); + _timeout = new Timeout(); } Gamepad::~Gamepad() { delete _led1,_led2,_led3,_led4,_led5,_led6; - delete button_A,button_B,button_joystick,vert,horiz; - delete button_X, button_Y, button_back, button_start; - delete button_L, button_R, buzzer, pot, timeout; + delete _button_A,_button_B,_button_joystick,_vert,_horiz; + delete _button_X,_button_Y,_button_back,_button_start; + delete _button_L,_button_R, _buzzer, _pot, _timeout; } ///////////////// public methods ///////////////// @@ -46,13 +46,19 @@ init_buttons(); // read centred values of joystick - _x0 = horiz->read(); - _y0 = vert->read(); + _x0 = _horiz->read(); + _y0 = _vert->read(); // clear all flags - a_flag=0,b_flag=0,x_flag=0,y_flag=0,joy_flag=0; - l_flag=0,r_flag=0,back_flag=0,start_flag=0; - + _a_flag = false; + _b_flag = false; + _x_flag = false; + _y_flag = false; + _joy_flag = false; + _l_flag = false; + _r_flag = false; + _back_flag = false; + _start_flag = false; } void Gamepad::leds_off() @@ -65,7 +71,7 @@ leds(1.0); } -void Gamepad::leds(float val) +void Gamepad::leds(float val) const { if (val < 0.0f) { val = 0.0f; @@ -105,23 +111,23 @@ _led6->write(1.0f-val); // active-low so subtract from 1 } -float Gamepad::read_pot() +float Gamepad::read_pot() const { - return pot->read(); + return _pot->read(); } void Gamepad::tone(float frequency, float duration) { - buzzer->period(1.0f/frequency); - buzzer->write(0.5); // 50% duty cycle - square wave - timeout->attach(callback(this, &Gamepad::tone_off), duration ); + _buzzer->period(1.0f/frequency); + _buzzer->write(0.5); // 50% duty cycle - square wave + _timeout->attach(callback(this, &Gamepad::tone_off), duration ); } bool Gamepad::a_pressed() { // ISR must have been triggered - if (a_flag) { - a_flag = 0; // clear flag + if (_a_flag) { + _a_flag = 0; // clear flag return true; } else { return false; @@ -131,8 +137,8 @@ bool Gamepad::b_pressed() { // ISR must have been triggered - if (b_flag) { - b_flag = 0; // clear flag + if (_b_flag) { + _b_flag = false; // clear flag return true; } else { return false; @@ -142,8 +148,8 @@ bool Gamepad::x_pressed() { // ISR must have been triggered - if (x_flag) { - x_flag = 0; // clear flag + if (_x_flag) { + _x_flag = false; // clear flag return true; } else { return false; @@ -153,8 +159,8 @@ bool Gamepad::y_pressed() { // ISR must have been triggered - if (y_flag) { - y_flag = 0; // clear flag + if (_y_flag) { + _y_flag = false; // clear flag return true; } else { return false; @@ -164,8 +170,8 @@ bool Gamepad::l_pressed() { // ISR must have been triggered - if (l_flag) { - l_flag = 0; // clear flag + if (_l_flag) { + _l_flag = false; // clear flag return true; } else { return false; @@ -175,8 +181,8 @@ bool Gamepad::r_pressed() { // ISR must have been triggered - if (r_flag) { - r_flag = 0; // clear flag + if (_r_flag) { + _r_flag = false; // clear flag return true; } else { return false; @@ -186,8 +192,8 @@ bool Gamepad::back_pressed() { // ISR must have been triggered - if (back_flag) { - back_flag = 0; // clear flag + if (_back_flag) { + _back_flag = false; // clear flag return true; } else { return false; @@ -197,8 +203,8 @@ bool Gamepad::start_pressed() { // ISR must have been triggered - if (start_flag) { - start_flag = 0; // clear flag + if (_start_flag) { + _start_flag = false; // clear flag return true; } else { return false; @@ -208,8 +214,8 @@ bool Gamepad::joystick_pressed() { // ISR must have been triggered - if (joy_flag) { - joy_flag = 0; // clear flag + if (_joy_flag) { + _joy_flag = false; // clear flag return true; } else { return false; @@ -265,70 +271,70 @@ void Gamepad::tone_off() { - buzzer->write(0.0); + _buzzer->write(0.0); } void Gamepad::init_buttons() { // turn on pull-downs as other side of button is connected to 3V3 // button is 0 when not pressed and 1 when pressed - button_A->mode(PullDown); - button_B->mode(PullDown); - button_X->mode(PullDown); - button_Y->mode(PullDown); - button_back->mode(PullDown); - button_start->mode(PullDown); - button_L->mode(PullDown); - button_R->mode(PullDown); - button_joystick->mode(PullDown); + _button_A->mode(PullDown); + _button_B->mode(PullDown); + _button_X->mode(PullDown); + _button_Y->mode(PullDown); + _button_back->mode(PullDown); + _button_start->mode(PullDown); + _button_L->mode(PullDown); + _button_R->mode(PullDown); + _button_joystick->mode(PullDown); // therefore setup rising edge interrupts - button_A->rise(callback(this,&Gamepad::a_isr)); - button_B->rise(callback(this,&Gamepad::b_isr)); - button_X->rise(callback(this,&Gamepad::x_isr)); - button_Y->rise(callback(this,&Gamepad::y_isr)); - button_L->rise(callback(this,&Gamepad::l_isr)); - button_R->rise(callback(this,&Gamepad::r_isr)); - button_start->rise(callback(this,&Gamepad::start_isr)); - button_back->rise(callback(this,&Gamepad::back_isr)); - button_joystick->rise(callback(this,&Gamepad::joy_isr)); + _button_A->rise(callback(this,&Gamepad::a_isr)); + _button_B->rise(callback(this,&Gamepad::b_isr)); + _button_X->rise(callback(this,&Gamepad::x_isr)); + _button_Y->rise(callback(this,&Gamepad::y_isr)); + _button_L->rise(callback(this,&Gamepad::l_isr)); + _button_R->rise(callback(this,&Gamepad::r_isr)); + _button_start->rise(callback(this,&Gamepad::start_isr)); + _button_back->rise(callback(this,&Gamepad::back_isr)); + _button_joystick->rise(callback(this,&Gamepad::joy_isr)); } // button interrupts ISRs void Gamepad::a_isr() { - a_flag=1; + _a_flag=true; } void Gamepad::b_isr() { - b_flag=1; + _b_flag=true; } void Gamepad::x_isr() { - x_flag=1; + _x_flag=true; } void Gamepad::y_isr() { - y_flag=1; + _y_flag=true; } void Gamepad::l_isr() { - l_flag=1; + _l_flag=true; } void Gamepad::r_isr() { - r_flag=1; + _r_flag=true; } void Gamepad::back_isr() { - back_flag=1; + _back_flag=true; } void Gamepad::start_isr() { - start_flag=1; + _start_flag=true; } void Gamepad::joy_isr() { - joy_flag=1; + _joy_flag=true; } // get raw joystick coordinate in range -1 to 1 @@ -341,8 +347,8 @@ { // read() returns value in range 0.0 to 1.0 so is scaled and centre value // substracted to get values in the range -1.0 to 1.0 - float x = 2.0f*( horiz->read() - _x0 ); - float y = 2.0f*( vert->read() - _y0 ); + float x = 2.0f*( _horiz->read() - _x0 ); + float y = 2.0f*( _vert->read() - _y0 ); // Note: the x value here is inverted to ensure the positive x is to the // right. This is simply due to how the potentiometer on the joystick