Use of Phidgets

Share your advanced PureBasic knowledge/code with the community.
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Use of Phidgets

Post by jesperbrannmark »

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: Select all

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()
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Use of Phidgets

Post by luis »

Is he him ?

http://www.purebasic.fr/english/viewtop ... 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: Select all

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).
"Have you tried turning it off and on again ?"
Post Reply