Class used to interface with the handheld gamepad.
Fork of Gamepad by
Gamepad.cpp
- Committer:
- eencae
- Date:
- 2017-02-04
- Revision:
- 2:ea5538fcfe2f
- Parent:
- 1:6d25cd49059b
- Child:
- 3:964a6d95acdd
File content as of revision 2:ea5538fcfe2f:
#include "Gamepad.h" //////////// constructor/destructor //////////// Gamepad::Gamepad() { led_1 = new PwmOut(PTA1); led_2 = new PwmOut(PTA2); led_3 = new PwmOut(PTC2); led_4 = new PwmOut(PTC3); led_5 = new PwmOut(PTC4); led_6 = new PwmOut(PTD3); lcd = new N5110(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); joystick = new Joystick(PTB10,PTB11,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); 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() { fade_leds(0.0); } void Gamepad::leds_on() { 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; }