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);
    }
}

Committer:
rwunderl
Date:
Wed Aug 05 20:21:34 2015 +0000
Revision:
0:13d10cc6a822
Initial commit of the Joystick library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rwunderl 0:13d10cc6a822 1 /* mbed Joystick Library
rwunderl 0:13d10cc6a822 2 * Copyright (c) 2015, rwunderl, http://mbed.org
rwunderl 0:13d10cc6a822 3 *
rwunderl 0:13d10cc6a822 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
rwunderl 0:13d10cc6a822 5 * of this software and associated documentation files (the "Software"), to deal
rwunderl 0:13d10cc6a822 6 * in the Software without restriction, including without limitation the rights
rwunderl 0:13d10cc6a822 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
rwunderl 0:13d10cc6a822 8 * copies of the Software, and to permit persons to whom the Software is
rwunderl 0:13d10cc6a822 9 * furnished to do so, subject to the following conditions:
rwunderl 0:13d10cc6a822 10 *
rwunderl 0:13d10cc6a822 11 * The above copyright notice and this permission notice shall be included in
rwunderl 0:13d10cc6a822 12 * all copies or substantial portions of the Software.
rwunderl 0:13d10cc6a822 13 *
rwunderl 0:13d10cc6a822 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
rwunderl 0:13d10cc6a822 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
rwunderl 0:13d10cc6a822 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
rwunderl 0:13d10cc6a822 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
rwunderl 0:13d10cc6a822 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rwunderl 0:13d10cc6a822 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
rwunderl 0:13d10cc6a822 20 * THE SOFTWARE.
rwunderl 0:13d10cc6a822 21 */
rwunderl 0:13d10cc6a822 22
rwunderl 0:13d10cc6a822 23 #include "Joystick.h"
rwunderl 0:13d10cc6a822 24
rwunderl 0:13d10cc6a822 25 // apply the deadzone to the axis extremes (comment if you don't want to use this feature)
rwunderl 0:13d10cc6a822 26 #define JOYSTICK_DEADZONE_EXTREMES
rwunderl 0:13d10cc6a822 27
rwunderl 0:13d10cc6a822 28 // adjust the deadzone value for your application, if required
rwunderl 0:13d10cc6a822 29 const double JOYSTICK_DEADZONE = 0.01;
rwunderl 0:13d10cc6a822 30 // set up these constants so they don't have to be calculated every time
rwunderl 0:13d10cc6a822 31 #ifdef JOYSTICK_DEADZONE_EXTREMES
rwunderl 0:13d10cc6a822 32 const double JOYSTICK_MIN = 0.0 + JOYSTICK_DEADZONE; // effective minimum joystick value
rwunderl 0:13d10cc6a822 33 const double JOYSTICK_MINMID = 0.5 - JOYSTICK_DEADZONE; // minimum-side midpoint value
rwunderl 0:13d10cc6a822 34 const double JOYSTICK_MAXMID = 0.5 + JOYSTICK_DEADZONE; // maximum-side midpoint value
rwunderl 0:13d10cc6a822 35 const double JOYSTICK_MAX = 1.0 - JOYSTICK_DEADZONE; // effective maximum joystick value
rwunderl 0:13d10cc6a822 36 #else
rwunderl 0:13d10cc6a822 37 const double JOYSTICK_MIN = 0.0; // minimum joystick value
rwunderl 0:13d10cc6a822 38 const double JOYSTICK_MINMID = 0.5 - JOYSTICK_DEADZONE; // minimum-side midpoint value
rwunderl 0:13d10cc6a822 39 const double JOYSTICK_MAXMID = 0.5 + JOYSTICK_DEADZONE; // maximum-side midpoint value
rwunderl 0:13d10cc6a822 40 const double JOYSTICK_MAX = 1.0; // maximum joystick value
rwunderl 0:13d10cc6a822 41 #endif
rwunderl 0:13d10cc6a822 42
rwunderl 0:13d10cc6a822 43 double _map(double value, double in_min, double in_max, double out_min, double out_max) {
rwunderl 0:13d10cc6a822 44 return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
rwunderl 0:13d10cc6a822 45 }
rwunderl 0:13d10cc6a822 46
rwunderl 0:13d10cc6a822 47 double _scale(double pos) {
rwunderl 0:13d10cc6a822 48 double scaled = 0;
rwunderl 0:13d10cc6a822 49
rwunderl 0:13d10cc6a822 50 if (pos < JOYSTICK_MINMID) {
rwunderl 0:13d10cc6a822 51 scaled = _map(pos, JOYSTICK_MIN, JOYSTICK_MINMID, -1, 0);
rwunderl 0:13d10cc6a822 52 } else if (pos > JOYSTICK_MAXMID) {
rwunderl 0:13d10cc6a822 53 scaled = _map(pos, JOYSTICK_MAXMID, JOYSTICK_MAX, 0, 1);
rwunderl 0:13d10cc6a822 54 }
rwunderl 0:13d10cc6a822 55
rwunderl 0:13d10cc6a822 56 // check out of bounds [-1.0, 1.0] (happens at the extremes)
rwunderl 0:13d10cc6a822 57 if (scaled < -1) {
rwunderl 0:13d10cc6a822 58 scaled = -1;
rwunderl 0:13d10cc6a822 59 } else if (scaled > 1) {
rwunderl 0:13d10cc6a822 60 scaled = 1;
rwunderl 0:13d10cc6a822 61 }
rwunderl 0:13d10cc6a822 62
rwunderl 0:13d10cc6a822 63 return scaled;
rwunderl 0:13d10cc6a822 64 }
rwunderl 0:13d10cc6a822 65
rwunderl 0:13d10cc6a822 66 Joystick::Joystick(PinName x, PinName y, PinName a) : _x(x), _y(y), _a(a) {
rwunderl 0:13d10cc6a822 67 }
rwunderl 0:13d10cc6a822 68
rwunderl 0:13d10cc6a822 69 joypos Joystick::read() {
rwunderl 0:13d10cc6a822 70 joypos pos;
rwunderl 0:13d10cc6a822 71
rwunderl 0:13d10cc6a822 72 pos.x = getX();
rwunderl 0:13d10cc6a822 73 pos.y = getY();
rwunderl 0:13d10cc6a822 74
rwunderl 0:13d10cc6a822 75 return pos;
rwunderl 0:13d10cc6a822 76 }
rwunderl 0:13d10cc6a822 77
rwunderl 0:13d10cc6a822 78 double Joystick::getX() {
rwunderl 0:13d10cc6a822 79 double val = _x.read();
rwunderl 0:13d10cc6a822 80 val = _scale(val);
rwunderl 0:13d10cc6a822 81 return val;
rwunderl 0:13d10cc6a822 82 }
rwunderl 0:13d10cc6a822 83
rwunderl 0:13d10cc6a822 84 double Joystick::getY() {
rwunderl 0:13d10cc6a822 85 double val = _y.read();
rwunderl 0:13d10cc6a822 86 val = _scale(val);
rwunderl 0:13d10cc6a822 87 return val;
rwunderl 0:13d10cc6a822 88 }
rwunderl 0:13d10cc6a822 89
rwunderl 0:13d10cc6a822 90 double Joystick::getRawX() {
rwunderl 0:13d10cc6a822 91 return _x.read();
rwunderl 0:13d10cc6a822 92 }
rwunderl 0:13d10cc6a822 93
rwunderl 0:13d10cc6a822 94 double Joystick::getRawY() {
rwunderl 0:13d10cc6a822 95 return _y.read();
rwunderl 0:13d10cc6a822 96 }
rwunderl 0:13d10cc6a822 97
rwunderl 0:13d10cc6a822 98 void Joystick::rise(void (*fptr)(void)) {
rwunderl 0:13d10cc6a822 99 _a.rise(fptr);
rwunderl 0:13d10cc6a822 100 }
rwunderl 0:13d10cc6a822 101
rwunderl 0:13d10cc6a822 102 void Joystick::fall(void (*fptr)(void)) {
rwunderl 0:13d10cc6a822 103 _a.fall(fptr);
rwunderl 0:13d10cc6a822 104 }