2

I'm trying to interface a 4x4 keypad with a TFT display. I want to implement a keypad event listener such that the appropriate screen will be loaded each time a key is pressed. The event listener will be keeping track of which screen is currently loaded, and will be listening for specific key inputs.

The keypad event listener is defined (and will be called in the setup() function in my main arduino sketch (I'm using Atmel Studio + Visual muicro arduino plug-in. The code is written in C++):

void Keypad_apiClass::createKeypadEventHandler(void)
{
    keypad44.addEventListener(screenState_keyHandler);
}

"screenState_keyHandler" is the name of the event listener function, which is defined below:

void screenState_keyHandler(KeypadEvent keyPressed)
{
   switch(Display_api.screenDisplayID)
   {
      case 0x0A:      //menu screen
      switch(keyPressed)
      {
         case '1':
         //go to sensor readings screen
         break;

         case '2':
         //go to  system settings screen
         break;

         case '#':
         //go to  systemReboot screen
         break;

         default:
         //do nothing
         break;
      }
      break;

      default:
      break;
   }

}

I keep getting this compile error:

Compiling 'adutoSecuritySystem' for 'Arduino Mega w/ ATmega2560 (Mega 2560)'
keypad_api.cpp:In member function 'void Keypad_apiClass::createKeypadEventHandler()'
keypad_api.cpp:112:50: error: no matching function for call to 'Keypad::addEventListener(<unresolved overloaded function type>)'
keypad_api.cpp:candidate is
Keypad.h:addEventListener(void (*)(char))
Keypad.h:no known conversion for argument 1 from '<unresolved overloaded function type>' to 'void (*)(char)'
Error compiling

It would seem that the addEventListener is not accepting the type of "screenState_keyHandler". I'm following an example sketch from arduino's website: Event Serial Keypad.

How do I fix this? Thanks

Tim A.
  • 33
  • 5
  • 1
    Is `screenState_keyHandler` also a member function of the `Keypad_apiClass` class? Because a pointer to a member function is not the same as a pointer to a non-member function. – Some programmer dude Jul 09 '15 at 09:18
  • Yes its is also a member function. I can see from the keypad library that the "addEventListener" accepts parameters of type "void (*)(char)". – Tim A. Jul 09 '15 at 09:48
  • What i'm really confused about is the implications of "void (*)(char). In the keypad.h header file, the function prototype reads: void addEventListener(void (*listener)(char)); I need help understanding the parameter. – Tim A. Jul 09 '15 at 10:06

1 Answers1

0

I found the answer to this from another post. The problem was overloading. screenState_keyHandler was also a member function in the Keypad_apiClass. The solution is to exclude screenState_keyHandler from the class altogether, and make it a free function, or the second option: to use namespace instead of class. Thanks to @CoryKramer for the help. See here

Community
  • 1
  • 1
Tim A.
  • 33
  • 5