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

Joystick.h

Committer:
rwunderl
Date:
2015-08-05
Revision:
0:13d10cc6a822

File content as of revision 0:13d10cc6a822:

/* 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 */