Calling attachInterrupt() inside a C++ class

Status
Not open for further replies.

skpang

Well-known member
I'm trying to write a C++ class which a attachInterrupt() is called but it won't compile and giving me an error message of :

/var/folders/6s/w7v5ds1s2hb0f3g_z9fb0ftc0000gn/T/arduino_build_39910/sketch/lin.cpp: In member function 'void lin::slave_init()':
lin.cpp:16: error: invalid use of non-static member function
attachInterrupt(digitalPinToInterrupt(rx_pin),rxISR,CHANGE);
^
invalid use of non-static member function

How can I fix this ?

isr_test.ino
Code:
#include "lin.h"

lin linslave(1);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

lin.cpp
Code:
#include "lin.h"

lin::lin(uint8_t Rx_pin)
{
 rx_pin = Rx_pin;
}

void lin::rxISR(void)
{
  uint8_t i;
  
}
void lin::slave_init(void)
{
  
  attachInterrupt(digitalPinToInterrupt(rx_pin),rxISR,CHANGE);  // <- This line won't compile
  
}

lin.h
Code:
#include <Arduino.h>

class lin
{
  
  public:
  lin(uint8_t Rx_pin);
  void rxISR(void);
  void slave_init(void);
 
  private:
  uint8_t rx_pin;
  
};

This compiles ok if I don't use class and put everything in the .ino file but making a class would be more useful to others.
 
This line in the class definition is the problem.

Code:
  void rxISR(void);

Functions called from attachInterrupt must be static.
 
Status
Not open for further replies.
Back
Top