using Joystick library within another library outside of main.cpp

Hello all,

I’m fairly new to libraries but I think it would help clean up my code and make programming a bunch of different arduinos a lot easier.

The current problem I’m having is trying to get the Joystick library to function within a library that will hopefully hold all of my joystick functions.

Here’s the error I get:

     Joystick[i_joy].setButton(i_button, 1);
             ^
src\myLib\myJoy.cpp:33:13: error: no match for 'operator[]' (operand types are 'Joystick_' and 'int')
Compiling .pio\build\due\FrameworkArduino\Reset.cpp.o

Here’s a very shortened version of my code with trying to use libraries

Main.cpp

#include <Arduino.h>

#include <Joystick.h>
#define JOYSTICK_COUNT 1

#include "myLib\myJoy.h"
myJoy Joy;

Joystick_ Joystick[JOYSTICK_COUNT] = {
  Joystick_(0x04, JOYSTICK_TYPE_JOYSTICK,  //joystick address, joystick type (JOYSTICK_TYPE_JOYSTICK, JOYSTICK_TYPE_GAMEPAD, or JOYSTICK_TYPE_MULTI_AXIS)
            25, 2,                // button count, hat switch count
            true, true, false,  // X, Y, and Z Axis
            true, true, false,  // Rx, Ry, and Rz
            false, false,         // rudder and throttle
            false, false, false) // accelerator, brake, and steering
          };

/*-------------------------------------------------------------------------------------------
*-------------------------------- GLOBAL CONFIGURATION SECTION ---------------------------------------
--------------------------------------------------------------------------------------------*/
   
  unsigned int delay_loop = 50; //[ms] time to delay at the end of Arduino Loop function.

/*-------------------------------------------------------------------------------------------
*-------------------------------- ARDUINO PIN ASSIGNMENT SECTION ---------------------------------------
--------------------------------------------------------------------------------------------*/

  unsigned int Cage_Uncage_Switch = 52;

/*-------------------------------------------------------------------------------------------
*-------------------------------- CONTROLLER BUTTON ASSIGNMENT SECTION---------------------------------------
--------------------------------------------------------------------------------------------*/

  int Cage_Uncage_Button = 0;
  
/*-------------------------------------------------------------------------------------------
*-------------------------------- FORWARD FUNCTION DECLARATION SECTION ----------------------
--------------------------------------------------------------------------------------------*/

  void Right_Throttle_Side_Panel();
  
  void Cage_Uncage_Switch_Interrupt();
 
  volatile bool Cage_Uncage_Switch_Interrupt_Active = false;
  
  void Button_hold_HOTAS(int i_joy, unsigned int i_switch, int i_button); //for HOTAS where multiple buttons on throttle may be toggled, removes while loop
  
/*-------------------------------------------------------------------------------------------
*-------------------------------- ARDUINO PRIMARY FUNCTIONS  ----------------------
--------------------------------------------------------------------------------------------*/
  void setup()
  {

    Joy.initialize();
  
    pinMode(Cage_Uncage_Switch, INPUT_PULLUP);

    attachInterrupt(digitalPinToInterrupt(Cage_Uncage_Switch), Cage_Uncage_Switch_Interrupt, CHANGE);

}

void loop() {

  Right_Throttle_Side_Panel();

  delay(delay_loop);

}


/*-------------------------------------------------------------------------------------------
*-------------------------------- INTERRUPT AND SWITCH MONITORING SECTION ----------------------
--------------------------------------------------------------------------------------------*/

void Cage_Uncage_Switch_Interrupt(){
  Cage_Uncage_Switch_Interrupt_Active = true;
}

void Right_Throttle_Side_Panel(){

  if(Cage_Uncage_Switch_Interrupt_Active == true){
    Joy.Button_hold_HOTAS(0, Cage_Uncage_Switch, Cage_Uncage_Button);
    Cage_Uncage_Switch_Interrupt_Active = false;
  }

}

myJoy.h

/*
myJoy.h - library to handle Joystick controls
 */

#ifndef myJoy_h
#define myJoy_h

#include "Arduino.h"

class Joystick;

class myJoy
{

  public:
    myJoy();
    void initialize();

    //Joystick functions
    void Button_hold_HOTAS(int i_joy, unsigned int i_switch, int i_button);

    //tracker variables
    long pin_debounce_timers[54] = {};
    bool pin_last[54] = {};

    //delays
    unsigned int delay_button_press = 50;
    unsigned int delay_debounce = 100;

  private:
    
};

#endif

myJoy.cpp

/*
myJoy.cpp - library to handle Joystick controls
 */

#include "Arduino.h"
#include "myLib\myJoy.h"

#include <Joystick.h>
Joystick_ Joystick;

myJoy::myJoy(){
}

void myJoy::initialize(){

     for (int i = 0; i < 54; i++)
    {
      pin_debounce_timers[i] = millis();
      pin_last[i] = false;
    }

}

void myJoy::Button_hold_HOTAS(int i_joy, unsigned int i_switch, int i_button){
  if (digitalRead(i_switch) == LOW && pin_last[i_switch] == false && (millis() - pin_debounce_timers[i_switch] > delay_debounce))
  {
    Joystick[i_joy].setButton(i_button, 1);
    pin_last[i_switch] = true;
    pin_debounce_timers[i_switch] = millis();
  }
  else if (digitalRead(i_switch) == HIGH && pin_last[i_switch] == true && (millis() - pin_debounce_timers[i_switch] > delay_debounce))
  {
    Joystick[i_joy].setButton(i_button, 0);
    pin_debounce_timers[i_switch] = millis();
    pin_last[i_switch] = false;
  }
}

In your library you define Joystick this way:

Joystick_ Joystick;

So a single instance.

You cannot access it as an array later on:

    Joystick[i_joy].setButton(i_button, 1);

I guess you try to access the instances you defined in your main application but that isn't possible as the library knows nothing about the variables defined there.

Thanks for the feedback pylon,

I think I’ll try having the libraries handle just the logic and return some value for the main.cpp file to interpret to run the Joystick library. I think something like this should work out fine:

main.cpp

#include <Arduino.h>

#include <Joystick.h>
#define JOYSTICK_COUNT 1

#include "myLib\myJoy.h"
myJoy Joy;

Joystick_ Joystick[JOYSTICK_COUNT] = {
  Joystick_(0x04, JOYSTICK_TYPE_JOYSTICK,  //joystick address, joystick type (JOYSTICK_TYPE_JOYSTICK, JOYSTICK_TYPE_GAMEPAD, or JOYSTICK_TYPE_MULTI_AXIS)
            25, 2,                // button count, hat switch count
            true, true, false,  // X, Y, and Z Axis
            true, true, false,  // Rx, Ry, and Rz
            false, false,         // rudder and throttle
            false, false, false) // accelerator, brake, and steering
          };

/*-------------------------------------------------------------------------------------------
*-------------------------------- GLOBAL CONFIGURATION SECTION ---------------------------------------
--------------------------------------------------------------------------------------------*/
   
  unsigned int delay_loop = 50; //[ms] time to delay at the end of Arduino Loop function.

/*-------------------------------------------------------------------------------------------
*-------------------------------- ARDUINO PIN ASSIGNMENT SECTION ---------------------------------------
--------------------------------------------------------------------------------------------*/

  unsigned int Cage_Uncage_Switch = 52;

/*-------------------------------------------------------------------------------------------
*-------------------------------- CONTROLLER BUTTON ASSIGNMENT SECTION---------------------------------------
--------------------------------------------------------------------------------------------*/

  int Cage_Uncage_Button = 0;
  
/*-------------------------------------------------------------------------------------------
*-------------------------------- FORWARD FUNCTION DECLARATION SECTION ----------------------
--------------------------------------------------------------------------------------------*/

  void Right_Throttle_Side_Panel();
  
  void Cage_Uncage_Switch_Interrupt();
 
  volatile bool Cage_Uncage_Switch_Interrupt_Active = false;
  
  void Button_hold_HOTAS(int i_joy, unsigned int i_switch, int i_button); //for HOTAS where multiple buttons on throttle may be toggled, removes while loop
  
/*-------------------------------------------------------------------------------------------
*-------------------------------- ARDUINO PRIMARY FUNCTIONS  ----------------------
--------------------------------------------------------------------------------------------*/
  void setup()
  {

    Joy.initialize();
  
    pinMode(Cage_Uncage_Switch, INPUT_PULLUP);

    attachInterrupt(digitalPinToInterrupt(Cage_Uncage_Switch), Cage_Uncage_Switch_Interrupt, CHANGE);

}

void loop() {

  Right_Throttle_Side_Panel();

  delay(delay_loop);

}


/*-------------------------------------------------------------------------------------------
*-------------------------------- INTERRUPT AND SWITCH MONITORING SECTION ----------------------
--------------------------------------------------------------------------------------------*/

void Cage_Uncage_Switch_Interrupt(){
  Cage_Uncage_Switch_Interrupt_Active = true;
}

void Right_Throttle_Side_Panel(){

  if(Cage_Uncage_Switch_Interrupt_Active == true){
    if(Joy.Button_hold_HOTAS(0, Cage_Uncage_Switch, Cage_Uncage_Button) == 1){
      Joystick[0].setButton(Cage_Uncage_Button, 1);
    }
    else{
      Joystick[0].setButton(Cage_Uncage_Button, 0);
    }
    Cage_Uncage_Switch_Interrupt_Active = false;
  }

}

myJoy.h

/*
myJoy.h - library to handle Joystick controls
 */

#ifndef myJoy_h
#define myJoy_h

#include "Arduino.h"

class Joystick;

class myJoy
{

  public:
    myJoy();
    void initialize();

    //Joystick functions
    int Button_hold_HOTAS(int i_joy, unsigned int i_switch, int i_button); //returns: 0 = off, 1 = on

    //tracker variables
    long pin_debounce_timers[54] = {};
    bool pin_last[54] = {};

    //delays
    unsigned int delay_button_press = 50;
    unsigned int delay_debounce = 100;

  private:
    
};

#endif

myJoy.cpp

/*
myJoy.cpp - library to handle Joystick controls
 */

#include "Arduino.h"
#include "myLib\myJoy.h"

myJoy::myJoy(){
}

void myJoy::initialize(){

     for (int i = 0; i < 54; i++)
    {
      pin_debounce_timers[i] = millis();
      pin_last[i] = false;
    }

}

int myJoy::Button_hold_HOTAS(int i_joy, unsigned int i_switch, int i_button){
  if (digitalRead(i_switch) == LOW && pin_last[i_switch] == false && (millis() - pin_debounce_timers[i_switch] > delay_debounce))
  {
    
    pin_last[i_switch] = true;
    pin_debounce_timers[i_switch] = millis();
    return 1;
  }
  else if (digitalRead(i_switch) == HIGH && pin_last[i_switch] == true && (millis() - pin_debounce_timers[i_switch] > delay_debounce))
  {
    pin_debounce_timers[i_switch] = millis();
    pin_last[i_switch] = false;
    return 0;
  }
}