I think, he can use static member function. It should work with interrupts since there is no context (this) passed
That would concern me that there's an undiscovered error in your code.
That still leaves the code vulnerable to incorrect behavior. Suppose an instance of Motor is created dynamically and then deleted without a call to stop(). Or, there's a local Motor object in a function that returns without calling stop(). In either case, the code and hardware would be left in an incorrect state. Most troubling would be the timer and external interrupts that would now be calling ISRs for a non-existent instance.
If it were my code, I'd set a flag in stop() indicating it had been called. Then, I'd check that flag in the destructor. If it's not set, call stop();
It's best to put new code in a new post every time you change something. Updating previous code makes others' comments and suggestions incorrect and disrupts the discussion's flow.
You can’t use context related variables in a static member function (class function) so not sure this matches the needs.
Well, I had the same problem with my MobaTools library, e.g. the MoToStepper functionality.
The ISR itself can definitely not be a member of an object, because it is called by a hardware event, and this means it knows nothing of an object.
I solved it this way:
All ISR related data of an object is summarized in a struct. This structs are concatenated by a pointer chain. Every time a new object is created, this chain is updated accordingly. The root-pointer of the chain is a static variable, not a member of any object.
If an IRQ occurs, the ISR loops through the chain, checking if any work has to be done for one of the objects.
Maybe this could be done with the 'this' pointer itself - so every object contains the 'this' ptr of the next object? In the end the this-pointer is a pointer to the complete struct containing the object.
There's a fairly elegant way to have instance-specific ISRs using a table of instance pointers and some template trickery:
// ------------------------------ Define the Number of Interrupts Available for Various Boards ------------------
// Teensy (and maybe others) define these automatically
#if !defined(CORE_NUM_INTERRUPT)
// Arduino Uno
#if defined(__AVR_ATmega328P__)
#define CORE_NUM_INTERRUPT 2
// Arduino Mega
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define CORE_NUM_INTERRUPT 6
// ESP32
#elif defined(ESP32)
#define CORE_NUM_INTERRUPT 40
#endif
#endif
// ------------------------------ Add More Boards as Necessary ------------------
#if !defined(CORE_NUM_INTERRUPT)
#error CORE_NUM_INTERRUPT Not Defined!!!
#endif
class Motor {
public:
Motor(uint8_t p): pinEncA(p) {};
~Motor() {
if(intAttached) {
detachInterrupt(interruptNum);
}
}
bool begin();
private:
int interruptNum {-1};
bool intAttached {false};
using isrFunct = void (*)();
static Motor *objectTable[CORE_NUM_INTERRUPT];
uint8_t pinEncA;
void cbkEncA ();
template<uint8_t NUM_INTERRUPTS = CORE_NUM_INTERRUPT>
static isrFunct getIsr(uint8_t intNumber);
};
Motor * Motor::objectTable[CORE_NUM_INTERRUPT];
template<uint8_t NUM_INTERRUPTS>
Motor::isrFunct Motor::getIsr(uint8_t intNumber) {
if (intNumber == (NUM_INTERRUPTS - 1)) {
return [] {
(objectTable[NUM_INTERRUPTS - 1])->cbkEncA();
};
}
return getIsr < NUM_INTERRUPTS - 1 > (intNumber);
}
template<>
inline Motor::isrFunct Motor::getIsr<0>(uint8_t intNum) {
(void) intNum;
return nullptr;
}
bool Motor::begin() {
pinMode(pinEncA, INPUT_PULLUP);
intAttached = false;
interruptNum = digitalPinToInterrupt(pinEncA);
if ((interruptNum < 0) || (interruptNum >= CORE_NUM_INTERRUPT)) {
return false;
}
isrFunct isr {getIsr(interruptNum)};
if (isr == nullptr) {
return false;
}
objectTable[interruptNum] = this;
attachInterrupt(interruptNum, isr, RISING);
intAttached = true;
return true;
}
// Instance-specific ISR Here
void Motor::cbkEncA() {
}
Motor motor(3);
void setup() {
Serial.begin(115200);
delay(1000);
if (!motor.begin()) {
Serial.println("Attach Interrupt Failed");
while (1) {}
}
Serial.println("Attached");
}
void loop() {
}