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

Dependents:   L2_SpaceInvaders 6-Joystick

Files at this revision

API Documentation at this revision

Comitter:
avi23
Date:
Thu May 05 11:00:46 2016 +0000
Parent:
1:78d3e8b50d19
Commit message:
Changed debounce timeout to attach on the falling edge of the joystick. Prevents debounce when button is held down

Changed in this revision

Joystick.cpp Show annotated file Show diff for this revision Revisions of this file
Joystick.h Show annotated file Show diff for this revision Revisions of this file
diff -r 78d3e8b50d19 -r c9f2a9e2f304 Joystick.cpp
--- a/Joystick.cpp	Mon May 02 16:27:49 2016 +0000
+++ b/Joystick.cpp	Thu May 05 11:00:46 2016 +0000
@@ -7,102 +7,114 @@
 #include "Joystick.h"
 #include "mbed.h"
 
-Joystick::Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin) {
-        x_axis_ = new AnalogIn(x_axis_pin);
-        y_axis_ = new AnalogIn(y_axis_pin);
-        button_ = new InterruptIn(button_pin);
-        button_debounce_ = new Timeout();
+Joystick::Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin) 
+{
+    x_axis_ = new AnalogIn(x_axis_pin);
+    y_axis_ = new AnalogIn(y_axis_pin);
+    button_ = new InterruptIn(button_pin);
+    button_debounce_ = new Timeout();
 }
 
-Joystick::~Joystick() {
-        delete x_axis_;
-        delete y_axis_;
-        delete button_;
-        delete button_debounce_;
+Joystick::~Joystick() 
+{
+    delete x_axis_;
+    delete y_axis_;
+    delete button_;
+    delete button_debounce_;
 }
 
-void Joystick::init() {
+void Joystick::init() 
+{
         //Sets up the button ISR
-        button_->mode(PullDown);
-        button_->rise(this, &Joystick::button_isr);
+    button_->mode(PullDown);
+    button_->rise(this, &Joystick::button_isr);
+    button_->fall(this, &Joystick::button_fall_isr);
 
         //Initalises the vairables and flags
-        x_offset_ = 0;
-        y_offset_ = 0;
-        g_button_flag_ = false;
-        g_button_debounce_flag_ = false;
+    x_offset_ = 0;
+    y_offset_ = 0;
+    g_button_flag_ = false;
+    g_button_debounce_flag_ = false;
 
         //Samples the joystick 5 times and takes an average to get the offset
-        float x_sum = 0;
-        float y_sum = 0;
+    float x_sum = 0;
+    float y_sum = 0;
 
-        for (int i = 0; i < 5; ++i) {
-            x_sum += x_axis_->read();
-            y_sum += y_axis_->read();
-        }
+    for (int i = 0; i < 5; ++i) {
+        x_sum += x_axis_->read();
+        y_sum += y_axis_->read();
+    }
 
-        x_offset_ = 0.5f - x_sum/5.0f;
-        y_offset_ = 0.5f - y_sum/5.0f;
+    x_offset_ = 0.5f - x_sum/5.0f;
+    y_offset_ = 0.5f - y_sum/5.0f;
 }
 
-float Joystick::GetXValue() {
+float Joystick::GetXValue() 
+{
 
-        float x_sum = 0;
+    float x_sum = 0;
         //Iterates 5 times and calculates an average
-        for (int i = 0; i < 5; ++i) {
-            x_sum += x_axis_->read();
-        }
+    for (int i = 0; i < 5; ++i) {
+        x_sum += x_axis_->read();
+    }
 
-        float x_value = x_sum/5.0f + x_offset_;
+    float x_value = x_sum/5.0f + x_offset_;
 
         //Caps the value for the POT between 0 and 1
-        if (x_value < 0.0f) {
-            return 0;
-        } else if (x_value > 1.0f) {
-            return 1;
-        } else {
-            return x_value;
-        }
+    if (x_value < 0.0f) {
+        return 0;
+    } else if (x_value > 1.0f) {
+        return 1;
+    } else {
+        return x_value;
+    }
 }
 
-float Joystick::GetYValue() {
+float Joystick::GetYValue() 
+{
 
-        float y_sum = 0;
+    float y_sum = 0;
         //Iterates 5 times and calculates an average
-        for (int i = 0; i < 5; ++i) {
-            y_sum += y_axis_->read();
-        }
+    for (int i = 0; i < 5; ++i) {
+        y_sum += y_axis_->read();
+    }
 
-        float y_value = y_sum/5.0f + y_offset_;
+    float y_value = y_sum/5.0f + y_offset_;
 
         //Caps the value for the POT between 0 and 1
-        if (y_value < 0.0f) {
-            return 0;
-        } else if (y_value > 1.0f) {
-            return 1;
-        } else {
-            return y_value;
-        }
+    if (y_value < 0.0f) {
+        return 0;
+    } else if (y_value > 1.0f) {
+        return 1;
+    } else {
+        return y_value;
+    }
 }
 
-int Joystick::get_button_flag() {
-        return g_button_flag_;
+int Joystick::get_button_flag() 
+{
+    return g_button_flag_;
 }
 
-void Joystick::set_button_flag(bool value) {
-        g_button_flag_ = value;
+void Joystick::set_button_flag(bool value) 
+{
+    g_button_flag_ = value;
 }
 
-void Joystick::button_isr() {
-        if (!g_button_debounce_flag_) {
-            g_button_flag_ = true;
-            g_button_debounce_flag_ = true;
-
-            //Sets up the debounce ticker to fire in 0.2s
-            button_debounce_->attach(this, &Joystick::button_debounce_isr, 0.2);
-        }
+void Joystick::button_isr() 
+{
+    if (!g_button_debounce_flag_) {
+        g_button_flag_ = true;
+        g_button_debounce_flag_ = true;
+    }
 }
 
-void Joystick::button_debounce_isr() {
-        g_button_debounce_flag_ = false;
+void Joystick::button_fall_isr() 
+{
+    button_debounce_->attach(this, &Joystick::button_debounce_isr, 0.125);
+}
+
+void Joystick::button_debounce_isr() 
+{
+    g_button_debounce_flag_ = false;
 }
\ No newline at end of file
diff -r 78d3e8b50d19 -r c9f2a9e2f304 Joystick.h
--- a/Joystick.h	Mon May 02 16:27:49 2016 +0000
+++ b/Joystick.h	Thu May 05 11:00:46 2016 +0000
@@ -107,6 +107,7 @@
 
 private:
     void button_isr();
+    void button_fall_isr();
     void button_debounce_isr();
 private:
     //Pin inputs