1

I am trying to call the keypad function into my own library. Before this I had an error "Invalid use of non-static error member function" and have changed the function into a static function. However since the keypad function is non-static, it still does not run.

This is the error.

sketch\latch.cpp: In static member function 'static void latch::keypadEvent(KeypadEvent)':

latch.cpp:18:11: error: invalid use of member 'latch::keypad' in static member function

   switch (keypad.getState()){

           ^

In file included from sketch\latch.cpp:1:0:

sketch\latch.h:20:12: note: declared here

     Keypad keypad;

            ^

exit status 1
invalid use of member 'latch::keypad' in static member function

My code

#include "latch.h"
latch doorlatch;

void setup(){
  doorlatch.begin(9600);
  }
 void loop(){
  doorlatch.main();
  }

h file

#include <Keypad.h>

#ifndef _latch_
#define _latch_

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class latch {

  public:
    latch();
    void begin(int baudrate);
    void main();
    static void keypadEvent(KeypadEvent input);
    Keypad keypad;

  private:
    const byte Rows = 4;
    const byte Cols = 4;

    char keys[4][4] = {
      {'1', '2', '3', 'A'},
      {'4', '5', '6', 'B'},
      {'7', '8', '9', 'C'},
      {'*', '0', '#', 'D'}
    };

    byte rowPins[4] = {7, 6, 5, 4};
    byte colPins[4] = { 11, 10, 9, 8 };
};

#endif

cpp file

latch::latch():keypad( makeKeymap(keys), rowPins, colPins, Rows, Cols ) {
}

void latch::begin(int baudrate){
  Serial.begin(baudrate);
  keypad.addEventListener(keypadEvent);

}

void latch::main(){
  keypad.getKey();
}
void latch::keypadEvent(KeypadEvent input){
  switch (keypad.getState()){
  case PRESSED:
  Serial.print("Enter: ");
  Serial.println(input);
  delay(10);

  }
}

Can someone help me solve this ? Or should I use a different method ? such as declaring it as friend ?

wan
  • 31
  • 4

1 Answers1

0

The problem is that you have conflicting requirements.

On the one hand, the signature of the callback function that is added via the addEventListener() method can only be something like

void keypadEvent(KeypadEvent key);

due to the library Keypad.h

On the other hand, you want to access the instance Keypad keypad; inside class latch from within the event listener, but Keypad keypad is not an input argument of the callback function nor visible in the global scope.

You have three possible choices:

  • sub-class class Keypad and introduce a new method for adding a callback function which is either a member instance or a friend of class latch; or, alternatively, a function which accepts as additional parameter the reference to the Keypad object you want to have access to. More Info

  • Provide a unique instance of Keypad at the Global Scope using the singleton pattern, i.e. a static, factory method that always returns a reference to the same instance of Keypad. More Info

The latter could be as easy as making

 `Keypad keypad`

available at the global scope, and it is actually a much more preferable approach than the other.

Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40