c++ - 中断创建arduino库

标签 c++ arduino

我正在为能量计数器创建一个库。我想通过 arduino 计算能量计数器产生的脉冲并将其转换为能量。为了捕捉任何脉冲,我想使用 arduino 中断。我希望它易于使用:

#include <EnergyCounter.h>

#define counterPin 2; // interrupt code 0 in Uno
EnergyCounter counter;

void setup(){
    counter.begin(counterPin);
}
...

现在,在 begin 方法中,我想将中断附加到 counterPin:

void EnergyCounter::begin(byte pin){
    pinMode(pin, INPUT);
    attachInterrupt(0, countPulse, RISING);
}

void EnergyCounter::countPulse(){
    display += increment_per_pulse;
}

当我尝试时,出现以下错误:

error: cannot convert 'EnergyCounter::countPulse' from type 'void (EnergyCounter::)()' to type 'void (*)()'
 attachInterrupt(0, countPulse, RISING);
                                      ^
Erro ao compilar.

这是否意味着我不能将方法用作中断服务程序 (ISR)? 我怎样才能解决这个问题,同时拥有一个易于使用的库界面?

最佳答案

increment_per_pulse 需要是一个静态函数(见签名)。

因此,将其标记为static:

static void increment_per_pulse();

关于c++ - 中断创建arduino库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33627745/

相关文章:

c++ - 使用折叠表达式将参数包扩展到 lambda - gcc vs clang

c++ - CUDA 将 GpuMat 的 c 数组传递给内核

c++ - 错误 : 'Qt::ConnectionType' is not a class or namespace

c++ - 从串行数据读取的长度始终为1

c++ - 用逗号分隔的Arduino字符串到数组

c++ - 具有灵活阵列成员的打包结构的可移植替代方案

c++ - Boost SerialPort 的意外结果

time - 无法通过USB将计算机时间同步到Arduino

c - Arduino - 变量应该增加第二个变量 - 总是只等于第二个变量

c++ - 如何将较大数字中 0 到 9 的各个数字放入仅包含数字的数组中