Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to wrap the Joystick class #259

Open
mancio opened this issue Dec 5, 2022 · 0 comments
Open

Unable to wrap the Joystick class #259

mancio opened this issue Dec 5, 2022 · 0 comments

Comments

@mancio
Copy link

mancio commented Dec 5, 2022

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

  • Arduino Board: arduino leonardo

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant