[code]
Well, here it is, months later and I've made much progress.
What I wanted to do all along was create a callback- virtual functions to the rescue! The problem, as Nick mentioned, was that it's not enough in C++ to get the address of a method- in order to correctly identify a method the "this" pointer is required. Because methods are defined in the Class, but instantiated as part of the payload of an object.
So I have found the below code (at the end) and made a little library (cb.h), and created an Interface (that's Java terminology, don't know if it applies to C++). A full example will be given at my Tigger library at Google Code Archive - Long-term storage for Google Code Project Hosting. when I upload it.
One subclasses the CallBackInterface class, e.g.:
class Tigger : public CallBackInterface {
public:
// constructors an whatnot go here
}
implement a method called void cbmethod(void)
in your class ("Tigger", above), e.g.:
void Tigger::cbmethod() {
uint8_t oldSREG = SREG;
cli();
tigermillis = timer0_millis;
SREG = oldSREG;
if (tigermillis-startTime <= _delay) return;
startTime=tigermillis;
count++;
}
Instantiate the class:
Tigger myTigger=new Tigger();
Then finally, call the cbmethod from, say, an interrupt (pseudo-code). First, you need to setup the situation; call
addPin(myTigger):
The addPin method would look something like this:
void PCintPort::addPin(CallBackInterface* cbIface)
{
InterruptPin p=createPin(arduinoPin, CHANGE);
// ...nefarious details deleted for brevity
// cbIface is the object instantiated from a subclass of CallBackInterface
CallBack<CallBackInterface, void>* my_callback=new CallBack<CallBackInterface, void> (cbIface, &CallBackInterface::cbmethod);
p->pinCallBack=my_callback;
In the Interrupt code, call the CallBack at your leisure. All variables in the object are accessible. This call will actually call the cbmethod passed to it above, and previously defined in the class:
(*(p->pinCallBack))();
Here's the code I found:
/*
**********************************************************************
* cb by GreyGnome aka Mike Schwager *
* version 1.0 Mon Jan 16 09:25:59 CST 2012 *
* *
* based on: *
* Variable Parameter Call-Back Template (version 0.0.1) *
* *
* Author: Arash Partow - 2000 *
* URL: http://www.partow.net/programming/templatecallback/index.html *
* *
* Copyright Notice: *
* Free use of this library is permitted under the guidelines and *
* in accordance with the most current version of the Common Public *
* License. *
* http://www.opensource.org/licenses/cpl.php *
* *
**********************************************************************
*/
#ifndef INCLUDE_CALLBACK_H
#define INCLUDE_CALLBACK_H
class CallBackInterface
{
public:
CallBackInterface() {};
virtual void cbmethod() {
};
};
template < class Class, typename ReturnType>
class CallBack
{
public:
typedef ReturnType (Class::*Method)(void);
CallBack(Class* _class_instance, Method _method)
{
class_instance = _class_instance;
method = _method;
};
ReturnType operator()()
{
return (class_instance->*method)();
};
ReturnType execute()
{
return operator()();
};
private:
Class* class_instance;
Method method;
};
#endif
[/code]