I am trying to make a library for Arduino, which requires an interruption, but the problem is that when I try the attachInterrupt
function inside my library, there's a problem about attachInterrupt is static. Right now, in the library I have this in the header file:
#ifndef RegletaCMA_h
#define RegletaCMA_h
#include "Arduino.h"
class DimmerGlider {
public:
DimmerGlider(int min, int max, int increase);
int getMin() { return _min; }
int getMax() { return _max; }
int getIncrease() { return _increase; }
void setMin(int min) { _min = min; }
void setMax(int max) { _max = max; }
void setIncrease(int increase) { _increase = increase; }
private:
int _min, _max, _increase;
};
class Dimmer {
public:
Dimmer(DimmerGlider &glider, int outputPin, int t3);
void Dimmer::begin() {
pinMode(_outputPin, OUTPUT);
digitalWrite(_outputPin, false);
}
void Dimmer::dim() {
delayMicroseconds(_DIMMER_counter);
digitalWrite(_outputPin, true); // Turn _DIMMER_pinDimmer on
delayMicroseconds(_t3);
digitalWrite(_outputPin, false); // Turn _DIMMER_pinDimmer off
if (_DIMMER_dir) _DIMMER_counter += _glider.getIncrease(); // If dir is negative add _DIMMER_increase
else _DIMMER_counter -= _glider.getIncrease(); // If dir is negative substract _DIMMER_increase
if (_DIMMER_counter > _glider.getMax()) _DIMMER_dir = false; // Change dir if reached bottom
if (_DIMMER_counter < _glider.getMin()) _DIMMER_dir = true; // Change dir if reached top
_DIMMER_lastExecutionTime_micros = micros(); // Reset timer
}
void Dimmer::force(bool status) {
digitalWrite(_outputPin, status);
}
private:
DimmerGlider &_glider;
int _outputPin;
int _DIMMER_counter = 1000;
bool _DIMMER_dir = true;
unsigned long long _DIMMER_lastExecutionTime_micros = 0;
int _t3;
};
class DimmerImplementor {
public:
DimmerImplementor(Dimmer dimmerList[], int optoPin);
void DimmerImplementor::begin(){
attachInterrupt(_optoPin, dimAll, RISING);
}
void DimmerImplementor::dimAllDimmers() {
for (int c = 0; c < sizeof(_dimmerList); c++)
_dimmerList[c].dim();
}
void DimmerImplementor::dimAll() {
DimmerImplementor::dimAllDimmers();
}
private:
int _optoPin;
Dimmer _dimmerList[];
};
#endif
and this in the cpp file:
#include "Arduino.h"
#include "RegletaCMA.h"
DimmerGlider::DimmerGlider(int min, int max, int increase) :
_min(min),
_max(max),
_increase(increase) {
}
Dimmer::Dimmer(DimmerGlider &glider, int outputPin, int t3) :
_glider(glider),
_outputPin(outputPin),
_t3(t3) {
}
DimmerImplementor::DimmerImplementor(Dimmer *dimmerList, int optoPin) :
_dimmerList(dimmerList),
_optoPin(optoPin) {
for(int c = 0; c < sizeof(dimmerList); c++){
DimmerImplementor::dimmers[c] = dimmerList[c];
}
for (int c = 0; c < sizeof(dimmers); c++) {
Dimmer dimmer = dimmers[c];
dimmer.begin();
}
}
and when I try to run the library with the Arduino IDE, with this code:
DimmerGlider dimmer1Glider = DimmerGlider(_DIMMER_1_tempMin, _DIMMER_1_tempMax, _DIMMER_1_increase);
Dimmer dimmer1 = Dimmer(
dimmer1Glider,
_DIMMER_1_pin,
_DIMMER_t3
);
DimmerGlider dimmer2Glider = DimmerGlider(_DIMMER_2_tempMin, _DIMMER_2_tempMax, _DIMMER_2_increase);
Dimmer dimmer2 = Dimmer(
dimmer2Glider,
_DIMMER_2_pin,
_DIMMER_t3
);
DimmerGlider dimmer3Glider = DimmerGlider(_DIMMER_3_tempMin, _DIMMER_3_tempMax, _DIMMER_3_increase);
Dimmer dimmer3 = Dimmer(
dimmer3Glider,
_DIMMER_3_pin,
_DIMMER_t3
);
Dimmer dimmers[] = {
dimmer1, dimmer2, dimmer3
};
DimmerImplementor implementor = DimmerImplementor(dimmers, _DIMMER_pinOpto);
void Dimmer_Init() {
implementor.begin();
}
It throws the following error:
C:\Users\Arnym\Documents\Arduino\libraries\RegletaCMA/RegletaCMA.h: In member function 'void DimmerImplementor::begin()':
C:\Users\Arnym\Documents\Arduino\libraries\RegletaCMA/RegletaCMA.h:73:44: error: invalid use of non-static member function
attachInterrupt(_optoPin, dimAll, 3); // 3 is the same as RISING
^
I have been messing around with this problem for some days and I can't find an answer, so I would be really pleased if you can help me. If you need any more info, don't doubt on asking. Thanks in advance.