SwitchManager.h Wrapper Question:

Hi,

I'm attempting to use Nick Gammon's SwitchManager.h library for a project. The library can be found here: [http://gammon.com.au/Arduino/SwitchManager.zip]

When I setup my project as per the comments in the SwitchManager.h library everything works great.

#include <SwitchManager.h>             
//object instantiations
SwitchManager myIncSwitch;
SwitchManager myDecSwitch;

unsigned long currentMillis;

unsigned long incLongPress    = 2000UL;// 2 seconds 

const byte incSwitch          = 32;
const byte decSwitch          = 36;

int myCounter;

//======================================================================

void setup()
{
  Serial.begin(9600);

  myIncSwitch.begin (incSwitch, switchOneHandler); 
  myDecSwitch.begin (decSwitch, switchTwoHandler);
  
} 



void loop()
{
  //leave this line of code at the top of loop()
  currentMillis = millis();

  myIncSwitch.check ();  
  myDecSwitch.check (); 

}


void switchOneHandler(byte newState, unsigned long interval, byte whichPin)
{
    if (newState == HIGH)
    {
      if(interval >= incLongPress) 
      {
        Serial.println("Switch 1 pressed.");
      }
    }
}

void switchTwoHandler(const byte newState, const unsigned long interval, const byte whichPin)
{
    if (newState == HIGH)
    {
      if(interval >= incLongPress) 
      {
        Serial.println("Switch 2 pressed.");
      }
    }
}

I'm attempting to put this code inside of a wrapper class to keep things clean, but keep running into an "invalid use of non-static member function" when I try and initialize my buttons in the wrapper's constructor.

ButtonManager::ButtonManager()
{
  btnOne.begin(btnOne, btnOneHandler);
}

lib/ButtonManager/ButtonManager.cpp: In constructor 'ButtonManager::ButtonManager()':
lib/ButtonManager/ButtonManager.cpp:48:73: error: invalid use of non-static member function
btnOne.begin(btnOne, btnOneHandler);

How do I get rid of this error?

Thanks so much in advance for your help!!

when I try and initialize my buttons in the wrapper's constructor.

You'll notice that Nick was quite intelligent in NOT calling the class ButtonManager. It is to manage SWITCHES. So, stop sewing buttons onto the Arduino.

You should NOT be calling a class' begin() method in your constructor. The hardware is NOT ready when your constructor is called.

How do I get rid of this error?

Make the method to be called a static method, like the compiler is telling you.

Suppose that you create multiple instances of your class.

ButtonManager buttonManglerOne;
ButtonManager buttonManglerTwo;

Now, suppose that the switch connected to the buttonManglerOne instance is pressed.

What, EXACTLY, do you expect to happen?

Now, suppose that the switch connected to the buttonManglerTwo instance is pressed.

What, EXACTLY, do you expect to happen?

The functions (NOT method) that are supplied to Nick's class' method, begin(), are not class members. There is ONE and only one instance of the function.

Not so with the class method you are trying to supply as the function to be called.

son_goku:
I'm attempting to put this code inside of a wrapper class to keep things clean, but keep running into an "invalid use of non-static member function" when I try and initialize my buttons in the wrapper's constructor.

ButtonManager::ButtonManager()

{
 btnOne.begin(btnOne, btnOneHandler);
}



lib/ButtonManager/ButtonManager.cpp: In constructor 'ButtonManager::ButtonManager()':
lib/ButtonManager/ButtonManager.cpp:48:73: error: invalid use of non-static member function
btnOne.begin(btnOne, btnOneHandler);

How do I get rid of this error?

Given that you didn't show the full code that's NOT working, it's hard to see the value added in what you're trying to do.