Changing a variable from within an event handler?

C, C++, and Visual C++
autofasurer
Phidgetsian
Posts: 12
Joined: Sat May 16, 2015 4:45 am
Contact:

Changing a variable from within an event handler?

Postby autofasurer » Sat Jul 11, 2015 4:43 pm

Hi all,

I'm confused about how I can set an outside variable from within an event handler.

I have an interface kit class setup like this:

Code: Select all

class ofxPhidgetsIfkit : public ofxPhidgets {
   
public:
    void init();
    void digiOut(int index, int state);
    void exit();
   ~ofxPhidgetsIfkit() { exit(); };
    bool pushed;
   
private:
    static int CCONV InputChangeHandler(CPhidgetInterfaceKitHandle phid, void *usrptr, int index, int state);
    int display_properties(CPhidgetInterfaceKitHandle phid);
   int result, numSensors, i;
   const char *err;
   
   //Declare an InterfaceKit handle
   CPhidgetInterfaceKitHandle ifKit = 0;
};


And in the implementation of this method like so:

Code: Select all

int CCONV ofxPhidgetsIfkit::InputChangeHandler(CPhidgetInterfaceKitHandle phid, void *usrptr, int index, int state)
{
    printf("Digital Input: %d > State: %d\n", index, state);
    if (index && state) {
        //how do I set the pushed boolean to true?//
    }
    return 0;
}


Now the question, like it says in the code, How do I set the 'pushed' boolean to true from within that handler so I can use it in my main app e.g.: if (myInterfacekit.pushed){ //do stuff//}

Any help much appreciated!!

frodegill
Phidget Mastermind
Posts: 114
Joined: Thu Mar 04, 2010 2:51 am
Contact:

Re: Changing a variable from within an event handler?

Postby frodegill » Sun Jul 12, 2015 1:48 am

autofasurer wrote:How do I set the 'pushed' boolean to true from within that handler

When registering the event, set userptr to this:

Code: Select all

CPhidgetInterfaceKit_set_OnInputChange_Handler(ifKit, ofxPhidgetsIfkit::InputChangeHandler, this);


and in the handler you cast userptr to your class

Code: Select all

reinterpret_cast<ofxPhidgetsIfkit*>(userptr)->pushed=true;


(Code is neither compiled nor tested, but I hope you get the idea)

autofasurer
Phidgetsian
Posts: 12
Joined: Sat May 16, 2015 4:45 am
Contact:

Re: Changing a variable from within an event handler?

Postby autofasurer » Sun Jul 12, 2015 2:12 am

Great! I don't know what voodoo you pulled, but I'll definitely figure it out lateron ;)

It works great and I'm very happy with that for now without understanding!!!
I had an ugly solution on stand-by involving Max and OSC messaging to openframeworks, I'm glad I don't have to resort to that.

frodegill
Phidget Mastermind
Posts: 114
Joined: Thu Mar 04, 2010 2:51 am
Contact:

Re: Changing a variable from within an event handler?

Postby frodegill » Sun Jul 12, 2015 8:54 am

autofasurer wrote:Great! I don't know what voodoo you pulled, but I'll definitely figure it out lateron ;)

Huh? Not much voodoo going on here.
A pointer to the instance of your class is stored in a void-pointer at initialization, and this void-pointer is cast back to your class to be used in the callback handler.


Return to “C/C++”

Who is online

Users browsing this forum: wuhuilin11 and 3 guests