在库中无法使用attachInterrupt(Can't use attachInterru

2019-07-28 17:45发布

我正在写一个简单的库用于超声波距离传感器和想我会尝试使用中断。

但是我不能把我的功能在attachCallback正确方法。

我想HCSR04Interrupt::echoHigh()HCSR04Interrupt::echoLow()时,销分别为高和低调用。

我GOOGLE了这无济于事。 该Ardiuno IDE说以下内容:

./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp: In member function 'void HCSR04Interrupt::getDistance()':
./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp:31: error: argument of type 'void (HCSR04Interrupt::)()' does not match 'void (*)()'
./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp: In member function 'void HCSR04Interrupt::echoHigh()':
./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp:47: error: argument of type 'void (HCSR04Interrupt::)()' does not match 'void (*)()'

这里是我的头:

#ifndef _HCSR04Interrupt_
#define _HCSR04Interrupt_

#include "Arduino.h"

#define HCSR04_CM_FACTOR 58.0
#define HCSR04_IN_FACTOR 148.0
#define HCSR04_CM_MODE 0
#define HCSR04_IN_MODE 1

class HCSR04Interrupt {
  public:
    double distance;

    HCSR04Interrupt(int trigger_pin, int echo_pin, void (*callback)());

    void setUnits(int units);

    void getDistance();
  private:
    int _trigger_pin;
    int _echo_pin;
    int _units;
    unsigned long _micros_start;
    void (*_callback)();

    void initialize();
    void echoHigh();
    void echoLow();
};

#endif

而我的执行(不完整的,因为我不能让过去的attachInterrupt步骤):

#include "Arduino.h"
#include "HCSR04Interrupt.h"

HCSR04Interrupt::HCSR04Interrupt(int trigger_pin, int echo_pin, void (*callback)()) {
  _trigger_pin = trigger_pin;
  _echo_pin = echo_pin;
  _callback = callback;

  initialize();
}

void HCSR04Interrupt::setUnits(int units) {
  _units = units;
}

void HCSR04Interrupt::initialize() {
  pinMode(_trigger_pin, OUTPUT);
  pinMode(_echo_pin, INPUT);

  digitalWrite(_trigger_pin, LOW);
}

void HCSR04Interrupt::getDistance() {
  //Listen for the RISING interrupt
  attachInterrupt(_echo_pin - 2, echoHigh, RISING);

  //The trigger pin should be pulled high,
  digitalWrite(_trigger_pin, HIGH);

  //for 10 us.
  delayMicroseconds(20);

  //Then reset it.
  digitalWrite(_trigger_pin, LOW);
}

void HCSR04Interrupt::echoHigh() {
  _micros_start = micros();

  detachInterrupt(_echo_pin - 2);
  attachInterrupt(_echo_pin - 2, echoLow, FALLING);
}

void HCSR04Interrupt::echoLow() {
  detachInterrupt(_echo_pin - 2);

  unsigned long us = micros() - _micros_start;

  distance = us;

  (*_callback)();
}

Answer 1:

所以编译器 (而不是IDE)会告诉你到底出了什么问题:

argument of type 'void (HCSR04Interrupt::)()' does not match 'void (*)()

所以,虽然attachInterrupt()采用类型的函数指针void (*)()你想传递一个非静态成员函数,你不能。 你可以尝试使成员函数static和铸造:

static void echoHigh();

// ...

attachInterrupt(_echo_pin - 2, reinterpret_cast<void (*)()>(&echoHigh), RISING);


Answer 2:

Arduino的中断处理程序只能是功能。 您正在尝试使对象的中断处理程序的方法 。 因此,编译器会抱怨。

为了更加精确它,对象的方法有类似功能,但它是因为如果他们采取“隐藏”的参数,它指定该对象的实例。 因此,他们实际上有从普通功能的不同类型的签名。 这不允许一个传递一个方法指针时什么功能是寻找的是一个普通的函数指针。

该解决方案是将您的echoHigh()echoLow()出的HCSR04Interrupt类,并让他们朴素的功能。



Answer 3:

当我偶然发现了这个问题,一直没有一个公认的答案,我写的是什么,我发现,这为我工作:

中断必须由一个全球性的包装被调用。 此包装需要调用handleInterupt类的功能。 因此,它必须知道的类。 这可以通过将其存储在一个全局变量来完成。 如果要使用的类的多个实例,多个这样的全局变量必须使用。 但作为中断引脚只是少数,你可以写为每个引脚的全局变量和函数:

MyClass theInstance_pin3 = NULL;
MyClass theInstance_pin7 = NULL;

// Somewhere, fill in an initialized copy of MyClass,
// and set theInstance_pin3 or theInstance_pin7 to it

void ISR_3()
{
   if (theInstance_pin3)
       theInstance_pin3->handleInterrupt();
}
void ISR_7()
{
   if (theInstance_pin7)
       theInstance_pin7->handleInterrupt();
}

作为参考,参见: http://forum.arduino.cc/index.php?topic=41713.0或http://forum.arduino.cc/index.php?topic=160101.0



Answer 4:

I got around this by making a singleton base class which represents the hardware as a whole (which kinda makes sense in this situation anyway).

Any function pointers can then be passed to the sub-component class, and handled by the singleton, whose member variables and methods are all static.

Example headers (untested):

// Sub-component
class LampButton {
public:
    LampButton(int pin, void(*pushHandler)());
}

// Sub-component
class LampLed {
public:
    LampLed(int pin);
    void toggle();
}

// Singleton represents the hardware in it's entirety
class Lamp {
public:
    // Call this instead of a constructor
    static void initialize(int buttonPin, int ledPin);

    // Function implemented inline for clarity - don't do this
    static void handleButtonPush() {
        led.toggle();
    }

private:
    static LampButton button;
    static LampLed led;
}


文章来源: Can't use attachInterrupt in a library