Handles reading and scaling the analog inputs from a simple 2-axis joystick with a pushbutton. Can also attach rise and fall interrupts to the button.
Dependents: joystickdeneme NOKIA_KOMBAT projekat
Purpose
Simple library for reading an analog joystick like those found on game console controllers. These joysticks usually also have a pushbutton.
Example
Joystick Library Example
// Joystick Library Example // // Reads the joystick x axis and fades green and red LEDs in response. // The blue LED is turned on while the pushbutton is pressed. #include "mbed.h" #include <Joystick.h> // target is the KL25Z #define PIN_JOYX PTB0 // A0 #define PIN_JOYY PTB1 // A1 #define PIN_JOYA PTB2 PwmOut led_R(LED1); PwmOut led_G(LED2); PwmOut led_B(LED3); Joystick j(PIN_JOYX, PIN_JOYY, PIN_JOYA); // call on joystick button release void jrise(void) { led_B = 1.0f; } // call on joystick button press void jfall(void) { led_B = 0.0f; } int main () { // setup led_R = 1.0f; led_G = 1.0f; led_B = 1.0f; joypos p; // joystick position // attach joystick button interrupts j.rise(&jrise); j.fall(&jfall); float fade = 0.0f; // loop while (1) { p = j.read(); fade = p.x; if (fade > 0.0) { led_R = 1.0f; led_G = 1.0f - (float)fade; } else if (fade < 0.0) { led_R = 1.0f + (float)fade; led_G = 1.0f; } else { led_R = 1.0f; led_G = 1.0f; } wait(0.01); } }
Revision 0:13d10cc6a822, committed 2015-08-05
- Comitter:
- rwunderl
- Date:
- Wed Aug 05 20:21:34 2015 +0000
- Commit message:
- Initial commit of the Joystick library.
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 000000000000 -r 13d10cc6a822 Joystick.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Joystick.cpp Wed Aug 05 20:21:34 2015 +0000 @@ -0,0 +1,104 @@ +/* mbed Joystick Library + * Copyright (c) 2015, rwunderl, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "Joystick.h" + +// apply the deadzone to the axis extremes (comment if you don't want to use this feature) +#define JOYSTICK_DEADZONE_EXTREMES + +// adjust the deadzone value for your application, if required +const double JOYSTICK_DEADZONE = 0.01; +// set up these constants so they don't have to be calculated every time +#ifdef JOYSTICK_DEADZONE_EXTREMES + const double JOYSTICK_MIN = 0.0 + JOYSTICK_DEADZONE; // effective minimum joystick value + const double JOYSTICK_MINMID = 0.5 - JOYSTICK_DEADZONE; // minimum-side midpoint value + const double JOYSTICK_MAXMID = 0.5 + JOYSTICK_DEADZONE; // maximum-side midpoint value + const double JOYSTICK_MAX = 1.0 - JOYSTICK_DEADZONE; // effective maximum joystick value +#else + const double JOYSTICK_MIN = 0.0; // minimum joystick value + const double JOYSTICK_MINMID = 0.5 - JOYSTICK_DEADZONE; // minimum-side midpoint value + const double JOYSTICK_MAXMID = 0.5 + JOYSTICK_DEADZONE; // maximum-side midpoint value + const double JOYSTICK_MAX = 1.0; // maximum joystick value +#endif + +double _map(double value, double in_min, double in_max, double out_min, double out_max) { + return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + +double _scale(double pos) { + double scaled = 0; + + if (pos < JOYSTICK_MINMID) { + scaled = _map(pos, JOYSTICK_MIN, JOYSTICK_MINMID, -1, 0); + } else if (pos > JOYSTICK_MAXMID) { + scaled = _map(pos, JOYSTICK_MAXMID, JOYSTICK_MAX, 0, 1); + } + + // check out of bounds [-1.0, 1.0] (happens at the extremes) + if (scaled < -1) { + scaled = -1; + } else if (scaled > 1) { + scaled = 1; + } + + return scaled; +} + +Joystick::Joystick(PinName x, PinName y, PinName a) : _x(x), _y(y), _a(a) { +} + +joypos Joystick::read() { + joypos pos; + + pos.x = getX(); + pos.y = getY(); + + return pos; +} + +double Joystick::getX() { + double val = _x.read(); + val = _scale(val); + return val; +} + +double Joystick::getY() { + double val = _y.read(); + val = _scale(val); + return val; +} + +double Joystick::getRawX() { + return _x.read(); +} + +double Joystick::getRawY() { + return _y.read(); +} + +void Joystick::rise(void (*fptr)(void)) { + _a.rise(fptr); +} + +void Joystick::fall(void (*fptr)(void)) { + _a.fall(fptr); +}
diff -r 000000000000 -r 13d10cc6a822 Joystick.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Joystick.h Wed Aug 05 20:21:34 2015 +0000 @@ -0,0 +1,112 @@ +/* mbed Joystick Library + * Copyright (c) 2015, rwunderl, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MBED_JOYSTICK_H +#define MBED_JOYSTICK_H + +#include "mbed.h" + +/** Joystick position structure + * + * Struct to keep track of the joystick x and y positions. + */ +struct joypos { + double x; + double y; +}; + +/** Joystick class + * + * Reads a 2-axis analog joystick and keeps track of its positions. + * + * The joystick uses 2 AnalogIns and 1 DigitalIn + * - X AnalogIn + * - Y AnalogIn + * - A DigitalIn (momentary pushbutton; Low is pressed) + */ +class Joystick { +public: + /** Joystick interface + * Create the joystick object and assign pins. + * + * @param x X-axis AnalogIn. + * @param y Y-axis AnalogIn. + * @param a Pushbutton DigitalIn. + */ + Joystick(PinName x, PinName y, PinName a); + + /** Read the joystick position + * The position is returned as a joypos value with x and y in the range [-1.0, 1.0]. + * + * @returns Position structure of the joystick. + */ + joypos read(); + + /** Get the X position + * Read the joystick's horizontal position, represented as a double value in the range [-1.0, 1.0]. + * + * @returns Horizontal position of the joystick. + */ + double getX(); + + /** Get the Y position + * Read the joystick's vertical position, represented as a double value in the range [-1.0, 1.0]. + * + * @returns Vertical position of the joystick. + */ + double getY(); + + /** Get the raw X position + * Read the joystick's raw horizontal position, represented as a double value in the range [0.0, 1.0]. + * + * @returns Horizontal position of the joystick. + */ + double getRawX(); + + /** Get the raw Y position + * Read the joystick's raw vertical position, represented as a double value in the range [0.0, 1.0]. + * + * @returns Vertical position of the joystick. + */ + double getRawY(); + + /** Attach the rise interrupt + * Attach a function pointer to call when a rising edge occurs on the button input. + * + * @param fptr Pointer to a void function. Set to NULL or 0 for none. + */ + void rise(void (*fptr)(void)); + + /** Attach the fall interrupt + * Attach a function pointer to call when a falling edge occurs on the button input. + * + * @param fptr Pointer to a void function. Set to NULL or 0 for none. + */ + void fall(void (*fptr)(void)); + +protected: + AnalogIn _x; + AnalogIn _y; + InterruptIn _a; +}; + +#endif /* MBED_JOYSTICK_H */