Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: joystickdeneme NOKIA_KOMBAT projekat
Homepage
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);
}
}