Setting Callbacks to Libraries

I have quite a few libraries for motion control that require callbacks for things like PID updates, encoder interrupts, etc. It appears there is no really "clean" way to hook these to the interrupts, other than having the sketch do it, as it appears not possible to get a pointer to a non-static member function. I currently get around this by providing a SetCallback(int interval, void(*handler)()) method in the library that the sketch must invoke to create a timer callback, passing a pointer to the sketch-level interrupt handler that does nothing more than call the update handler in the library. This works fine, but seems a bit "klunky". Is there a way to have the library setup its own interrupt handler?

Regards, Ray L.

as it appears not possible to get a pointer to a non-static member function.

Correct. When the event happens, which instance of the class should notice? That's the dilemma, and why is is the individual instance's responsibility to make that clear to the class, so the static method knows which instance method to call.

Is there a way to have the library setup its own interrupt handler?

No. But think of a doorman class. A hotel might have several doormen - for weekdays, for weeknights, for weekends, etc.

Only one doorman is on duty at a time, so if a car drives up (an interrupt), the hotel manager knows which instance of the doorman class should handle the interrupt. At the start of the next doorman's shift, he (or she) registers as the doorman of interest, relieving the previously active doorman of that responsibility.

Your class needs to be based on this model, where the class is responsible for dealing with the interrupt and calling the appropriate instance's method.

as it appears not possible to get a pointer to a non-static member function.

It [u]is[/u] possible, however a member function pointer is not the same as a non-member function pointer.

Is there a way to have the library setup its own interrupt handler?

You've already mentioned the common technique, you can just move it all to the class by having the interrupt handler as a static or friend function, and a static pointer/reference to the object in question.

Otherwise if you want to call a non static member function directly, you need to modify the code which calls the callback function.

OK, but HOW does a static member function know which instance of the object to dispatch the interrupt to? Easy if there can be only one instance, but that's not the case here. The only method I could see would be to have several static interrupt handlers, each of which dispatches to another instance. But that means having a list of instances, being able to add/delete instances on that list, dealing with too many entries on the list, etc. Is there a nicer way?

Regards, Ray L.

OK, but HOW does a static member function know which instance of the object to dispatch the interrupt to?

That's your problem. Suppose the interrupt is a timer-based interrupt. Which instance(s) care? The interrupt can't tell that.

But that means having a list of instances, being able to add/delete instances on that list, dealing with too many entries on the list, etc.

Exactly.

Is there a nicer way?

I think you already know.

Now, if you wanted to talk specifics, maybe we would have other ideas.