Phidget Manager: Difference between revisions

From Phidgets Support
Line 14: Line 14:
==Using the Phidget Manager==
==Using the Phidget Manager==


As with any other Phidget software object, the Phidget Manager must be [[General Phidget Programming#Opening the Phidget|opened]], then [[General Phidget Programming#Attaching the Phidget|attached]] within your code. Here's an example of how the manager is used in C:
Much like a Phidget channel, a Phidget Manager must be created and opened.  Refer to the {{Phidget22API}} for details.


As soon as a manager is opened the Phidget library will fire an event for each device channel that is currently know to the system.  After those initial events, '''attach''' and '''detach''' events will be fired when new Phidgets become available or existing Phidget are removed.
In the C programming language, it is not safe to refer to a {{Code|PhidgetHandle}} after an event handler returns, unless {{Code|Phidget_retain()}} is first called ({{Code|Phidget_open()}} and {{Code|Phidget_close()}} retain and release handlers automatically).  The {{Code|PhidgetHandle}} must be released with {{Code|Phidget_release()}} if it has been retained. The object oriented languages supported in the {{Phidget22API}} automatically retain and release handles.
What follows is an example of how to use a manager in C:


<div class="source">
<div class="source">
<syntaxhighlight lang=c>
<syntaxhighlight lang=c>
int main() {


    // Define the manager handle and create
static void CCONV
    PhidgetManagerHandle device = 0;
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {
    PhidgetManager_create(&device);


    // Set up event handlers
    PhidgetLog_log("%P attached", devch);
    PhidgetManager_setOnAttachHandler(device, AttachHandler, NULL);
}
    PhidgetManager_setOnDetachHandler(device, DetachHandler, NULL);


    // Open the Phidget Manager
static void CCONV
    PhidgetManager_open(device);
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {


    // Wait for attach/detach events
    PhidgetLog_log("%P detached", devch);
    printf("Press Enter to end anytime...\n");
    getchar();
 
    // Close and clean up manager object
    PhidgetManager_close(device);
    PhidgetManager_delete(&device);
    return 0;
}
}


void CCONV AttachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {
int
 
main(int argc, char **argv) {
    const char *chanClass;
    PhidgetManagerHandle mgr;
    // Define handles and spatial variables
    PhidgetAccelerometerHandle accHandle;
    PhidgetGyroscopeHandle gyrHandle;
    PhidgetMagnetometerHandle magHandle;
    double acceleration[3];
    double angularRate[3];
    double fieldStrength[3];
 
    // Get the class of this generic Phidget object that has just attached
    Phidget_getChannelClassName(device, &chanClass);
   
    // If the channel is an accelerometer, handle it accordingly:
    if(strcmp(chanClass,"PhidgetAccelerometer") == 0) {
 
          // Cast the generic Phidget object into a Phidget Accelerometer object
          accHandle = (PhidgetAccelerometerHandle) device;
          // Now that the channel is cast, we can call Accelerometer-specific methods like getAcceleration:
          PhidgetAccelerometer_getAcceleration(accHandle, &acceleration);
          // Print out the acceleration values and close the object
          printf("Accelerometer attached! X:%f Y:%f Z:%f \n", acceleration[0], acceleration[1], acceleration[2]);
  Phidget_close(accHandle);
    }
 
    // If the channel is a gyroscope, handle it accordingly:
    if(strcmp(chanClass,"PhidgetGyroscope") == 0) {
 
          // Cast the generic Phidget object into a Phidget Gyroscope object
          gyrHandle = (PhidgetGyroscopeHandle) device;
          // Now that the channel is cast, we can call Gyroscope-specific methods like getAngularRate:
          PhidgetGyroscope_getAngularRate(gyrHandle, &angularRate);
          // Print out the angular rate values and close the object
          printf("Gyroscope attached! X:%f Y:%f Z:%f \n", angularRate[0], angularRate[1], angularRate[2]);
  Phidget_close(gyrHandle);
    }


    // If the channel is a magnetometer, handle it accordingly:
    PhidgetManager_create(&mgr);
    if(strcmp(chanClass,"PhidgetMagnetometer") == 0) {


          // Cast the generic Phidget object into a Phidget Magnetometer object
    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);
          magHandle = (PhidgetMagnetometerHandle) device;
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);
          // Now that the channel is cast, we can call Magnetometer-specific methods like getFieldStrength:
          PhidgetMagnetometer_getFieldStrength(magHandle, &fieldStrength);
          // Print out the field strength values and close the object
          printf("Magnetometer attached! X:%f Y:%f Z:%f \n", fieldStrength[0], fieldStrength[1], fieldStrength[2]);
  Phidget_close(magHandle);
    }
}


void CCONV DetachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {
    PhidgetManager_open(mgr);
   
    const char *chanClass;


    // Get an print the class of this generic Phidget object that has just detached
    /* Do something long running, like run a GUI threads */
    Phidget_getChannelClassName(device, &chanClass);
    printf("%s detached!\n",chanClass);
    /* Now close and delete the manager */
    PhidgetManager_close(mgr);
    PhidgetManager_delete(&mgr);
    exit(0);
}
}
</syntaxhighlight>
</syntaxhighlight>
</div>
</div>


 
A further example would be "HelloWorld" for most [[Software Overview#Language Support|supported languages]] uses the Phidget Manager.
For more sample code that uses the Phidget Manager, have a look at our "HelloWorld" programming examples, which are available for most of [[Software Overview#Language Support|the languages we support]].

Revision as of 17:04, 13 July 2017

General Overview

The Phidget Manager is an interface into the device channels available to the Phidget library. The API is strictly asynchronous, a continuously monitors channels as they attach and detach. Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or become available over the network), an attach event is fired for each channel available on that Phidget.

It is important to understand the concepts of attach and detach as the they relate to the manager. A manager attach does not imply that a user channel has attached to a device channel, but that the device channel have become available. The device channel is now ready to be attached to a user channel. When a user channel closes and detaches from a device channel a manager event will not be fired. A manager detach event is fired when the Phidget device is removed from the system.

Purpose

The primary function of a Phidget Manager is to fire an attach event for each new device channel when a Phidget becomes available to a system, and to fire a detach event for each device channel that is no longer available when the Phidget is removed from the system. For example, when a 1044_0 - PhidgetSpatial is connected, a Phidget Manager would fire three attach events, one for each device channel, as illustrated below:

Using the Phidget Manager

Much like a Phidget channel, a Phidget Manager must be created and opened. Refer to the Phidget22 API for details.

As soon as a manager is opened the Phidget library will fire an event for each device channel that is currently know to the system. After those initial events, attach and detach events will be fired when new Phidgets become available or existing Phidget are removed.

In the C programming language, it is not safe to refer to a PhidgetHandle after an event handler returns, unless Phidget_retain() is first called (Phidget_open() and Phidget_close() retain and release handlers automatically). The PhidgetHandle must be released with Phidget_release() if it has been retained. The object oriented languages supported in the Phidget22 API automatically retain and release handles.

What follows is an example of how to use a manager in C:

static void CCONV
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {

    PhidgetLog_log("%P attached", devch);
}

static void CCONV
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {

    PhidgetLog_log("%P detached", devch);
}

int
main(int argc, char **argv) {
    PhidgetManagerHandle mgr;

    PhidgetManager_create(&mgr);

    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);

    PhidgetManager_open(mgr);

    /* Do something long running, like run a GUI threads */
 
    /* Now close and delete the manager */
    PhidgetManager_close(mgr);
    PhidgetManager_delete(&mgr);
    exit(0);
}

A further example would be "HelloWorld" for most supported languages uses the Phidget Manager.