Use of Phidgets
Posted: Wed Mar 21, 2012 8:32 am
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/
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()