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.cpp
00001 /** 00002 @file Joystick.cpp 00003 00004 @brief Member functions implementations 00005 @brief Fixed button 00006 00007 */ 00008 #include "Joystick.h" 00009 #include "mbed.h" 00010 00011 Joystick::Joystick(PinName x_axis_pin, PinName y_axis_pin, PinName button_pin) 00012 { 00013 x_axis_ = new AnalogIn(x_axis_pin); 00014 y_axis_ = new AnalogIn(y_axis_pin); 00015 button_ = new DigitalIn(button_pin); 00016 } 00017 00018 Joystick::~Joystick() 00019 { 00020 delete x_axis_; 00021 delete y_axis_; 00022 delete button_; 00023 } 00024 00025 void Joystick::init() 00026 { 00027 //Sets up the button ISR 00028 button_->mode(PullUp); 00029 //Initalises the vairables and flags 00030 x_offset_ = 0; 00031 y_offset_ = 0; 00032 g_button_flag_ = 0; 00033 00034 //Samples the joystick 5 times and takes an average to get the offset 00035 float x_sum = 0; 00036 float y_sum = 0; 00037 00038 for (int i = 0; i < 5; ++i) { 00039 x_sum += x_axis_->read(); 00040 y_sum += y_axis_->read(); 00041 } 00042 00043 x_offset_ = 0.5f - x_sum/5.0f; 00044 y_offset_ = 0.5f - y_sum/5.0f; 00045 } 00046 00047 float Joystick::GetXValue() 00048 { 00049 00050 float x_sum = 0; 00051 //Iterates 5 times and calculates an average 00052 for (int i = 0; i < 5; ++i) { 00053 x_sum += x_axis_->read(); 00054 } 00055 00056 float x_value = x_sum/5.0f + x_offset_; 00057 00058 //Caps the value for the POT between 0 and 1 00059 if (x_value < 0.0f) { 00060 return 0; 00061 } else if (x_value > 1.0f) { 00062 return 1; 00063 } else { 00064 return x_value; 00065 } 00066 } 00067 00068 float Joystick::GetYValue() 00069 { 00070 00071 float y_sum = 0; 00072 //Iterates 5 times and calculates an average 00073 for (int i = 0; i < 5; ++i) { 00074 y_sum += y_axis_->read(); 00075 } 00076 00077 float y_value = y_sum/5.0f + y_offset_; 00078 00079 //Caps the value for the POT between 0 and 1 00080 if (y_value < 0.0f) { 00081 return 0; 00082 } else if (y_value > 1.0f) { 00083 return 1; 00084 } else { 00085 return y_value; 00086 } 00087 } 00088 00089 bool Joystick::get_button_flag() 00090 { 00091 g_button_flag_ = button_ -> read(); 00092 return g_button_flag_; 00093 }
Generated on Sat Jul 30 2022 00:01:50 by
