Phidget Manager: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
Line 2: Line 2:
==General Overview==
==General Overview==


The Phidget Manager is a software object that lets you access all of the Phidgets attached to your system. If you want to 'search for all attached Phidgets', in your code the Manager is the tool you would use.
The Phidget Manager is a software object that can be set up to notify your program whenever a Phidget attached or detaches to your computer.  
 
If you wanted to keep track of several Phidgets on your system that are constantly being plugged in and removed, the Manager is the tool you would use.


==Object Structure==
==Object Structure==


You are probably already used to Phidget software objects, where a single set of functions for a board - say, an Interface Kit:
You are probably already familiar with Phidgets software objects, where a single set of functions for a class of hardware are contained in one object. For example, the VoltageInput object:
 
[[Image:voltageInput_object.jpg|300px|link=|alt=]]
 
The methods for the Phidget Manager are contained in the same way, but it does not belong to any particular Phidget device. It exists separately, so you can have an active Phidget Manager object even when there are no Phidgets attached.
 
[[Image:manager.jpg|300px|link=|alt=]]
 
You can find the complete API for the Phidget Manager [[here]]. <font color=red> Link to API docs </font>
 
==Purpose==
 
The main function of the Phidget Manager is to launch events when a Phidget attaches or detaches, so you can execute code to prepare those Phidgets for use. For Phidgets that have multiple objects and/or channels (which happens to be most of them), a separate attach and detach event will trigger for each one. For example, when the 1044_0 - PhidgetSpatial is connected, the Phidget Manager would launch three attach events, one for each distinct object, as illustrated below:


[[Image:Manag-singleobject.png|300px|link=|alt=]]
[[Image:manager22.jpg|link=|1000px|alt=]]


....Is actually embedded into an object that also uses the Phidget Common library to open, close, and delete:


[[Image:Manag-inheritedobject.png|300px|link=|alt=]]
Once an attach event triggers, the attach event handler will be called and your code will run. In order to interact with the object, you need to cast it as the correct type and open it. You can determine the correct type by using <code>getChannelClassName()</code> (or the <code>channelClassName </code> property if you're using and object-oriented language). Once cast, you can open it and begin to use methods and properties that belong to that specific object. In the next section we'll look at an example of a program implementing the process described by this diagram.


And this common set of functions does not vary from Phidget device to Phidget device:
==Using the Phidget Manager==


[[Image:Manag-inheritedobjectII.png|300px|link=|alt=]]
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:




This is '''not''' true for the Manager.  The Manager is an object like other Phidget objects, but with a distinct difference - all of its functions are Manager functions:
<div class="source">
<syntaxhighlight lang=c>
int main() {


[[Image:Manag-managerobject.png|300px|link=|alt=]]
    // Define the manager handle and create
    PhidgetManagerHandle device = 0;
    PhidgetManager_create(&device);


For some languages, such as C, this means you do not have to change between a {{Code|Phidget}} type and your specific device type when opening, closing, etc.  All manager functions take a Phidget Manager type object. 
    // Set up event handlers
    PhidgetManager_setOnAttachHandler(device, AttachHandler, NULL);
    PhidgetManager_setOnDetachHandler(device, DetachHandler, NULL);


You can find the API for the Phidget Manager in the API on [[Software Overview#Language Support|the page for your language]].
    // Open the Phidget Manager
    PhidgetManager_open(device);


==Usefulness==
    // Wait for attach/detach events
    printf("Press Enter to end anytime...\n");
    getchar();


Perhaps the most useful function of the Manager is the {{Code|getAttachedDevices()}} function.  This is the tool that returns all attached Phidget devices, as its name implies.  The returned devices are returned as an array:
    // Close and clean up manager object
    PhidgetManager_close(device);
    PhidgetManager_delete(&device);
    return 0;
}


[[Image:Manag-array.png|400px|link=|alt=]]
void CCONV AttachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {


This works due to the fact that all software objects for Phidget devices are Phidget software objects.  So you get a whole array back of Phidget objects, upon which you can use the Phidget Common API - such as getSerialNumber(), or getDeviceName() - to determine what they are and then '''cast''' the array item into the Phidget device that it actually is:
    const char *chanClass;
    // Define handles and spatial variables
    PhidgetAccelerometerHandle accHandle;
    PhidgetGyroscopeHandle gyrHandle;
    PhidgetMagnetometerHandle magHandle;
    double acceleration[3];
    double angularRate[3];
    double fieldStrength[3];


[[Image:Manag-array-expanded.png|600px|link=|alt=]]
    // 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) {


This way, you can use the whole API for your device.
          // 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:
    if(strcmp(chanClass,"PhidgetMagnetometer") == 0) {
 
          // Cast the generic Phidget object into a Phidget Magnetometer object
          magHandle = (PhidgetMagnetometerHandle) device;
          // 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) {
   
    const char *chanClass;
 
    // Get an print the class of this generic Phidget object that has just detached
    Phidget_getChannelClassName(device, &chanClass);
    printf("%s detached!\n",chanClass);
}


When using the Manager expressly for the {{Code|getAttachedDevices()}} function, you must allow time for the PhidgetManager to detect for any Phidgets attached to the system.  This can be achieved by allowing the program to wait a short while before attempting to get the attached devices.  Another method would be to get the attached devices once the PhidgetManager's attach event triggers.


==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.  These and other concepts are discussed on the [[General Phidget Programming]] page.
</syntaxhighlight>
</div>


We even have a specific section of code snippets on using the Manager, in the [[General Phidget Programming#Using the Manager|Using the Manager section of General Phidget Programming]].


Finally, all of our HelloWorld programming examples - available for most of [[Software Overview#Language Support|the languages we support]] - use the Manager to work with any Phidget.
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 16:43, 13 January 2017

General Overview

The Phidget Manager is a software object that can be set up to notify your program whenever a Phidget attached or detaches to your computer.

If you wanted to keep track of several Phidgets on your system that are constantly being plugged in and removed, the Manager is the tool you would use.

Object Structure

You are probably already familiar with Phidgets software objects, where a single set of functions for a class of hardware are contained in one object. For example, the VoltageInput object:

The methods for the Phidget Manager are contained in the same way, but it does not belong to any particular Phidget device. It exists separately, so you can have an active Phidget Manager object even when there are no Phidgets attached.

You can find the complete API for the Phidget Manager here. Link to API docs

Purpose

The main function of the Phidget Manager is to launch events when a Phidget attaches or detaches, so you can execute code to prepare those Phidgets for use. For Phidgets that have multiple objects and/or channels (which happens to be most of them), a separate attach and detach event will trigger for each one. For example, when the 1044_0 - PhidgetSpatial is connected, the Phidget Manager would launch three attach events, one for each distinct object, as illustrated below:


Once an attach event triggers, the attach event handler will be called and your code will run. In order to interact with the object, you need to cast it as the correct type and open it. You can determine the correct type by using getChannelClassName() (or the channelClassName property if you're using and object-oriented language). Once cast, you can open it and begin to use methods and properties that belong to that specific object. In the next section we'll look at an example of a program implementing the process described by this diagram.

Using the Phidget Manager

As with any other Phidget software object, the Phidget Manager must be opened, then attached within your code. Here's an example of how the manager is used in C:


int main() {

     // Define the manager handle and create
     PhidgetManagerHandle device = 0;
     PhidgetManager_create(&device);

     // Set up event handlers
     PhidgetManager_setOnAttachHandler(device, AttachHandler, NULL);
     PhidgetManager_setOnDetachHandler(device, DetachHandler, NULL);

     // Open the Phidget Manager
     PhidgetManager_open(device);

     // Wait for attach/detach events
     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) {

     const char *chanClass;
     // 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:
     if(strcmp(chanClass,"PhidgetMagnetometer") == 0) {

          // Cast the generic Phidget object into a Phidget Magnetometer object
          magHandle = (PhidgetMagnetometerHandle) device;
          // 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) {
     
     const char *chanClass;

     // Get an print the class of this generic Phidget object that has just detached
     Phidget_getChannelClassName(device, &chanClass);
     printf("%s detached!\n",chanClass);
}


For more sample code that uses the Phidget Manager, have a look at our "HelloWorld" programming examples, which are available for most of the languages we support.