1

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);
}

And the screenState_keyHandler is defined thus:

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;
  }
}

When I look at the prototype of the function addEventListener (in the header file where it is declared, I see this:

void addEventListener(void (*listener)(char));

I'm having trouble understanding the parameter of the prototype. Is it expecting a function pointer or what? Could someone please explain the parameter in the prototype above?

When I compile, I get

    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

That's why I'm confused! The only other place the function exists is in the member declaration of the class:

class Keypad_apiClass
{
 protected:


 public:
   void init();
   char retrieveKeyPressed(void);
   char waitAndRetrieveKeyPressed(void);
   uint8_t returnKeyPressed_IntVersion(char keyPressed);
   void createKeypadventHandler(void);
   void screenState_keyHandler(KeypadEvent keyPressed);
};
Tim A.
  • 33
  • 5

1 Answers1

1

Yes it is a function pointer

void           (*listener)       (char)
^return type     ^function name   ^argument type

So it is a function named listener that takes a char argument and has no return value.

Your screenState_keyHandler fits this signature.

void screenState_keyHandler(KeypadEvent keyPressed)

It has no return, and I'm assuming that KeypadEvent is a typedef'd char

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • yes, it is typedef'd 'char'. Everything looks fine, but I still get compile errors. – Tim A. Jul 09 '15 at 13:18
  • Is there another overload of `screenState_keyHandler` other than the one you showed? That compile error is telling you that there are 2 (or more) overloads and it can't resolve which one to use – Cory Kramer Jul 09 '15 at 13:20
  • That's odd! The only other place it exists is in the member declaration in the header file. – Tim A. Jul 09 '15 at 13:23
  • That is an important detail! It is not a free function, it is a member function of a class. [See here](https://stackoverflow.com/questions/15841338/c-unresolved-overloaded-function-type). Actually the *real* signature of this function is then `void (Keypad_apiClass::*)(char)` – Cory Kramer Jul 09 '15 at 13:27
  • Interesting! So, could removing the function from the class "membership" solve the problem? How would you go about it? – Tim A. Jul 09 '15 at 13:32
  • Why do you have that class? Does it have any member variables or anything, or are you just using it to group those functions? If you're just using it for grouping, I would change `class` to `namespace`, which is actually how I'd handle it. – Cory Kramer Jul 09 '15 at 13:33
  • the class is only to group functions. I have several functions and i'm grouping them in classes so as to have more structured code. – Tim A. Jul 09 '15 at 13:36
  • That is exactly the purpose of `namespace`. I would avoid using `class` just for that purpose, it adds a bunch of other complexity and overhead, when `namespace` will provide the grouping you need. – Cory Kramer Jul 09 '15 at 13:37