Class used to interface with the handheld gamepad.

Fork of Gamepad by Craig Evans

Gamepad.h

Committer:
eencae
Date:
2017-02-07
Revision:
9:893189072e89
Parent:
8:7eaf26f4b5f2
Child:
10:a13d2f9d8a14

File content as of revision 9:893189072e89:

#ifndef GAMEPAD_H
#define GAMEPAD_H

#include "mbed.h"

#define TOL 0.1f
#define RAD2DEG 57.2957795131f

/** Enum for direction */
enum Direction {
    CENTRE,  /**< joystick centred */
    N,       /**< pushed North (0)*/
    NE,      /**< pushed North-East (45) */
    E,       /**< pushed East (90) */
    SE,      /**< pushed South-East (135) */
    S,       /**< pushed South (180) */
    SW,      /**< pushed South-West (225) */
    W,       /**< pushed West (270) */
    NW       /**< pushed North-West (315) */
};

/** Vector 2D struct */
struct Vector2D {
    float x; /**< float for x value */
    float y; /**< float for y value */
};

/** Polar coordinate struct */
struct Polar {
    float mag;  /**< float for magnitude */
    float angle; /**< float for angle (in degrees) */
};

/** Gamepad Class
@brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds
@author Dr Craig A. Evans
@date Febraury 2017
*/
class Gamepad
{

public:

    /** Constructor */
    Gamepad();

    /** Destructor */
    ~Gamepad();

    /** Initialise all peripherals and configure interrupts */
    void init();

    /** Turn all LEDs on */
    void leds_on();

    /** Turn all LEDs off */
    void leds_off();

    /** Set all LEDs to duty-cycle
    *@param value in range 0.0 to 1.0
    */
    void set_leds(float val);

    /** Set LED to duty-cycle
    *@param value in range 0.0 to 1.0
    */
    void set_led1(float val);

    /** Set LED to duty-cycle
      *@param value in range 0.0 to 1.0
      */
    void set_led2(float val);

    /** Set LED to duty-cycle
    *@param value in range 0.0 to 1.0
    */
    void set_led3(float val);

    /** Set LED to duty-cycle
    *@param value in range 0.0 to 1.0
    */
    void set_led4(float val);

    /** Set LED to duty-cycle
    *@param value in range 0.0 to 1.0
    */
    void set_led5(float val);

    /** Set LED to duty-cycle
    *@param value in range 0.0 to 1.0
    */
    void set_led6(float val);

    /** Read potentiometer
    *@returns potentiometer value in range 0.0 to 1.0
    */
    float read_pot();

    /** Play tone on piezo
    * @param frequency in Hz
    * @param duration of tone in seconds
    */
    void tone(float frequency, float duration);

    /** Check if A button pressed
    * @returns true if yes, false if no
    */
    bool a_pressed();

    /** Check if B button pressed
    * @returns true if yes, false if no
    */
    bool b_pressed();

    /** Check if X button pressed
    * @returns true if yes, false if no
    */
    bool x_pressed();

    /** Check if Y button pressed
    * @returns true if yes, false if no
    */
    bool y_pressed();

    /** Check if L button pressed
    * @returns true if yes, false if no
    */
    bool l_pressed();

    /** Check if R button pressed
    * @returns true if yes, false if no
    */
    bool r_pressed();

    /** Check if Back button pressed
    * @returns true if yes, false if no
    */
    bool back_pressed();

    /** Check if Start button pressed
    * @returns true if yes, false if no
    */
    bool start_pressed();

    /** Check if Joystick button pressed
    * @returns true if yes, false if no
    */
    bool joystick_pressed();

    /** Get magnitude of joystick movement
    * @returns value in range 0.0 to 1.0
    */

    float get_mag();

    /** Get angle of joystick movement
    * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central
    */
    float get_angle();

    /** Gets joystick direction
    * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW,
    */
    Direction get_direction();    // N,NE,E,SE etc.

    /** Gets raw cartesian co-ordinates of joystick
    * @returns a struct with x,y members, each in the range 0.0 to 1.0
    */
    Vector2D get_coord();         // cartesian co-ordinates x,y

    /** Gets cartesian coordinates mapped to circular grid
    * @returns a struct with x,y members, each in the range 0.0 to 1.0
    */
    Vector2D get_mapped_coord();  // x,y mapped to circle

    /** Gets polar coordinates of the joystick
    * @returns a struct contains mag and angle
    */
    Polar get_polar();            // mag and angle in struct form

private:

    PwmOut *led1;
    PwmOut *led2;
    PwmOut *led3;
    PwmOut *led4;
    PwmOut *led5;
    PwmOut *led6;

    InterruptIn *button_A;
    InterruptIn *button_B;
    InterruptIn *button_X;
    InterruptIn *button_Y;
    InterruptIn *button_back;
    InterruptIn *button_start;
    InterruptIn *button_L;
    InterruptIn *button_R;
    InterruptIn *button_joystick;

    AnalogIn *vert;
    AnalogIn *horiz;

    PwmOut *buzzer;
    AnalogIn *pot;

    Timeout *timeout;

    void init_buttons();
    void tone_off();

    void a_isr();
    void b_isr();
    void x_isr();
    void y_isr();
    void l_isr();
    void r_isr();
    void back_isr();
    void start_isr();
    void joy_isr();

    bool a_flag,b_flag,x_flag,y_flag,l_flag,r_flag,back_flag,start_flag,joy_flag;

    // centred x,y values
    float _x0;
    float _y0;
};

#endif