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.
Joystick.h@2:c9f2a9e2f304, 2016-05-05 (annotated)
- Committer:
 - avi23
 - Date:
 - Thu May 05 11:00:46 2016 +0000
 - Revision:
 - 2:c9f2a9e2f304
 - Parent:
 - 1:78d3e8b50d19
 - Child:
 - 3:82731de4e878
 
Changed debounce timeout to attach on the falling edge of the joystick. Prevents debounce when button is held down
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| avi23 | 1:78d3e8b50d19 | 1 | /** | 
| avi23 | 1:78d3e8b50d19 | 2 | @file Joystick.h | 
| avi23 | 1:78d3e8b50d19 | 3 | @brief Joysitck header file containing member functions and variables | 
| avi23 | 1:78d3e8b50d19 | 4 | */ | 
| avi23 | 0:56a82b52e0d4 | 5 | //Joystick class - Header file | 
| avi23 | 0:56a82b52e0d4 | 6 | //Define guards | 
| avi23 | 0:56a82b52e0d4 | 7 | #ifndef JOYSTICK_H | 
| avi23 | 0:56a82b52e0d4 | 8 | #define JOYSTICK_H | 
| avi23 | 0:56a82b52e0d4 | 9 | |
| avi23 | 0:56a82b52e0d4 | 10 | #include "mbed.h" | 
| avi23 | 0:56a82b52e0d4 | 11 | |
| avi23 | 1:78d3e8b50d19 | 12 | /** | 
| avi23 | 1:78d3e8b50d19 | 13 | @brief Library for interfacing with a Joystick (http://proto-pic.co.uk/thumb-joystick-analogue/) | 
| avi23 | 1:78d3e8b50d19 | 14 | @brief Joystick is just 2 potentiometers and a button so can be interfaced with AnalogIn and DigitalIn/InterruptIn | 
| avi23 | 1:78d3e8b50d19 | 15 | @brief The library contains a method to prevent button debounce with a Timeout | 
| avi23 | 1:78d3e8b50d19 | 16 | |
| avi23 | 1:78d3e8b50d19 | 17 | @author Avinash Patel | 
| avi23 | 1:78d3e8b50d19 | 18 | @date April 2016 | 
| avi23 | 1:78d3e8b50d19 | 19 | * Example | 
| avi23 | 1:78d3e8b50d19 | 20 | * @code | 
| avi23 | 1:78d3e8b50d19 | 21 | |
| avi23 | 1:78d3e8b50d19 | 22 | #include "mbed.h" | 
| avi23 | 1:78d3e8b50d19 | 23 | #include "Joystick.h" | 
| avi23 | 1:78d3e8b50d19 | 24 | |
| avi23 | 1:78d3e8b50d19 | 25 | // Xaxis,Yaxis,Button | 
| avi23 | 1:78d3e8b50d19 | 26 | Joystick joystick(PTB3, PTB2, PTB11); | 
| avi23 | 1:78d3e8b50d19 | 27 | DigitalOut r_led(LED_RED); | 
| avi23 | 1:78d3e8b50d19 | 28 | Serial pc(USBTX, USBRX); | 
| avi23 | 1:78d3e8b50d19 | 29 | |
| avi23 | 1:78d3e8b50d19 | 30 | int main() | 
| avi23 | 1:78d3e8b50d19 | 31 | { | 
| avi23 | 1:78d3e8b50d19 | 32 | //First initalise joystick | 
| avi23 | 1:78d3e8b50d19 | 33 | joystick.init(); | 
| avi23 | 1:78d3e8b50d19 | 34 | |
| avi23 | 1:78d3e8b50d19 | 35 | while (true) { | 
| avi23 | 1:78d3e8b50d19 | 36 | //Stores x and y output values | 
| avi23 | 1:78d3e8b50d19 | 37 | float x, y; | 
| avi23 | 1:78d3e8b50d19 | 38 | |
| avi23 | 1:78d3e8b50d19 | 39 | //Calls the "GetXValue" and "GetYValue" and stores it in x and y | 
| avi23 | 1:78d3e8b50d19 | 40 | x = joystick.GetXValue(); | 
| avi23 | 1:78d3e8b50d19 | 41 | y = joystick.GetYValue(); | 
| avi23 | 1:78d3e8b50d19 | 42 | |
| avi23 | 1:78d3e8b50d19 | 43 | //Prints the values to the terminal | 
| avi23 | 1:78d3e8b50d19 | 44 | pc.printf("X: %f, Y: %f\n", x, y); | 
| avi23 | 1:78d3e8b50d19 | 45 | |
| avi23 | 1:78d3e8b50d19 | 46 | //If the button flag is pressed switch the led | 
| avi23 | 1:78d3e8b50d19 | 47 | if (joystick.get_button_flag()) { | 
| avi23 | 1:78d3e8b50d19 | 48 | r_led = !r_led | 
| avi23 | 1:78d3e8b50d19 | 49 | } | 
| avi23 | 1:78d3e8b50d19 | 50 | } | 
| avi23 | 1:78d3e8b50d19 | 51 | } | 
| avi23 | 1:78d3e8b50d19 | 52 | * @endcode | 
| avi23 | 1:78d3e8b50d19 | 53 | */ | 
| avi23 | 1:78d3e8b50d19 | 54 | |
| avi23 | 0:56a82b52e0d4 | 55 | class Joystick | 
| avi23 | 0:56a82b52e0d4 | 56 | { | 
| avi23 | 1:78d3e8b50d19 | 57 | public: | 
| avi23 | 1:78d3e8b50d19 | 58 | /** Creates a Joystick object connected to the given pins | 
| avi23 | 1:78d3e8b50d19 | 59 | * Dynamically allocates AnalogIn for input potentiometers, InterruptIn for the joystick buton and the debounce Timeout | 
| avi23 | 1:78d3e8b50d19 | 60 | * | 
| avi23 | 1:78d3e8b50d19 | 61 | * @param x_axis_pin connected to the Joystick's x potentiometer output | 
| avi23 | 1:78d3e8b50d19 | 62 | * @param y_axis_pin connected to the Joystick's y potentiometer output | 
| avi23 | 1:78d3e8b50d19 | 63 | * @param button_pin connected to the Joystick's button | 
| avi23 | 1:78d3e8b50d19 | 64 | * | 
| avi23 | 1:78d3e8b50d19 | 65 | */ | 
| avi23 | 0:56a82b52e0d4 | 66 | Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin); | 
| avi23 | 0:56a82b52e0d4 | 67 | |
| avi23 | 1:78d3e8b50d19 | 68 | /** Destroys the Joystick object | 
| avi23 | 1:78d3e8b50d19 | 69 | * Clears the AnalogIn's, InterruptIn and Timeout from memory | 
| avi23 | 1:78d3e8b50d19 | 70 | */ | 
| avi23 | 0:56a82b52e0d4 | 71 | ~Joystick(); | 
| avi23 | 0:56a82b52e0d4 | 72 | |
| avi23 | 1:78d3e8b50d19 | 73 | /** Initalises the Joystick | 
| avi23 | 1:78d3e8b50d19 | 74 | * Sets up the InterruptIn Mode ISR | 
| avi23 | 1:78d3e8b50d19 | 75 | * Initalises the offset vairables and ISR flags | 
| avi23 | 1:78d3e8b50d19 | 76 | * Samples the AnalogIn's 5 times and takes an average to get the offset | 
| avi23 | 1:78d3e8b50d19 | 77 | */ | 
| avi23 | 0:56a82b52e0d4 | 78 | void init(); | 
| avi23 | 0:56a82b52e0d4 | 79 | |
| avi23 | 1:78d3e8b50d19 | 80 | /** Gets the value of the x potentiometer | 
| avi23 | 1:78d3e8b50d19 | 81 | * Takes 5 readings from the potentiometer | 
| avi23 | 1:78d3e8b50d19 | 82 | * Calculates the average measurement, accounting for joystick offset | 
| avi23 | 1:78d3e8b50d19 | 83 | * Caps the average between 0 and 1 | 
| avi23 | 1:78d3e8b50d19 | 84 | * | 
| avi23 | 1:78d3e8b50d19 | 85 | * @returns the average value of the x potentiometer | 
| avi23 | 1:78d3e8b50d19 | 86 | */ | 
| avi23 | 0:56a82b52e0d4 | 87 | float GetXValue(); | 
| avi23 | 0:56a82b52e0d4 | 88 | |
| avi23 | 1:78d3e8b50d19 | 89 | /** Gets the value of the y potentiometer | 
| avi23 | 1:78d3e8b50d19 | 90 | * Takes 5 readings from the potentiometer | 
| avi23 | 1:78d3e8b50d19 | 91 | * Calculates the average measurement, accounting for joystick offset | 
| avi23 | 1:78d3e8b50d19 | 92 | * Caps the average between 0 and 1 | 
| avi23 | 1:78d3e8b50d19 | 93 | * | 
| avi23 | 1:78d3e8b50d19 | 94 | * @returns the average value of the y potentiometer | 
| avi23 | 1:78d3e8b50d19 | 95 | */ | 
| avi23 | 0:56a82b52e0d4 | 96 | float GetYValue(); | 
| avi23 | 0:56a82b52e0d4 | 97 | |
| avi23 | 1:78d3e8b50d19 | 98 | /** Reads the state of the button flag | 
| avi23 | 1:78d3e8b50d19 | 99 | * @returns the button flag | 
| avi23 | 1:78d3e8b50d19 | 100 | */ | 
| avi23 | 0:56a82b52e0d4 | 101 | int get_button_flag(); | 
| avi23 | 0:56a82b52e0d4 | 102 | |
| avi23 | 1:78d3e8b50d19 | 103 | /** Sets the button flag | 
| avi23 | 1:78d3e8b50d19 | 104 | * @param value The value the flag will be set to | 
| avi23 | 1:78d3e8b50d19 | 105 | */ | 
| avi23 | 0:56a82b52e0d4 | 106 | void set_button_flag(bool value); | 
| avi23 | 0:56a82b52e0d4 | 107 | |
| avi23 | 1:78d3e8b50d19 | 108 | private: | 
| avi23 | 0:56a82b52e0d4 | 109 | void button_isr(); | 
| avi23 | 2:c9f2a9e2f304 | 110 | void button_fall_isr(); | 
| avi23 | 0:56a82b52e0d4 | 111 | void button_debounce_isr(); | 
| avi23 | 1:78d3e8b50d19 | 112 | private: | 
| avi23 | 0:56a82b52e0d4 | 113 | //Pin inputs | 
| avi23 | 0:56a82b52e0d4 | 114 | AnalogIn* x_axis_; | 
| avi23 | 0:56a82b52e0d4 | 115 | AnalogIn* y_axis_; | 
| avi23 | 0:56a82b52e0d4 | 116 | InterruptIn* button_; | 
| avi23 | 0:56a82b52e0d4 | 117 | |
| avi23 | 0:56a82b52e0d4 | 118 | //Ticker to prevent joystick button bounce | 
| avi23 | 0:56a82b52e0d4 | 119 | Timeout* button_debounce_; | 
| avi23 | 0:56a82b52e0d4 | 120 | |
| avi23 | 0:56a82b52e0d4 | 121 | //Stores X and Y offsets | 
| avi23 | 0:56a82b52e0d4 | 122 | float x_offset_; | 
| avi23 | 0:56a82b52e0d4 | 123 | float y_offset_; | 
| avi23 | 0:56a82b52e0d4 | 124 | |
| avi23 | 0:56a82b52e0d4 | 125 | //Stores interrupt flags | 
| avi23 | 0:56a82b52e0d4 | 126 | volatile bool g_button_flag_; | 
| avi23 | 0:56a82b52e0d4 | 127 | volatile bool g_button_debounce_flag_; | 
| avi23 | 1:78d3e8b50d19 | 128 | }; | 
| avi23 | 1:78d3e8b50d19 | 129 | |
| avi23 | 1:78d3e8b50d19 | 130 | #endif |