2012-01-27 19 views
5

我在使用TimerOne library在4位数7段显示器上显示数字的最新Arduino项目代码时出现了一些小错误。我使用中断来使微处理器在每个数字之间不断弹动,因为它们基本上连接在一起。使用attachInterrupt时没有匹配函数错误

如果我将它全部保存在主PDE文件中,我的代码工作得很完美,但我认为最好是在自己的类中隔离显示。

我的编译过程用下面的代码在PDE第二线故障:

Timer1.initialize(500); 
Timer1.attachInterrupt(digitDisplay.flashDigit,500); 

在attachInterrupt第二ARG应该是可选的, 我有没有这个试过!无论如何,我得到以下错误:

DigitDisplayTest.cpp: In function 'void setup()': 
DigitDisplayTest:29: error: no matching function for call to  'TimerOne::attachInterrupt(<unresolved overloaded function type>)' 
C:\Program Files (x86)\arduino-0022\arduino-0022\libraries\Timer1/TimerOne.h:62: note: candidates are: void TimerOne::attachInterrupt(void (*)(), long int) 

在DigitDisplay(其中digitDisplay是一个实例),我定义flashDigit如下:

class DigitDisplay 
{ 
    private: 
    /*...*/ 
    public: 
    /*...*/ 
    void flashDigit(); 
} 

void DigitDisplay::flashDigit() 
{ 
    wipeDisplay(); 
    for (int i = 0; i < _digitCount ; i++) 
    { 
    if (i == _digit) digitalWrite(_digitPins[i], HIGH); 
    else digitalWrite(_digitPins[i], LOW); 
    } 
    displayNumber(_digits[_digit]); 
    _digit++ ; 
    _digit %= _digitCount; 
} 

如果你需要更多的代码,请让我知道,但我很确定flashDigit()方法的混淆没有什么错 - 它在我把它放入自己的类之前肯定有效。

很明显,我可以通过添加

void Interrupt() 
{ 
    digitDisplay.flashDigit(); 
} 

到主PDE和安装该功能避免这个错误,但是,这只是一个变通,这将是很好,如果我可以直接调用它。

我看到错误是做一个函数指针(其中一个不存在,因此错误),但指针不是我的强项,所以我真的可以做一个手排序。

+0

你的第二个例子使用'digitDisplay.flashDigit()'这是比'digitDisplay不同.flashDigit'。你试过了吗? – gary 2012-01-27 01:19:32

+0

是的,我做了,它并没有解决问题,但无论如何感谢;) – SmallJoeMan 2012-01-27 09:42:57

回答

3

你非常接近。问题是成员函数(flashDigit())与函数(void function())不同。与编译时已知的函数不同,成员函数是可能在运行时更改的函数的ptr。 (因此关于未解决的函数类型的错误信息)。 有两个“解决方法”。您指出的第一个包络函数。其次,如果函数不需要利用类的实例的唯一成员值,则可以将成员函数声明为静态。

static void flashDigit();

This is described in more detail in section 33.1-33.3 of the Cline's C++ FAQ

+0

非常感谢,这是非常有益的。 – SmallJoeMan 2012-01-27 09:43:21

1

我曾与Arduino的TWI库,并指定回调函数同样的问题。 所以我创建了一个调用类对象的静态包装函数。

在我.h文件中我有:

#ifndef Classname 
#define Classname 
class Classname { 
    pubic: 
    void receiveEvent(int numBytes); 
    static void receiveEvent_wrapper(int numBytes); 
}; 
#endif 

,并在我的。CPP文件我有:

#include "Classname.h" 
void* pt2Object; 

void Classname::receiveEvent_wrapper (int numBytes){ 
    // explicitly cast to a pointer to Classname 
    Classname* mySelf = (Classname*) pt2Object; 

    // call member 
    mySelf->receiveEvent(numBytes); 
} 

现在我所说的包装函数,而不是

详细和完整的解释在这里:http://www.newty.de/fpt/callback.html#static

相关问题