Nicht verwenden kann, attachInterrupt in einer Bibliothek

Schreibe ich eine einfache Bibliothek für ein Ultraschall-Distanz-sensor und dachte, ich würde versuchen, die Verwendung von interrupts.

Jedoch kann ich nicht meine Funktionen in der attachCallback Methode richtig.

Möchte ich HCSR04Interrupt::echoHigh() und HCSR04Interrupt::echoLow() aufgerufen, wenn der pin geht hoch und niedrig, beziehungsweise.

Habe ich Gegoogelt dies ohne Erfolg. Der Ardiuno-IDE-sagt der folgende:

./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 (*)()'

Hier ist mein header:

#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

Und meine Umsetzung (nicht komplett, da kann ich nicht vorbei an den attachInterrupt Schritt):

#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)();
}
InformationsquelleAutor Maciej Swic | 2013-03-27
Schreibe einen Kommentar