Ifkit and waitForAttachment using C API under Linux

C, C++, and Visual C++
jmtw000
Fresh meat
Posts: 2
Joined: Fri Oct 28, 2011 1:38 pm
Contact:

Ifkit and waitForAttachment using C API under Linux

Postby jmtw000 » Wed Feb 22, 2012 3:52 pm

Hello,

I have a question about the behavior of the SensorChange_Handler while using CPhidget_waitForAttachment() on an IfKit. If I do not use CPhidget_waitForAttachment() the SensorChange_Handler callback seems to be triggered right after registration with the current value of the attached sensor. However, if I use CPhidget_waitForAttachment() on the IfKit this does not happen. It will be triggered on an actual change in the sensor value and not initially. Is it supposed to work this way or am I doing something incorrect? Please see my simple code below for an example of the issue. I am testing it on an 8 port IfKit with only a temp sensor connected to port 0.

Thanks.



#include <phidget21.h>
#include <stdio.h>
#include <unistd.h>

int result;
const char *err;

int SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int
Index, int Value)
{
if(Index==0)
{
Value = ((Value * 0.22222) - 61.11)*1.8+32;
printf("%d\n",Value);
}

return 0;
}


int main(int argc, char* argv[])
{
CPhidgetInterfaceKitHandle ifKit = 0;
CPhidgetInterfaceKit_create(&ifKit);

CPhidget_open((CPhidgetHandle)ifKit, -1);


//Comment out the entire if block below to see the differences in behavior

if((result = CPhidget_waitForAttachment((CPhidgetHandle)ifKit, 2000)))
{
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 0;
}

CPhidgetInterfaceKit_set_OnSensorChange_Handler (ifKit, SensorChangeHandler, NULL);

//wait for input before cleaning up and exiting
getchar();

//delete and close
CPhidget_close((CPhidgetHandle)ifKit);
CPhidget_delete((CPhidgetHandle)ifKit);

return 0;
}

User avatar
Patrick
Lead Developer
Posts: 3100
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Ifkit and waitForAttachment using C API under Linux

Postby Patrick » Thu Feb 23, 2012 10:33 am

It's expected to get the initial state of all sensors via the sensor change handler right after attach.

You're not getting them when you use waitForAttachment because you register for sensor change events after this call.

Normally, you should register your event handlers before calling open - this way you are guaranteed to get these initial state events.

-Patrick

jmtw000
Fresh meat
Posts: 2
Joined: Fri Oct 28, 2011 1:38 pm
Contact:

Re: Ifkit and waitForAttachment using C API under Linux

Postby jmtw000 » Thu Feb 23, 2012 11:13 am

I didn't realize the handler should be registered before open. I get consistent behavior whether I use waitForAttachment or not when I do that.

Thank you :D


Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 1 guest