Progra Mator / Joystick_fixed
Revision:
0:56a82b52e0d4
Child:
1:78d3e8b50d19
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Joystick.h	Thu Apr 28 14:14:22 2016 +0000
@@ -0,0 +1,66 @@
+//Joystick class - Header file
+//Define guards
+#ifndef JOYSTICK_H
+#define JOYSTICK_H
+
+#include "mbed.h"
+
+class Joystick
+{
+public: //Public Functions
+    //Constructor - Called when the object is created
+    //Dynamically allocates AnalogIn for input potentiometers, InterruptIn for the joystick buton and the debounce Timeout
+    Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin);
+
+    //Deconstructor - Called when the object is destroyed
+    //Clears the AnalogIn's, InterruptIn and Timeout from memory
+    ~Joystick();
+
+    //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
+    void init();
+
+    //Take 5 readings of the X potentiomenter
+    //Returns the average measurement, accounting for joystick offset x and y values
+    //Caps the returned value between 0-1
+    float GetXValue();
+
+    //Take 5 readings of the Y potentiomenter
+    //Returns the average measurement, accounting for joystick offset x and y values
+    //Caps the returned value between 0-1
+    float GetYValue();
+
+    //Gets the button flag
+    int get_button_flag();
+
+    //Sets the button flag
+    void set_button_flag(bool value);
+
+private: //Private Functions
+    //Button ISR Method
+    //Only sets the button flag if the debounce flag isn't set
+    //While setting the button flag, set the debounce flag and configures the debounce timeout to fire in 0.2s, clearing the debounce flag
+    void button_isr();
+
+    //Button debounce ISR method
+    void button_debounce_isr();
+
+private: //Private pointers and vairables
+    //Pin inputs
+    AnalogIn* x_axis_;
+    AnalogIn* y_axis_;
+    InterruptIn* button_;
+
+    //Ticker to prevent joystick button bounce
+    Timeout* button_debounce_;
+
+    //Stores X and Y offsets
+    float x_offset_;
+    float y_offset_;
+
+    //Stores interrupt flags
+    volatile bool g_button_flag_;
+    volatile bool g_button_debounce_flag_;
+};
\ No newline at end of file