The phidgets_api package contains a C++ API built on top of the phidgets_c_api package.

The package provides a base Phidget class, from which all other Phidgets inherit. Currently, we have implemented classes for the following devices:

* IMU sensor (PhidgetsSpatial 3/3/3)

* IR sensor (PhidgetIR)

Creating a class is fairly easy - feel free to request devices to be added to the stack. If you write a class for any device using the following example as reference and submit a patch, we'll add it to the package.


In this example, we go through creating an IMU phidget using the C++ API.

We first define the Imu class, which is inherited from the base Phidget class:

   1 class Imu: public Phidget
   2 {
   3   public:
   5     Imu();
   7   protected:
   9     CPhidgetSpatialHandle imu_handle_;
  11     void zero();
  12     void setDataRate(int rate);
  14     virtual void dataHandler(CPhidgetSpatial_SpatialEventDataHandle *data, int count);
  16   private:
  18     static int SpatialDataHandler(CPhidgetSpatialHandle spatial, void *userptr, CPhidgetSpatial_SpatialEventDataHandle *data, int count);
  19 };

The Imu class has a CPhidgetSpatialHandle, which is a special handle for the Phidget Spatial device. Different devices will have different handle types.

The constructor needs to perform 4 operations:

  • create the device-specific handle,
  • pass the handle to the base class for intialization
  • ask the base class to register common Phidget callbacks
  • register IMU-specific callbacks

   1 Imu::Imu(): Phidget(), imu_handle_(0)
   2 {
   3   // create the handle
   4   CPhidgetSpatial_create(&imu_handle_);
   6   // pass handle to base class
   7   Phidget::init((CPhidgetHandle)imu_handle_);
   9   // register base class callbacks
  10   Phidget::registerHandlers();
  12   // register imu data callback
  13   CPhidgetSpatial_set_OnSpatialData_Handler(
  14     imu_handle_, SpatialDataHandler, this);
  15 }

The zero() and setDataRate functions implement sepcific calls to the IMU device using the C API. The data handler function receives spatial data events.

   1 void Imu::setDataRate(int rate)
   2 {
   3   CPhidgetSpatial_setDataRate(imu_handle_, rate);
   4 }
   6 void Imu::zero()
   7 {
   8   // zero (calibrate) gyro
   9   CPhidgetSpatial_zeroGyro(imu_handle_);
  10 }

The SpatialDataHandler is a static callback function which forwards the data to the correct Imu class instance.

   1 int Imu::SpatialDataHandler(CPhidgetSpatialHandle handle, void *userptr, CPhidgetSpatial_SpatialEventDataHandle *data, int count)
   2 {
   3   // get the class instance from the userptr, and invoke the data handler
   4   ((Imu*)userptr)->dataHandler(data, count);
   5   return 0;
   6 }

Finally, the virtual dataHandler function is the one which is called back when Spatial data arrives. In this example, the function just prints out a message. However, you can create a class which inherits from the Imu class and overwrites this behavior.

   1 void Imu::dataHandler(CPhidgetSpatial_SpatialEventDataHandle *data, int count)
   2 {
   3   printf("Empty data handler\n");
   4 }

Bug Reports & Feature Requests

We appreciate the time and effort spent submitting bug reports and feature requests.

Please submit your tickets through github (requires github account) or by emailing the maintainers.

Wiki: phidgets_api (last edited 2012-09-08 07:39:09 by IvanDryanovski)