ELEC2645 (2015/16) / Joystick

Dependents:   L2_SpaceInvaders 6-Joystick

Embed: (wiki syntax)

« Back to documentation index

Joystick Class Reference

Joystick Class Reference

Library for interfacing with a Joystick (http://proto-pic.co.uk/thumb-joystick-analogue/) More...

#include <Joystick.h>

Public Member Functions

 Joystick (PinName x_axis_pin, PinName y_axis_pin, PinName button_pin)
 Creates a Joystick object connected to the given pins Dynamically allocates AnalogIn for input potentiometers, InterruptIn for the joystick buton and the debounce Timeout.
 ~Joystick ()
 Destroys the Joystick object Clears the AnalogIn's, InterruptIn and Timeout from memory.
void init ()
 Initalises the Joystick Sets up the InterruptIn Mode ISR Initalises the offset vairables and ISR flags Samples the AnalogIn's 5 times and takes an average to get the offset.
float GetXValue ()
 Gets the value of the x potentiometer Takes 5 readings from the potentiometer Calculates the average measurement, accounting for joystick offset Caps the average between 0 and 1.
float GetYValue ()
 Gets the value of the y potentiometer Takes 5 readings from the potentiometer Calculates the average measurement, accounting for joystick offset Caps the average between 0 and 1.
int get_button_flag ()
 Reads the state of the button flag.
void set_button_flag (bool value)
 Sets the button flag.

Detailed Description

Library for interfacing with a Joystick (http://proto-pic.co.uk/thumb-joystick-analogue/)

Joystick is just 2 potentiometers and a button so can be interfaced with AnalogIn and DigitalIn/InterruptIn The library contains a method to prevent button debounce with a Timeout

Author:
Avinash Patel
Date:
April 2016 Example
#include "mbed.h"
#include "Joystick.h"

//               Xaxis,Yaxis,Button 
Joystick joystick(PTB3, PTB2, PTB11);
DigitalOut r_led(LED_RED);
Serial pc(USBTX, USBRX);

int main()
{
    //First initalise joystick
    joystick.init();

    while (true) {
        //Stores x and y output values
        float x, y;

        //Calls the "GetXValue" and "GetYValue" and stores it in x and y
        x = joystick.GetXValue();
        y = joystick.GetYValue();

        //Prints the values to the terminal
        pc.printf("X: %f, Y: %f\n", x, y);

        //If the button flag is pressed switch the led
        if (joystick.get_button_flag()) {
            r_led = !r_led
        }
    }
}

Definition at line 55 of file Joystick.h.


Constructor & Destructor Documentation

Joystick ( PinName  x_axis_pin,
PinName  y_axis_pin,
PinName  button_pin 
)

Creates a Joystick object connected to the given pins Dynamically allocates AnalogIn for input potentiometers, InterruptIn for the joystick buton and the debounce Timeout.

Parameters:
x_axis_pinconnected to the Joystick's x potentiometer output
y_axis_pinconnected to the Joystick's y potentiometer output
button_pinconnected to the Joystick's button

Definition at line 10 of file Joystick.cpp.

~Joystick (  )

Destroys the Joystick object Clears the AnalogIn's, InterruptIn and Timeout from memory.

Definition at line 18 of file Joystick.cpp.


Member Function Documentation

int get_button_flag (  )

Reads the state of the button flag.

Returns:
the button flag

Definition at line 94 of file Joystick.cpp.

float GetXValue (  )

Gets the value of the x potentiometer Takes 5 readings from the potentiometer Calculates the average measurement, accounting for joystick offset Caps the average between 0 and 1.

Returns:
the average value of the x potentiometer

Definition at line 52 of file Joystick.cpp.

float GetYValue (  )

Gets the value of the y potentiometer Takes 5 readings from the potentiometer Calculates the average measurement, accounting for joystick offset Caps the average between 0 and 1.

Returns:
the average value of the y potentiometer

Definition at line 73 of file Joystick.cpp.

void init (  )

Initalises the Joystick Sets up the InterruptIn Mode ISR Initalises the offset vairables and ISR flags Samples the AnalogIn's 5 times and takes an average to get the offset.

Definition at line 26 of file Joystick.cpp.

void set_button_flag ( bool  value )

Sets the button flag.

Parameters:
valueThe value the flag will be set to

Definition at line 99 of file Joystick.cpp.