Joystick USB

De Wiki LOGre
Aller à : navigation, rechercher


Projet réalisé par fma38.

Abouti

Présentation

L'idée est juste de montrer comment remettre au goût du jour un vieux joystick analogique, en lui ajoutant une connexion USB.

Le coeur du projet repose sur le fameux micro-contrôleur Teensy (la version 2.0 suffit), qui dispose de librairies très sioux, permettant d'émuler clavier/souris/joystick en quelques lignes de code seulement. Le travail principal consiste en fait à recâbler le joystick.

Réalisation

Le joystick utilisé ici est un Saitek Cyborg 3D Digital, qui dispose de 4 axes analogiques, et 10 boutons. Il sera très certainement utilisé pour piloter mon hexapode.

Ajouter photos.

Code

/*
 * This sketch must be run on a Teensy.
 *
 * You must select Joystick from the "Tools > USB Type" menu before compiling.
 */
 
#include <Bounce.h>
 
#define NB_BUTTONS 10  // max 16
#define NB_AXES  4  // max 6
#define HAT
#define DEBOUNCE_DELAY 10  // 10ms is appropriate for most mechanical pushbuttons
 
// Define Bounce objects
// The Bounce object automatically deals with contact chatter or "bounce",
// and it makes detecting changes very simple.
Bounce *m_buttons;
 
 
void setup() {
 
    // Create bounce objects
    m_buttons = (Bounce *)malloc(NB_BUTTONS * sizeof(Bounce));
    for (int iButton=0; iButton<NB_BUTTONS; iButton++) {
        m_buttons[iButton] = Bounce(iButton, DEBOUNCE_DELAY);
    }
 
    // Configure the pins for input mode with pullup resistors.
    // The pushbuttons connect from each pin to ground.
    // When the button is pressed, the pin reads LOW because the button shorts it to ground.
    // When released, the pin reads HIGH because the pullup resistor connects to +5 volts inside the chip.
    // LOW for "on", and HIGH for "off" may seem backwards, but using the on-chip pullup resistors is very convenient.
    // The scheme is called "active low", and it's very commonly used in electronics,
    // so much that the chip has built-in pullup resistors!
    for (int iButton=0; iButton<NB_BUTTONS; iButton++) {
        pinMode(iButton, INPUT_PULLUP);
    }
 
    // configure the joystick to manual send mode.  This gives precise
    // control over when the computer receives updates, but it does
    // require you to manually call Joystick.send_now().
    //Joystick.useManualSend(true);
}
 
 
void loop() {
 
    // There should not be any long delays in loop(),
    // so this runs repetitively at a rate faster than the buttons could be pressed and released.
 
    for (int iButton=0; iButton<NB_BUTTONS; iButton++) {
 
        // Update buttons
        m_buttons[iButton].update();
 
        // Check each button for "falling" edge.
        // Update the Joystick buttons only upon changes.
        // falling = high (not pressed - voltage from pullup resistor)
        //           to low (pressed - button connects pin to ground)
        if (m_buttons[iButton].fallingEdge()) {
            Joystick.button(iButton+1, 1);
        }
 
        // Check each button for "rising" edge
        // Update the Joystick buttons only upon changes.
        // rising = low (pressed - button connects pin to ground)
        //          to high (not pressed - voltage from pullup resistor)
        else if (m_buttons[iButton].risingEdge()) {
            Joystick.button(iButton+1, 0);
        }
 
    }
 
    if (NB_AXES >= 1) {
        Joystick.X(analogRead(0));
    }
    if (NB_AXES >= 2) {
        Joystick.Y(analogRead(1));
    }
    if (NB_AXES >= 3) {
        Joystick.Z(analogRead(2));
    }
    if (NB_AXES >= 4) {
        Joystick.Zrotate(analogRead(3));
    }
    if (NB_AXES >= 5) {
        Joystick.sliderLeft(analogRead(4));
    }
    if (NB_AXES >= 6) {
        Joystick.sliderRight(analogRead(5));
    }
 
#ifdef HAT
    Joystick.hat(45.);  // TODO: compute angle from hat buttons
#endif
 
    // Because setup configured the Joystick manual send,
    // the computer does not see any of the changes yet.
    // This send_now() transmits everything all at once.
    //Joystick.send_now();
}

Liens