ELEC2645 (2015/16) / Joystick

Dependents:   L2_SpaceInvaders 6-Joystick

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Joystick.h Source File

Joystick.h

Go to the documentation of this file.
00001 /**
00002 @file Joystick.h
00003 @brief Joysitck header file containing member functions and variables
00004 */
00005 //Joystick class - Header file
00006 //Define guards
00007 #ifndef JOYSTICK_H
00008 #define JOYSTICK_H
00009 
00010 #include "mbed.h"
00011 
00012 /**
00013 @brief Library for interfacing with a Joystick (http://proto-pic.co.uk/thumb-joystick-analogue/)
00014 @brief Joystick is just 2 potentiometers and a button so can be interfaced with AnalogIn and DigitalIn/InterruptIn
00015 @brief The library contains a method to prevent button debounce with a Timeout
00016 
00017 @author Avinash Patel
00018 @date April 2016
00019  * Example
00020  * @code
00021 
00022 #include "mbed.h"
00023 #include "Joystick.h"
00024 
00025 //               Xaxis,Yaxis,Button 
00026 Joystick joystick(PTB3, PTB2, PTB11);
00027 DigitalOut r_led(LED_RED);
00028 Serial pc(USBTX, USBRX);
00029 
00030 int main()
00031 {
00032     //First initalise joystick
00033     joystick.init();
00034 
00035     while (true) {
00036         //Stores x and y output values
00037         float x, y;
00038 
00039         //Calls the "GetXValue" and "GetYValue" and stores it in x and y
00040         x = joystick.GetXValue();
00041         y = joystick.GetYValue();
00042 
00043         //Prints the values to the terminal
00044         pc.printf("X: %f, Y: %f\n", x, y);
00045 
00046         //If the button flag is pressed switch the led
00047         if (joystick.get_button_flag()) {
00048             r_led = !r_led
00049         }
00050     }
00051 }
00052  * @endcode
00053 */
00054 
00055 class Joystick
00056 {
00057 public:
00058     /** Creates a Joystick object connected to the given pins
00059     * Dynamically allocates AnalogIn for input potentiometers, InterruptIn for the joystick buton and the debounce Timeout
00060     *
00061     * @param x_axis_pin connected to the Joystick's x potentiometer output
00062     * @param y_axis_pin connected to the Joystick's y potentiometer output
00063     * @param button_pin connected to the Joystick's button
00064     *
00065     */
00066     Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin);
00067 
00068     /** Destroys the Joystick object
00069     * Clears the AnalogIn's, InterruptIn and Timeout from memory
00070     */
00071     ~Joystick();
00072 
00073     /** Initalises the Joystick
00074     * Sets up the InterruptIn Mode ISR
00075     * Initalises the offset vairables and ISR flags
00076     * Samples the AnalogIn's 5 times and takes an average to get the offset
00077     */
00078     void init();
00079 
00080     /** Gets the value of the x potentiometer
00081     * Takes 5 readings from the potentiometer 
00082     * Calculates the average measurement, accounting for joystick offset
00083     * Caps the average between 0 and 1
00084     * 
00085     * @returns the average value of the x potentiometer
00086     */
00087     float GetXValue();
00088 
00089     /** Gets the value of the y potentiometer
00090     * Takes 5 readings from the potentiometer 
00091     * Calculates the average measurement, accounting for joystick offset
00092     * Caps the average between 0 and 1
00093     * 
00094     * @returns the average value of the y potentiometer
00095     */
00096     float GetYValue();
00097 
00098     /** Reads the state of the button flag
00099     * @returns the button flag
00100     */
00101     int get_button_flag();
00102 
00103     /** Sets the button flag
00104     * @param value The value the flag will be set to
00105     */
00106     void set_button_flag(bool value);
00107 
00108 private:
00109     void button_isr();
00110     void button_fall_isr();
00111     void button_debounce_isr();
00112 private:
00113     //Pin inputs
00114     AnalogIn* x_axis_;
00115     AnalogIn* y_axis_;
00116     InterruptIn* button_;
00117 
00118     //Ticker to prevent joystick button bounce
00119     Timeout* button_debounce_;
00120 
00121     //Stores X and Y offsets
00122     float x_offset_;
00123     float y_offset_;
00124 
00125     //Stores interrupt flags
00126     volatile bool g_button_flag_;
00127     volatile bool g_button_debounce_flag_;
00128 };
00129 
00130 #endif