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.
Fork of The_Children_of_Cronos_el15mggr by
Joystick.cpp
00001 #include "Joystick.h" 00002 00003 Joystick::Joystick(PinName vertPin,PinName horizPin,PinName clickPin) 00004 { 00005 vert = new AnalogIn(vertPin); 00006 horiz = new AnalogIn(horizPin); 00007 click = new InterruptIn(clickPin); 00008 } 00009 00010 void Joystick::init() 00011 { 00012 // read centred values of joystick 00013 _x0 = horiz->read(); 00014 _y0 = vert->read(); 00015 00016 // this assumes that the joystick is centred when the init function is called 00017 // if perfectly centred, the pots should read 0.5, but this may 00018 // not be the case and x0 and y0 will be used to calibrate readings 00019 00020 // turn on pull-down for button -> this assumes the other side of the button 00021 // is connected to +3V3 so we read 1 when pressed and 0 when not pressed 00022 click->mode(PullDown); 00023 // we therefore need to fire the interrupt on a rising edge 00024 click->rise(callback(this,&Joystick::click_isr)); 00025 // need to use a callback since mbed-os5 - basically tells it to look in this class for the ISR 00026 _click_flag = 0; 00027 00028 } 00029 00030 Direction Joystick::get_direction() 00031 { 00032 float angle = get_angle(); // 0 to 360, -1 for centred 00033 00034 Direction d; 00035 // partition 360 into segments and check which segment the angle is in 00036 if (angle < 0.0f) { 00037 d = CENTRE; // check for -1.0 angle 00038 } else if (angle < 22.5f) { // then keep going in 45 degree increments 00039 d = N; 00040 } else if (angle < 67.5f) { 00041 d = NE; 00042 } else if (angle < 112.5f) { 00043 d = E; 00044 } else if (angle < 157.5f) { 00045 d = SE; 00046 } else if (angle < 202.5f) { 00047 d = S; 00048 } else if (angle < 247.5f) { 00049 d = SW; 00050 } else if (angle < 292.5f) { 00051 d = W; 00052 } else if (angle < 337.5f) { 00053 d = NW; 00054 } else { 00055 d = N; 00056 } 00057 00058 return d; 00059 } 00060 00061 // this method gets the magnitude of the joystick movement 00062 float Joystick::get_mag() 00063 { 00064 Polar p = get_polar(); 00065 return p.mag; 00066 } 00067 00068 // this method gets the angle of joystick movement (0 to 360, 0 North) 00069 float Joystick::get_angle() 00070 { 00071 Polar p = get_polar(); 00072 return p.angle; 00073 } 00074 00075 // get raw joystick coordinate in range -1 to 1 00076 // Direction (x,y) 00077 // North (0,1) 00078 // East (1,0) 00079 // South (0,-1) 00080 // West (-1,0) 00081 Vector2D Joystick::get_coord() 00082 { 00083 // read() returns value in range 0.0 to 1.0 so is scaled and centre value 00084 // substracted to get values in the range -1.0 to 1.0 00085 float x = 2.0f*( horiz->read() - _x0 ); 00086 float y = 2.0f*( vert->read() - _y0 ); 00087 00088 // Note: the x value here is inverted to ensure the positive x is to the 00089 // right. This is simply due to how the potentiometer on the joystick 00090 // I was using was connected up. It could have been corrected in hardware 00091 // by swapping the power supply pins. Instead it is done in software so may 00092 // need to be changed depending on your wiring setup 00093 00094 Vector2D coord = {-x,y}; 00095 return coord; 00096 } 00097 00098 // This maps the raw x,y coord onto a circular grid. 00099 // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html 00100 Vector2D Joystick::get_mapped_coord() 00101 { 00102 Vector2D coord = get_coord(); 00103 00104 // do the transformation 00105 float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); 00106 float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); 00107 00108 Vector2D mapped_coord = {x,y}; 00109 return mapped_coord; 00110 } 00111 00112 // this function converts the mapped coordinates into polar form 00113 Polar Joystick::get_polar() 00114 { 00115 // get the mapped coordinate 00116 Vector2D coord = get_mapped_coord(); 00117 00118 // at this point, 0 degrees (i.e. x-axis) will be defined to the East. 00119 // We want 0 degrees to correspond to North and increase clockwise to 359 00120 // like a compass heading, so we need to swap the axis and invert y 00121 float x = coord.y; 00122 float y = coord.x; 00123 00124 float mag = sqrt(x*x+y*y); // pythagoras 00125 float angle = RAD2DEG*atan2(y,x); 00126 // angle will be in range -180 to 180, so add 360 to negative angles to 00127 // move to 0 to 360 range 00128 if (angle < 0.0f) { 00129 angle+=360.0f; 00130 } 00131 00132 // the noise on the ADC causes the values of x and y to fluctuate slightly 00133 // around the centred values. This causes the random angle values to get 00134 // calculated when the joystick is centred and untouched. This is also when 00135 // the magnitude is very small, so we can check for a small magnitude and then 00136 // set the angle to -1. This will inform us when the angle is invalid and the 00137 // joystick is centred 00138 00139 if (mag < TOL) { 00140 mag = 0.0f; 00141 angle = -1.0f; 00142 } 00143 00144 Polar p = {mag,angle}; 00145 return p; 00146 } 00147 00148 bool Joystick::button_pressed() 00149 { 00150 // ISR must have been triggered 00151 if (_click_flag) { 00152 _click_flag = 0; // clear flag 00153 return true; 00154 } else { 00155 return false; 00156 } 00157 } 00158 00159 void Joystick::click_isr() 00160 { 00161 _click_flag = 1; 00162 }
Generated on Wed Jul 13 2022 07:38:17 by
 1.7.2 
    