Analog joystick library
Revision 1:8e384c3393dd, committed 2020-01-27
- Comitter:
- eencae
- Date:
- Mon Jan 27 15:28:02 2020 +0000
- Parent:
- 0:649b59b2fb26
- Commit message:
- Gamepad2
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 |
--- a/Joystick.cpp Tue Feb 07 13:01:48 2017 +0000 +++ b/Joystick.cpp Mon Jan 27 15:28:02 2020 +0000 @@ -1,10 +1,9 @@ #include "Joystick.h" -Joystick::Joystick(PinName vertPin,PinName horizPin,PinName clickPin) +Joystick::Joystick(PinName vertPin,PinName horizPin) { vert = new AnalogIn(vertPin); horiz = new AnalogIn(horizPin); - click = new InterruptIn(clickPin); } void Joystick::init() @@ -16,15 +15,6 @@ // this assumes that the joystick is centred when the init function is called // if perfectly centred, the pots should read 0.5, but this may // not be the case and x0 and y0 will be used to calibrate readings - - // turn on pull-down for button -> this assumes the other side of the button - // is connected to +3V3 so we read 1 when pressed and 0 when not pressed - click->mode(PullDown); - // we therefore need to fire the interrupt on a rising edge - click->rise(callback(this,&Joystick::click_isr)); - // need to use a callback since mbed-os5 - basically tells it to look in this class for the ISR - _click_flag = 0; - } Direction Joystick::get_direction() @@ -85,13 +75,8 @@ float x = 2.0f*( horiz->read() - _x0 ); float y = 2.0f*( vert->read() - _y0 ); - // Note: the x value here is inverted to ensure the positive x is to the - // right. This is simply due to how the potentiometer on the joystick - // I was using was connected up. It could have been corrected in hardware - // by swapping the power supply pins. Instead it is done in software so may - // need to be changed depending on your wiring setup - - Vector2D coord = {-x,y}; + // Note: the values are negated so postive is up (y). + Vector2D coord = {x,-y}; return coord; } @@ -143,20 +128,4 @@ Polar p = {mag,angle}; return p; -} - -bool Joystick::button_pressed() -{ - // ISR must have been triggered - if (_click_flag) { - _click_flag = 0; // clear flag - return true; - } else { - return false; - } -} - -void Joystick::click_isr() -{ - _click_flag = 1; -} +} \ No newline at end of file
--- a/Joystick.h Tue Feb 07 13:01:48 2017 +0000 +++ b/Joystick.h Mon Jan 27 15:28:02 2020 +0000 @@ -41,7 +41,7 @@ #include "Joystick.h" // y x button -Joystick joystick(PTB10,PTB11,PTC16); +Joystick joystick(PTB11,PTB10); int main() { @@ -61,10 +61,7 @@ Direction d = joystick.get_direction(); printf("Direction = %i\n",d); - - if (joystick.button_pressed() ) { - printf("Button Pressed\n"); - } + wait(0.5); } @@ -78,8 +75,8 @@ { public: - // y-pot x-pot button - Joystick(PinName vertPin,PinName horizPin,PinName clickPin); + // y-pot x-pot + Joystick(PinName vertPin,PinName horizPin); void init(); // needs to be called at start with joystick centred float get_mag(); // polar @@ -88,16 +85,11 @@ Vector2D get_mapped_coord(); // x,y mapped to circle Direction get_direction(); // N,NE,E,SE etc. Polar get_polar(); // mag and angle in struct form - bool button_pressed(); // read button flag set in ISR when button pressed private: AnalogIn *vert; AnalogIn *horiz; - InterruptIn *click; - - int _click_flag; // flag set in ISR - void click_isr(); // ISR on button press // centred x,y values float _x0;