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@4:9553952c95f5, 2020-06-07 (annotated)
- Committer:
 - zaikabadian
 - Date:
 - Sun Jun 07 17:07:22 2020 +0000
 - Revision:
 - 4:9553952c95f5
 - Parent:
 - 3:82731de4e878
 - Child:
 - 5:36cca0038ae3
 
New Example
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 | |
| zaikabadian | 3:82731de4e878 | 17 | @author Avinash Patel and fixer zayats1 | 
| 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" | 
| zaikabadian | 4:9553952c95f5 | 24 | //I use STM32Nucleo L476 RG | 
| 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 | |
| zaikabadian | 4:9553952c95f5 | 30 | #include "Joystick.h" | 
| zaikabadian | 4:9553952c95f5 | 31 | //Led1 switching task | 
| zaikabadian | 4:9553952c95f5 | 32 | //I tired, so no fixation for button click | 
| zaikabadian | 4:9553952c95f5 | 33 | Thread thread1; | 
| zaikabadian | 4:9553952c95f5 | 34 | const int PWMPERIODMS = 20; //0,02s | 
| zaikabadian | 4:9553952c95f5 | 35 | Joystick joy(PA_4,PB_0,PC_1); | 
| zaikabadian | 4:9553952c95f5 | 36 | Serial pc(USBTX, USBRX); | 
| zaikabadian | 4:9553952c95f5 | 37 | DigitalOut Led1(PB_10); | 
| zaikabadian | 4:9553952c95f5 | 38 | PwmOut Led2(PB_4); | 
| zaikabadian | 4:9553952c95f5 | 39 | PwmOut Led3(PB_3); | 
| zaikabadian | 4:9553952c95f5 | 40 | void ledToggler() | 
| zaikabadian | 4:9553952c95f5 | 41 | { | 
| zaikabadian | 4:9553952c95f5 | 42 | while(1) { | 
| zaikabadian | 4:9553952c95f5 | 43 | int button = joy.get_button_flag(); | 
| zaikabadian | 4:9553952c95f5 | 44 | //Switching Led | 
| zaikabadian | 4:9553952c95f5 | 45 | if (button == 0) Led1 = 1; | 
| zaikabadian | 4:9553952c95f5 | 46 | else Led1 = 0; | 
| zaikabadian | 4:9553952c95f5 | 47 | } | 
| zaikabadian | 4:9553952c95f5 | 48 | } | 
| avi23 | 1:78d3e8b50d19 | 49 | int main() | 
| avi23 | 1:78d3e8b50d19 | 50 | { | 
| zaikabadian | 4:9553952c95f5 | 51 | Led2.period_ms(PWMPERIODMS); | 
| zaikabadian | 4:9553952c95f5 | 52 | Led3.period_ms(PWMPERIODMS); | 
| zaikabadian | 4:9553952c95f5 | 53 | joy.init(); | 
| zaikabadian | 4:9553952c95f5 | 54 | thread1.start(ledToggler); | 
| zaikabadian | 4:9553952c95f5 | 55 | while(1) { | 
| zaikabadian | 4:9553952c95f5 | 56 | float x = joy.GetXValue(); | 
| zaikabadian | 4:9553952c95f5 | 57 | float y = joy.GetYValue(); | 
| avi23 | 1:78d3e8b50d19 | 58 | pc.printf("X: %f, Y: %f\n", x, y); | 
| zaikabadian | 4:9553952c95f5 | 59 | //in Mbed OS PWM uses analogue values | 
| zaikabadian | 4:9553952c95f5 | 60 | Led2 = x; | 
| zaikabadian | 4:9553952c95f5 | 61 | Led3 = y; | 
| avi23 | 1:78d3e8b50d19 | 62 | } | 
| avi23 | 1:78d3e8b50d19 | 63 | } | 
| avi23 | 1:78d3e8b50d19 | 64 | * @endcode | 
| avi23 | 1:78d3e8b50d19 | 65 | */ | 
| avi23 | 1:78d3e8b50d19 | 66 | |
| avi23 | 0:56a82b52e0d4 | 67 | class Joystick | 
| avi23 | 0:56a82b52e0d4 | 68 | { | 
| avi23 | 1:78d3e8b50d19 | 69 | public: | 
| avi23 | 1:78d3e8b50d19 | 70 | /** Creates a Joystick object connected to the given pins | 
| avi23 | 1:78d3e8b50d19 | 71 | * Dynamically allocates AnalogIn for input potentiometers, InterruptIn for the joystick buton and the debounce Timeout | 
| avi23 | 1:78d3e8b50d19 | 72 | * | 
| avi23 | 1:78d3e8b50d19 | 73 | * @param x_axis_pin connected to the Joystick's x potentiometer output | 
| avi23 | 1:78d3e8b50d19 | 74 | * @param y_axis_pin connected to the Joystick's y potentiometer output | 
| avi23 | 1:78d3e8b50d19 | 75 | * @param button_pin connected to the Joystick's button | 
| avi23 | 1:78d3e8b50d19 | 76 | * | 
| avi23 | 1:78d3e8b50d19 | 77 | */ | 
| avi23 | 0:56a82b52e0d4 | 78 | Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin); | 
| avi23 | 0:56a82b52e0d4 | 79 | |
| avi23 | 1:78d3e8b50d19 | 80 | /** Destroys the Joystick object | 
| avi23 | 1:78d3e8b50d19 | 81 | * Clears the AnalogIn's, InterruptIn and Timeout from memory | 
| avi23 | 1:78d3e8b50d19 | 82 | */ | 
| avi23 | 0:56a82b52e0d4 | 83 | ~Joystick(); | 
| avi23 | 0:56a82b52e0d4 | 84 | |
| avi23 | 1:78d3e8b50d19 | 85 | /** Initalises the Joystick | 
| avi23 | 1:78d3e8b50d19 | 86 | * Sets up the InterruptIn Mode ISR | 
| avi23 | 1:78d3e8b50d19 | 87 | * Initalises the offset vairables and ISR flags | 
| avi23 | 1:78d3e8b50d19 | 88 | * Samples the AnalogIn's 5 times and takes an average to get the offset | 
| avi23 | 1:78d3e8b50d19 | 89 | */ | 
| avi23 | 0:56a82b52e0d4 | 90 | void init(); | 
| avi23 | 0:56a82b52e0d4 | 91 | |
| avi23 | 1:78d3e8b50d19 | 92 | /** Gets the value of the x potentiometer | 
| avi23 | 1:78d3e8b50d19 | 93 | * Takes 5 readings from the potentiometer | 
| avi23 | 1:78d3e8b50d19 | 94 | * Calculates the average measurement, accounting for joystick offset | 
| avi23 | 1:78d3e8b50d19 | 95 | * Caps the average between 0 and 1 | 
| avi23 | 1:78d3e8b50d19 | 96 | * | 
| avi23 | 1:78d3e8b50d19 | 97 | * @returns the average value of the x potentiometer | 
| avi23 | 1:78d3e8b50d19 | 98 | */ | 
| avi23 | 0:56a82b52e0d4 | 99 | float GetXValue(); | 
| avi23 | 0:56a82b52e0d4 | 100 | |
| avi23 | 1:78d3e8b50d19 | 101 | /** Gets the value of the y potentiometer | 
| avi23 | 1:78d3e8b50d19 | 102 | * Takes 5 readings from the potentiometer | 
| avi23 | 1:78d3e8b50d19 | 103 | * Calculates the average measurement, accounting for joystick offset | 
| avi23 | 1:78d3e8b50d19 | 104 | * Caps the average between 0 and 1 | 
| avi23 | 1:78d3e8b50d19 | 105 | * | 
| avi23 | 1:78d3e8b50d19 | 106 | * @returns the average value of the y potentiometer | 
| avi23 | 1:78d3e8b50d19 | 107 | */ | 
| avi23 | 0:56a82b52e0d4 | 108 | float GetYValue(); | 
| avi23 | 0:56a82b52e0d4 | 109 | |
| avi23 | 1:78d3e8b50d19 | 110 | /** Reads the state of the button flag | 
| avi23 | 1:78d3e8b50d19 | 111 | * @returns the button flag | 
| avi23 | 1:78d3e8b50d19 | 112 | */ | 
| avi23 | 0:56a82b52e0d4 | 113 | int get_button_flag(); | 
| avi23 | 0:56a82b52e0d4 | 114 | |
| avi23 | 1:78d3e8b50d19 | 115 | /** Sets the button flag | 
| avi23 | 1:78d3e8b50d19 | 116 | * @param value The value the flag will be set to | 
| avi23 | 1:78d3e8b50d19 | 117 | */ | 
| avi23 | 0:56a82b52e0d4 | 118 | |
| avi23 | 1:78d3e8b50d19 | 119 | private: | 
| avi23 | 0:56a82b52e0d4 | 120 | //Pin inputs | 
| avi23 | 0:56a82b52e0d4 | 121 | AnalogIn* x_axis_; | 
| avi23 | 0:56a82b52e0d4 | 122 | AnalogIn* y_axis_; | 
| zaikabadian | 3:82731de4e878 | 123 | DigitalIn* button_; | 
| avi23 | 0:56a82b52e0d4 | 124 | |
| avi23 | 0:56a82b52e0d4 | 125 | //Stores X and Y offsets | 
| avi23 | 0:56a82b52e0d4 | 126 | float x_offset_; | 
| avi23 | 0:56a82b52e0d4 | 127 | float y_offset_; | 
| avi23 | 0:56a82b52e0d4 | 128 | |
| zaikabadian | 3:82731de4e878 | 129 | //returns one or zero | 
| zaikabadian | 3:82731de4e878 | 130 | volatile int g_button_flag_; | 
| avi23 | 1:78d3e8b50d19 | 131 | }; | 
| avi23 | 1:78d3e8b50d19 | 132 | |
| avi23 | 1:78d3e8b50d19 | 133 | #endif |