It is currently Sun May 19, 2013 12:27 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Use of Phidgets
PostPosted: Wed Mar 21, 2012 8:32 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Feb 16, 2009 10:42 am
Posts: 531
Location: sweden
My talented colleague made some code a few weeks back to use Phidgets with Purebasic. I share it here so anyone can use it. (windows)

Phidgets are a set of "plug and play" building blocks for low cost USB sensing and control from your PC. Temperature (like in this example), pressure, motors, robotics, movement/accelerometers etc...
http://www.phidgets.com/

Code:
Procedure.i AttachHandler(ptr1, ptr2)
  *name = AllocateMemory(256)
  CallFunction(0, "CPhidget_getDeviceName", ptr1, @name)
 
  serial.i
  CallFunction(0, "CPhidget_getSerialNumber", ptr1, @serial)
 
  ConsoleColor(10, 0)
  PrintN("[INFO] Device is attached : " + PeekS(name) + " (" + Str(serial.i) + ")")
EndProcedure

Procedure.i TemperatureChangeHandler(ptr1, ptr2, index.i, value.d)
  ambient.d
  CallFunction(0, "CPhidgetTemperatureSensor_getAmbientTemperature", ptr1, @ambient)
 
  ConsoleColor(11, 0)
  PrintN("[DATA] Temperature sensor : " + Str(index) + " > Temperature : " + StrD(value) + " (Ambient : " + StrD(ambient) + ")")
EndProcedure 
 
OpenConsole()
ConsoleTitle ("Phidget - Temperature Sensor")
EnableGraphicalConsole(1)

If OpenLibrary(0,"phidget21.dll")
  *tmp = AllocateMemory(1024)
  CallFunction(0, "CPhidgetTemperatureSensor_create", @tmp)
 
  CallFunction(0, "CPhidget_set_OnAttach_Handler", tmp, @AttachHandler(), 0)
 
  CallFunction(0, "CPhidgetTemperatureSensor_set_OnTemperatureChange_Handler", tmp, @TemperatureChangeHandler(), 0)
 
  CallFunction(0, "CPhidget_open", tmp, -1)
     
  Result.i = CallFunction(0, "CPhidget_waitForAttachment", tmp, 2000)
  If Result <> 0
    ConsoleColor(12, 0)
    PrintN("[ERROR] Problem waiting for attachment : " + Str(Result))
  EndIf
 
  ConsoleColor(7, 0)
  PrintN("Press ENTER to exit...")
  Input()

  CallFunction(0, "CPhidget_close", tmp)
   CallFunction(0, "CPhidget_delete", tmp)

  CloseLibrary(0) 
EndIf

CloseConsole()


Top
 Profile  
 
 Post subject: Re: Use of Phidgets
PostPosted: Wed Mar 21, 2012 2:10 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 2238
Location: Italy
Is he him ?

viewtopic.php?f=13&t=49338

Hmm... yes he is.

I see has followed (EDIT: almost) the suggestion in my last post on that thread (after some weeks I finally have an indirect reply about its validity, nice), but he added a memory allocation that I believe is unneeded. The reasons are explained in that same post.

And now that I can see the code available on that site:

Code:
int CCONV AttachHandler(CPhidgetHandle Analog, void *userptr)
{
   int serialNo;
   const char *name;

   CPhidget_getDeviceName (Analog, &name);
   CPhidget_getSerialNumber(Analog, &serialNo);
   printf("%s %10d attached!\n", name, serialNo);

   return 0;
}


you can see there is no external memory allocation here and that the pointer value is altered by the function as I supposed at the time.

I didn't check any of the remaining code, even if I still see the same problem I mentioned on that thread, the *tmp var become tmp (they are two distinct vars and I don't think it is supposed to happen, so I believe there is something wrong here too).

That's why I suggested to use EnableExplicit to help him spot the problems.

Looks like this code has been written without trying to understand what it is really happening at each and every step.

EDIT: Oh wait, I didn't notice. Has done the same with the *name pointer. He allocated the memory to *name and then used a new var name as param of the function, instead of using *name and then @*name.
By sheer luck it works (since at this point we can be sure the memory is allocated inside the library) and the original pointer is not altered (being two different variables) but the "memory leak" is still present (memory is allocated without a reason).

_________________
[ Home ] [ My PC ] [ New to PB ? ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Lord and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye