关于函数解释的混淆 [英] Confusing regarding function interpretation

查看:20
本文介绍了关于函数解释的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 4x4 键盘与 TFT 显示器连接起来.我想实现一个键盘事件侦听器,以便每次按下一个键时都会加载相应的屏幕.事件侦听器将跟踪当前加载的屏幕,并将侦听特定的键输入.

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.

键盘事件侦听器已定义(并将在我的主要 arduino 草图中的 setup() 函数中调用(我使用的是 Atmel Studio + Visual muicro arduino 插件.代码是用 C++ 编写的):

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 是这样定义的:

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

当我查看函数addEventListener的原型时(在声明它的头文件中,我看到:

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?

当我编译时,我得到

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

推荐答案

是的,它是一个函数指针

Yes it is a function pointer

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

所以它是一个名为 listener 的函数,它接受一个 char 参数并且没有返回值.

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

您的 screenState_keyHandler 符合此签名.

void screenState_keyHandler(KeypadEvent keyPressed)

它没有返回,我假设 KeypadEvent 是一个 typedef 的 char

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

这篇关于关于函数解释的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆