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.
Dependents: L2_SpaceInvaders 6-Joystick
Joystick.cpp
00001 /** 00002 @file Joystick.cpp 00003 00004 @brief Member functions implementations 00005 00006 */ 00007 #include "Joystick.h" 00008 #include "mbed.h" 00009 00010 Joystick::Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin) 00011 { 00012 x_axis_ = new AnalogIn(x_axis_pin); 00013 y_axis_ = new AnalogIn(y_axis_pin); 00014 button_ = new InterruptIn(button_pin); 00015 button_debounce_ = new Timeout(); 00016 } 00017 00018 Joystick::~Joystick() 00019 { 00020 delete x_axis_; 00021 delete y_axis_; 00022 delete button_; 00023 delete button_debounce_; 00024 } 00025 00026 void Joystick::init() 00027 { 00028 //Sets up the button ISR 00029 button_->mode(PullDown); 00030 button_->rise(this, &Joystick::button_isr); 00031 button_->fall(this, &Joystick::button_fall_isr); 00032 00033 //Initalises the vairables and flags 00034 x_offset_ = 0; 00035 y_offset_ = 0; 00036 g_button_flag_ = false; 00037 g_button_debounce_flag_ = false; 00038 00039 //Samples the joystick 5 times and takes an average to get the offset 00040 float x_sum = 0; 00041 float y_sum = 0; 00042 00043 for (int i = 0; i < 5; ++i) { 00044 x_sum += x_axis_->read(); 00045 y_sum += y_axis_->read(); 00046 } 00047 00048 x_offset_ = 0.5f - x_sum/5.0f; 00049 y_offset_ = 0.5f - y_sum/5.0f; 00050 } 00051 00052 float Joystick::GetXValue() 00053 { 00054 00055 float x_sum = 0; 00056 //Iterates 5 times and calculates an average 00057 for (int i = 0; i < 5; ++i) { 00058 x_sum += x_axis_->read(); 00059 } 00060 00061 float x_value = x_sum/5.0f + x_offset_; 00062 00063 //Caps the value for the POT between 0 and 1 00064 if (x_value < 0.0f) { 00065 return 0; 00066 } else if (x_value > 1.0f) { 00067 return 1; 00068 } else { 00069 return x_value; 00070 } 00071 } 00072 00073 float Joystick::GetYValue() 00074 { 00075 00076 float y_sum = 0; 00077 //Iterates 5 times and calculates an average 00078 for (int i = 0; i < 5; ++i) { 00079 y_sum += y_axis_->read(); 00080 } 00081 00082 float y_value = y_sum/5.0f + y_offset_; 00083 00084 //Caps the value for the POT between 0 and 1 00085 if (y_value < 0.0f) { 00086 return 0; 00087 } else if (y_value > 1.0f) { 00088 return 1; 00089 } else { 00090 return y_value; 00091 } 00092 } 00093 00094 int Joystick::get_button_flag() 00095 { 00096 return g_button_flag_; 00097 } 00098 00099 void Joystick::set_button_flag(bool value) 00100 { 00101 g_button_flag_ = value; 00102 } 00103 00104 void Joystick::button_isr() 00105 { 00106 if (!g_button_debounce_flag_) { 00107 g_button_flag_ = true; 00108 g_button_debounce_flag_ = true; 00109 } 00110 } 00111 00112 void Joystick::button_fall_isr() 00113 { 00114 button_debounce_->attach(this, &Joystick::button_debounce_isr, 0.125); 00115 } 00116 00117 void Joystick::button_debounce_isr() 00118 { 00119 g_button_debounce_flag_ = false; 00120 }
Generated on Tue Jul 12 2022 16:02:17 by
