Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Gamepad.h
- Committer:
- valavanisalex
- Date:
- 2017-03-03
- Revision:
- 12:1b0b6355da4f
- Parent:
- 11:ff86b2ffce01
- Child:
- 13:ef5fc9f58805
File content as of revision 12:1b0b6355da4f:
#ifndef GAMEPAD_H
#define GAMEPAD_H
#include <bitset>
#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) */
};
/**
* List of events that can be registered on the gamepad
*/
enum GamepadEvent {
EVENT_A_PRESSED, ///< Button A has been pressed
EVENT_B_PRESSED, ///< Button B has been pressed
EVENT_X_PRESSED, ///< Button X has been pressed
EVENT_Y_PRESSED, ///< Button Y has been pressed
EVENT_L_PRESSED, ///< Button L has been pressed
EVENT_R_PRESSED, ///< Button R has been pressed
EVENT_BACK_PRESSED, ///< Button "Back" has been pressed
EVENT_START_PRESSED, ///< Button "Start" has been pressed
EVENT_JOY_PRESSED, ///< Joystick button has been pressed
N_EVENTS ///< A dummy flag that marks the end of the list
};
/** Gamepad Class
@brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds
@author Dr Craig A. Evans
@date Febraury 2017
*/
class Gamepad
{
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;
std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed
// centred x,y values
float _x0;
float _y0;
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 leds(float val) const;
/** Set LED to duty-cycle
*@param value in range 0.0 to 1.0
*/
void led1(float val);
/** Set LED to duty-cycle
*@param value in range 0.0 to 1.0
*/
void led2(float val);
/** Set LED to duty-cycle
*@param value in range 0.0 to 1.0
*/
void led3(float val);
/** Set LED to duty-cycle
*@param value in range 0.0 to 1.0
*/
void led4(float val);
/** Set LED to duty-cycle
*@param value in range 0.0 to 1.0
*/
void led5(float val);
/** Set LED to duty-cycle
*@param value in range 0.0 to 1.0
*/
void led6(float val);
/** Read potentiometer
*@returns potentiometer value in range 0.0 to 1.0
*/
float read_pot() const;
/** Play tone on piezo
* @param frequency in Hz
* @param duration of tone in seconds
*/
void tone(float frequency, float duration);
/**
* @brief Check whether an event flag has been set and clear it
* @param id[in] The ID of the event to test
* @return true if the event occurred
*/
bool check_event(GamepadEvent const id);
/** 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:
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();
};
#endif