一个班级中断

时间:2016-05-17 22:50:46

标签: c++ arduino interrupt arduino-uno interrupt-handling

我正在尝试编写一个库来使用中断来计算PWM占空比。我理解一个类成员不是attachInterrupt的正确函数格式。

但是,我尝试按Calling an ISR from a classNick Gammon来完成这篇文章,他有一个解决方法,但令人沮丧的是我仍然收到错误:

  

无法声明成员函数' static void PWMin :: risingInt()'有静态链接

有人可以对我的代码或任何其他建议的错误有所了解吗?

这是cpp文件:

#include "PWMin.h"

PWMin::PWMin(int intPin, int* outputTime, bool direction=true){
    instance = this;

    this->_intPin = intPin;
    this->_outputTime = outputTime;
    this->_direction = direction;

    pinMode(this->_intPin, INPUT);
    attachInt();
}

void PWMin::attachInt(){
    attachInterrupt(this->_intPin, this->_direction ? risingInt : fallingInt, this->_direction ? RISING : FALLING);
}

void PWMin::risingISR(){
    this->start = micros();
    this->_direction = false;
    this->attachInt();
}

void PWMin::fallingISR(){
    this->timeElapsed = micros() - this->start;
    *_outputTime = this->timeElapsed;
    this->_direction = true;
    this->attachInt();
}

unsigned long PWMin::lastElapsedTime(){
    return this->timeElapsed;
}

static void PWMin::risingInt(){
    if(PWMin::instance != NULL){
        PWMin::instance->risingISR();
    }
}

static void PWMin::fallingInt(){
    if(PWMin::instance != NULL){
        PWMin::instance->fallingISR();
    }
}

这是头文件:

#ifndef PWMin_h
#define PWMin_h

class PWMin {
    public:
        PWMin(int intPin, int* outputTime, bool direction);
        unsigned long lastElapsedTime();

    private:
        static PWMin *instance;

        int _intPin;
        int* _outputTime;
        bool _direction;
        unsigned long start, timeElapsed;

        void attachInt();
        void risingISR();
        void fallingISR();
        static void risingInt();
        static void fallingInt();
};

#endif /* PWMin_h */

谢谢, 肖恩

1 个答案:

答案 0 :(得分:2)

在头文件中,您已将函数声明为static,因此无需再次在.cpp文件中执行此操作。

对于类似问题here有一个很好的答案,可以进一步澄清原因。