Phidget Manager: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
 
(14 intermediate revisions by 3 users not shown)
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 systemIf you want to 'search for all attached Phidgets', in your code the Manager is the tool you would use.
The Phidget Manager is an interface into the device channels available to the Phidget libraryThe API is strictly asynchronous, and continuously monitors channels as they attach and detach.


==Object Structure==
Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or becomes available over the network), a manager attach event is fired for each channel available from the Phidget.  When the Phidget is removed from the system, a manger detach event is fired for each channel that is no longer available.


You are probably already used to Phidget software objects, where a single set of functions for a board - say, an Interface Kit:
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 has ''appeared'', and 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 is not fired.  A manager '''detach''' event is fired when the Phidget is removed from the system.


[[Image:Manag-singleobject.png|300px|link=|alt=]]
==Purpose==


....Is actually embedded into an object that also uses the Phidget Common library to open, close, and delete:
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:


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


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=]]
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.


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:
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.


[[Image:Manag-managerobject.png|300px|link=|alt=]]
What follows is an example of how to use a manager in C:


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. 
<div class="source">
<syntaxhighlight lang=c>


You can find the API for the Phidget Manager in the API on [[Software Overview#Language Support|the page for your language]].
static void CCONV
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle mgrch) {


==Usefulness==
    PhidgetLog_log("%P attached", mgrch);
}


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:
static void CCONV
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle mgrch) {


[[Image:Manag-array.png|400px|link=|alt=]]
    PhidgetLog_log("%P detached", mgrch);
}


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:
int
main(int argc, char **argv) {
    PhidgetManagerHandle mgr;


[[Image:Manag-array-expanded.png|600px|link=|alt=]]
    PhidgetManager_create(&mgr);


This way, you can use the whole API for your device.
    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);


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.
    PhidgetManager_open(mgr);


==Using the Phidget Manager==
    /* Do something long running, like run a GUI threads */
    /* Now close and delete the manager */
    PhidgetManager_close(mgr);
    PhidgetManager_delete(&mgr);
    exit(0);
}
</syntaxhighlight>
</div>
 
A further example would be "HelloWorld" for most [[Programming_Resources|supported languages]] uses 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.
==Opening a Manager Channel==


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]].
A manager channel passed to a Phidget Manager attach handler is assigned all of the matching parameters for the Phidget device and the device channel the manager channel refers to; however, the manager channel is not attached to the device channel, and is not receiving data and status information from the device.


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.
User code may {{Code|Open()}} a manager channel, and just like a channel created by the user code, the Phidget library will begin trying to match the manager channel with a device channel.  User code may also set attach and detach handlers on the manager channel, and set change event handlers.  The manager channel can be treated exactly the same as a user channel.

Latest revision as of 16:41, 24 March 2022

General Overview

The Phidget Manager is an interface into the device channels available to the Phidget library. The API is strictly asynchronous, and 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 becomes available over the network), a manager attach event is fired for each channel available from the Phidget. When the Phidget is removed from the system, a manger detach event is fired for each channel that is no longer available.

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 has appeared, and 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 is not fired. A manager detach event is fired when the Phidget 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 mgrch) {

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

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

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

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.

Opening a Manager Channel

A manager channel passed to a Phidget Manager attach handler is assigned all of the matching parameters for the Phidget device and the device channel the manager channel refers to; however, the manager channel is not attached to the device channel, and is not receiving data and status information from the device.

User code may Open() a manager channel, and just like a channel created by the user code, the Phidget library will begin trying to match the manager channel with a device channel. User code may also set attach and detach handlers on the manager channel, and set change event handlers. The manager channel can be treated exactly the same as a user channel.