Class used to interface with the handheld gamepad.
Fork of Gamepad by
Diff: Gamepad.cpp
- Revision:
- 1:6d25cd49059b
- Parent:
- 0:a6288c29b936
- Child:
- 2:ea5538fcfe2f
diff -r a6288c29b936 -r 6d25cd49059b Gamepad.cpp --- a/Gamepad.cpp Sat Feb 04 15:31:44 2017 +0000 +++ b/Gamepad.cpp Sat Feb 04 16:13:18 2017 +0000 @@ -1,5 +1,6 @@ #include "Gamepad.h" +//////////// constructor/destructor //////////// Gamepad::Gamepad() { led_1 = new PwmOut(PTA1); @@ -24,30 +25,223 @@ buzzer = new PwmOut(PTC10); pot = new AnalogIn(PTB2); + timeout = new Timeout(); + } +Gamepad::~Gamepad() +{ + delete led_1,led_2,led_3,led_4,led_5,led_6; + delete lcd,joystick,button_A,button_B; + delete button_X, button_Y, button_back, button_start; + delete button_L, button_R, buzzer, pot, timeout; +} + +///////////////// public methods ///////////////// + void Gamepad::init() { lcd->init(); joystick->init(); + leds_off(); + + buzzer->period(1.0/1000.0); // 1 kHz + + // clear all flags + a_flag=0,b_flag=0,x_flag=0,y_flag=0,l_flag=0,r_flag=0,back_flag=0,start_flag=0; + } void Gamepad::leds_off() { - led_1->write(1.0); - led_2->write(1.0); - led_3->write(1.0); - led_4->write(1.0); - led_5->write(1.0); - led_6->write(1.0); + fade_leds(0.0); } void Gamepad::leds_on() { - led_1->write(0.0); - led_2->write(0.0); - led_3->write(0.0); - led_4->write(0.0); - led_5->write(0.0); - led_6->write(0.0); + fade_leds(1.0); +} + +void Gamepad::fade_leds(float val) +{ + if (val < 0.0f) { + val = 0.0f; + } + if (val > 1.0f) { + val = 1.0f; + } + + // leds are active-low, so subtract from 1.0 + // 0.0 corresponds to fully-off, 1.0 to fully-on + val = 1.0f - val; + + led_1->write(val); + led_2->write(val); + led_3->write(val); + led_4->write(val); + led_5->write(val); + led_6->write(val); +} + +float Gamepad::read_pot() +{ + 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 ); +} + +bool Gamepad::a_pressed() +{ + // ISR must have been triggered + if (a_flag) { + a_flag = 0; // clear flag + return true; + } else { + return false; + } +} + +bool Gamepad::b_pressed() +{ + // ISR must have been triggered + if (b_flag) { + b_flag = 0; // clear flag + return true; + } else { + return false; + } +} + +bool Gamepad::x_pressed() +{ + // ISR must have been triggered + if (x_flag) { + x_flag = 0; // clear flag + return true; + } else { + return false; + } +} + +bool Gamepad::y_pressed() +{ + // ISR must have been triggered + if (y_flag) { + y_flag = 0; // clear flag + return true; + } else { + return false; + } +} + +bool Gamepad::l_pressed() +{ + // ISR must have been triggered + if (l_flag) { + l_flag = 0; // clear flag + return true; + } else { + return false; + } } + +bool Gamepad::r_pressed() +{ + // ISR must have been triggered + if (r_flag) { + r_flag = 0; // clear flag + return true; + } else { + return false; + } +} + +bool Gamepad::back_pressed() +{ + // ISR must have been triggered + if (back_flag) { + back_flag = 0; // clear flag + return true; + } else { + return false; + } +} + +bool Gamepad::start_pressed() +{ + // ISR must have been triggered + if (a_flag) { + a_flag = 0; // clear flag + return true; + } else { + return false; + } +} +///////////////////// private methods //////////////////////// + +void Gamepad::tone_off() +{ + 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); + // 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 interrupts ISRs +void Gamepad::a_isr() +{ + a_flag=1; +} +void Gamepad::b_isr() +{ + b_flag=1; +} +void Gamepad::x_isr() +{ + x_flag=1; +} +void Gamepad::y_isr() +{ + y_flag=1; +} +void Gamepad::l_isr() +{ + l_flag=1; +} +void Gamepad::r_isr() +{ + r_flag=1; +} +void Gamepad::back_isr() +{ + back_flag=1; +} +void Gamepad::start_isr() +{ + start_flag=1; +} \ No newline at end of file