Gets potentiometer and button reading from a joystick (https://proto-pic.co.uk/thumb-joystick-analogue/)

Dependents:   L2_SpaceInvaders 6-Joystick

Committer:
avi23
Date:
Thu Apr 28 14:14:22 2016 +0000
Revision:
0:56a82b52e0d4
Child:
1:78d3e8b50d19
First Commit - Joystick class from main converted to a library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
avi23 0:56a82b52e0d4 1 //Joystick class - Header file
avi23 0:56a82b52e0d4 2 //Define guards
avi23 0:56a82b52e0d4 3 #ifndef JOYSTICK_H
avi23 0:56a82b52e0d4 4 #define JOYSTICK_H
avi23 0:56a82b52e0d4 5
avi23 0:56a82b52e0d4 6 #include "mbed.h"
avi23 0:56a82b52e0d4 7
avi23 0:56a82b52e0d4 8 class Joystick
avi23 0:56a82b52e0d4 9 {
avi23 0:56a82b52e0d4 10 public: //Public Functions
avi23 0:56a82b52e0d4 11 //Constructor - Called when the object is created
avi23 0:56a82b52e0d4 12 //Dynamically allocates AnalogIn for input potentiometers, InterruptIn for the joystick buton and the debounce Timeout
avi23 0:56a82b52e0d4 13 Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin);
avi23 0:56a82b52e0d4 14
avi23 0:56a82b52e0d4 15 //Deconstructor - Called when the object is destroyed
avi23 0:56a82b52e0d4 16 //Clears the AnalogIn's, InterruptIn and Timeout from memory
avi23 0:56a82b52e0d4 17 ~Joystick();
avi23 0:56a82b52e0d4 18
avi23 0:56a82b52e0d4 19 //Initalises the Joystick
avi23 0:56a82b52e0d4 20 //Sets up the InterruptIn Mode ISR
avi23 0:56a82b52e0d4 21 //Initalises the offset vairables and ISR flags
avi23 0:56a82b52e0d4 22 //Samples the AnalogIn's 5 times and takes an average to get the offset
avi23 0:56a82b52e0d4 23 void init();
avi23 0:56a82b52e0d4 24
avi23 0:56a82b52e0d4 25 //Take 5 readings of the X potentiomenter
avi23 0:56a82b52e0d4 26 //Returns the average measurement, accounting for joystick offset x and y values
avi23 0:56a82b52e0d4 27 //Caps the returned value between 0-1
avi23 0:56a82b52e0d4 28 float GetXValue();
avi23 0:56a82b52e0d4 29
avi23 0:56a82b52e0d4 30 //Take 5 readings of the Y potentiomenter
avi23 0:56a82b52e0d4 31 //Returns the average measurement, accounting for joystick offset x and y values
avi23 0:56a82b52e0d4 32 //Caps the returned value between 0-1
avi23 0:56a82b52e0d4 33 float GetYValue();
avi23 0:56a82b52e0d4 34
avi23 0:56a82b52e0d4 35 //Gets the button flag
avi23 0:56a82b52e0d4 36 int get_button_flag();
avi23 0:56a82b52e0d4 37
avi23 0:56a82b52e0d4 38 //Sets the button flag
avi23 0:56a82b52e0d4 39 void set_button_flag(bool value);
avi23 0:56a82b52e0d4 40
avi23 0:56a82b52e0d4 41 private: //Private Functions
avi23 0:56a82b52e0d4 42 //Button ISR Method
avi23 0:56a82b52e0d4 43 //Only sets the button flag if the debounce flag isn't set
avi23 0:56a82b52e0d4 44 //While setting the button flag, set the debounce flag and configures the debounce timeout to fire in 0.2s, clearing the debounce flag
avi23 0:56a82b52e0d4 45 void button_isr();
avi23 0:56a82b52e0d4 46
avi23 0:56a82b52e0d4 47 //Button debounce ISR method
avi23 0:56a82b52e0d4 48 void button_debounce_isr();
avi23 0:56a82b52e0d4 49
avi23 0:56a82b52e0d4 50 private: //Private pointers and vairables
avi23 0:56a82b52e0d4 51 //Pin inputs
avi23 0:56a82b52e0d4 52 AnalogIn* x_axis_;
avi23 0:56a82b52e0d4 53 AnalogIn* y_axis_;
avi23 0:56a82b52e0d4 54 InterruptIn* button_;
avi23 0:56a82b52e0d4 55
avi23 0:56a82b52e0d4 56 //Ticker to prevent joystick button bounce
avi23 0:56a82b52e0d4 57 Timeout* button_debounce_;
avi23 0:56a82b52e0d4 58
avi23 0:56a82b52e0d4 59 //Stores X and Y offsets
avi23 0:56a82b52e0d4 60 float x_offset_;
avi23 0:56a82b52e0d4 61 float y_offset_;
avi23 0:56a82b52e0d4 62
avi23 0:56a82b52e0d4 63 //Stores interrupt flags
avi23 0:56a82b52e0d4 64 volatile bool g_button_flag_;
avi23 0:56a82b52e0d4 65 volatile bool g_button_debounce_flag_;
avi23 0:56a82b52e0d4 66 };