Library for ELEC2645 Gamepad PCB. University of Leeds
Dependents: Project_MaZe1_copy Labirint Game_Controller_Project 200943373MAZE ... more
Gamepad.cpp
- Committer:
- eencae
- Date:
- 2017-02-06
- Revision:
- 3:964a6d95acdd
- Parent:
- 2:ea5538fcfe2f
- Child:
- 4:bafb7f483e93
File content as of revision 3:964a6d95acdd:
#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); 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); button_joystick = new InterruptIn(PTC16); vert = new AnalogIn(PTB10); horiz = new AnalogIn(PTB11); 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 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 ///////////////// void Gamepad::init() { lcd->init(); leds_off(); // read centred values of joystick _x0 = horiz->read(); _y0 = vert->read(); buzzer->period(1.0/1000.0); // 1 kHz // 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; } 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; } } bool Gamepad::joystick_pressed() { // ISR must have been triggered if (joy_flag) { joy_flag = 0; // clear flag return true; } else { return false; } } // this method gets the magnitude of the joystick movement float Gamepad::get_mag() { Polar p = get_polar(); return p.mag; } // this method gets the angle of joystick movement (0 to 360, 0 North) float Gamepad::get_angle() { Polar p = get_polar(); return p.angle; } Direction Joystick::get_direction() { float angle = get_angle(); // 0 to 360, -1 for centred Direction d; // partition 360 into segments and check which segment the angle is in if (angle < 0.0f) { d = CENTRE; // check for -1.0 angle } else if (angle < 22.5f) { // then keep going in 45 degree increments d = N; } else if (angle < 67.5f) { d = NE; } else if (angle < 112.5f) { d = E; } else if (angle < 157.5f) { d = SE; } else if (angle < 202.5f) { d = S; } else if (angle < 247.5f) { d = SW; } else if (angle < 292.5f) { d = W; } else if (angle < 337.5f) { d = NW; } else { d = N; } return d; } ///////////////////// 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); 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 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; } void Gamepad::joy_isr() { joy_flag=1; } // get raw joystick coordinate in range -1 to 1 // Direction (x,y) // North (0,1) // East (1,0) // South (0,-1) // West (-1,0) Vector2D Gamepad::get_coord() { // 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 ); // 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 // I was using was connected up. It could have been corrected in hardware // by swapping the power supply pins. Instead it is done in software so may // need to be changed depending on your wiring setup Vector2D coord = {-x,y}; return coord; } // This maps the raw x,y coord onto a circular grid. // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html Vector2D Gamepad::get_mapped_coord() { Vector2D coord = get_coord(); // do the transformation float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); Vector2D mapped_coord = {x,y}; return mapped_coord; } // this function converts the mapped coordinates into polar form Polar Gamepad::get_polar() { // get the mapped coordinate Vector2D coord = get_mapped_coord(); // at this point, 0 degrees (i.e. x-axis) will be defined to the East. // We want 0 degrees to correspond to North and increase clockwise to 359 // like a compass heading, so we need to swap the axis and invert y float x = coord.y; float y = coord.x; float mag = sqrt(x*x+y*y); // pythagoras float angle = RAD2DEG*atan2(y,x); // angle will be in range -180 to 180, so add 360 to negative angles to // move to 0 to 360 range if (angle < 0.0f) { angle+=360.0f; } // the noise on the ADC causes the values of x and y to fluctuate slightly // around the centred values. This causes the random angle values to get // calculated when the joystick is centred and untouched. This is also when // the magnitude is very small, so we can check for a small magnitude and then // set the angle to -1. This will inform us when the angle is invalid and the // joystick is centred if (mag < TOL) { mag = 0.0f; angle = -1.0f; } Polar p = {mag,angle}; return p; }