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

Download repository: zip gz

Files at revision 0:13d10cc6a822

Name Size Actions
[up]
Joystick.cpp 3577 Revisions Annotate
Joystick.h 3689 Revisions Annotate