MHeironimus / ArduinoJoystickLibrary

An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.
GNU Lesser General Public License v3.0
2.06k stars 403 forks source link

Unable to wrap the Joystick class #259

Open mancio opened 1 year ago

mancio commented 1 year ago

Description of Issue

I would like to make a new joystick class in my project that wrap this Joystick class of your library but seem like if I generate a private Joystick_ object without any parameter and then I define it in the constructor with parameters it set Joystick twice and soft brick my Arduino Leonardo (the only way to reset it is short GND and RST pin while is uploading to let him recognize the usb again from the bootloader)

Technical Details

Sketch File that Reproduces Issue

The only way to make it works is by declaring a single new Joystick_ object and passing it like an argument.

main.cpp

#include <Arduino.h>
#include <setmicro.h>
#include <joy.h>
#include <logger.h>
#include <bt.h>
#include <mux.h>

Joystick_ newJoy(
        JOYSTICK_DEFAULT_REPORT_ID, // joystick ID
        JOYSTICK_TYPE_JOYSTICK, // device type
        32, // buttons number starting from zero
        0, // hotswitch count
        true, // X axis
        true, // Y axis
        true, // Z axis
        false, // X rotation?
        false, // Y rotation?
        false, // Z rotation?
        false, // rudder
        false, // throttle
        false, // accelerator
        false, // brake
        false // steering
);
CD74HC4067 mux1(S0_M1, S0_M1, S2_M1, S3_M1);
CD74HC4067 mux2(S0_M2, S1_M2, S2_M2, S3_M2);
Button bArrayM1[TOT_BUTTONS_MUX];
Button bArrayM2[TOT_BUTTONS_MUX];

long axes_values[AXES_NUMBER];
bool log_active = true;

void setup() {
    setLed();
    setAxesRange(newJoy, AXES_NUMBER);
    setPinMux();
    setButtonSIG(bArrayM1, bArrayM2, SIG_M1, SIG_M2);
    setCursor();
    setPot();
}

void loop() {
    axes_values[0] = setAxis(newJoy, X, H_JOY, NORM, ZERO_AT_CENTER);
    axes_values[1] = setAxis(newJoy, Y, V_JOY, NORM, ZERO_AT_CENTER);
    axes_values[2] = setAxis(newJoy, Z, POT, NORM, ZERO_AT_START);

    int * btStateArray1 = readMux(newJoy, mux1, bArrayM1);
    int * btStateArray2 = readMux(newJoy, mux2, bArrayM2);

    if(log_active){
        logAxes(axes_values, AXES_NUMBER);
        logActiveButtons(btStateArray1, FIRST_ARRAY);
        logActiveButtons(btStateArray2, SECOND_ARRAY);
    }
}

joy.cpp

#include <joy.h>

void setAxesRange(Joystick_ NewJoy,  int axes) {
    for (int i = 1; i <= axes; ++i) {
        if(i == 1) NewJoy.setXAxisRange(OUT_MIN, OUT_MAX);
        if(i == 2) NewJoy.setYAxisRange(OUT_MIN, OUT_MAX);
        if(i == 3) NewJoy.setZAxisRange(OUT_MIN, OUT_MAX);
    }
}

long mapValue(long value, bool direction, bool type) {
    if(type){
        if(direction) return map(value, 0, IN_MAX, OUT_MIN, OUT_MAX);
        else return map(value, 0, IN_MAX, OUT_MAX, OUT_MIN);
    } else {
        if(direction) return map(value, 0, IN_MAX, 0, OUT_MAX);
        else return map(value, 0, IN_MAX, OUT_MAX, 0);
    }

}

long setAxis(Joystick_ NewJoy, int name, int pin, bool direction, bool type) {
    long val = analogRead(pin);
    long mapped = mapValue(val, direction, type);
    if(name == X) NewJoy.setXAxis(mapped);
    if(name == Y) NewJoy.setYAxis(mapped);
    if(name == Z) NewJoy.setZAxis(mapped);
    return mapped;
}

I would like to be able to write in this way:

Joy.cpp

Joy::Joy(){
    Joystick_ newJoy(
            JOYSTICK_DEFAULT_REPORT_ID, // joystick ID
            JOYSTICK_TYPE_JOYSTICK, // device type
            32, // buttons number starting from zero
            0, // hotswitch count
            true, // X axis
            true, // Y axis
            true, // Z axis
            false, // X rotation?
            false, // Y rotation?
            false, // Z rotation?
            false, // rudder
            false, // throttle
            false, // accelerator
            false, // brake
            false // steering
    );
    joy1 = newJoy;
}

joy.h

class Joy {
    private:
    Joystick_ joy1;
    Joy();
};

Is this issue happening because Joystick_ call pinmode() twice in case I use in constructor?

Thanks