Initialization
You must then tell the API that it must call the callback when a new module is connected.
YAPI
.
RegisterDeviceArrivalCallback
(
deviceArrival
);
Note that if modules are already connected when the callback is registered, the callback is called for
each of the already connected modules.
Triggering callbacks
A classis issue of callback programming is that these callbacks can be triggered at any time,
including at times when the main program is not ready to receive them. This can have undesired side
effects, such as dead-locks and other race conditions. Therefore, in the Yoctopuce API, module
arrival/departure callbacks are called only when the
UpdateDeviceList()
function is running.
You only need to call
UpdateDeviceList()
at regular intervals from a timer or from a specific
thread to precisely control when the calls to these callbacks happen:
// waiting loop managing callbacks
while
(true)
{
// module arrival / departure callback
YAPI
.
UpdateDeviceList
(ref
errmsg
);
// non active waiting time managing other callbacks
YAPI
.
Sleep
(
500
,
ref
errmsg
);
}
In a similar way, it is possible to have a callback when a module is disconnected. You can find a
complete example implemented in your favorite programming language in the
Examples/Prog-
EventBased
directory of the corresponding library.
Be aware that in most programming languages, callbacks must be global procedures, and not
methods. If you wish for the callback to call the method of an object, define your callback as a global
procedure which then calls your method.
20. Advanced programming
126
www.yoctopuce.com
Содержание Yocto-Display
Страница 1: ...Yocto Display User Manual...
Страница 2: ......
Страница 40: ...34 www yoctopuce com...
Страница 52: ...46 www yoctopuce com...
Страница 84: ...78 www yoctopuce com...
Страница 92: ...86 www yoctopuce com...
Страница 130: ...124 www yoctopuce com...
Страница 158: ...22 High level API Reference 152 www yoctopuce com On failure throws an exception or returns a negative error code...
Страница 422: ...416 www yoctopuce com...
Страница 424: ...24 Characteristics 418 www yoctopuce com...