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.
Detecting a modification in the value of a sensor
The Yoctopuce API also provides a callback system allowing you to be notified automatically with the
value of any sensor, either when the value has changed in a significant way or periodically at a
preset frequency. The code necessary to do so is rather similar to the code used to detect when a
new module has been connected.
This technique is useful in particular if you want to detect very quick value changes (within a few
milliseconds), as it is much more efficient than reading repeatedly the sensor value and therefore
gives better performances.
Calliback invocation
To enable a better control, value change callbacks are only called when the
YAPI.Sleep()
and
YAPI.HandleEvents()
functions are running. Therefore, you must call one of these functions at
a regular interval, either from a timer or from a parallel thread.
while
(
true
)
{
// inactive waiting loop allowing you to trigger
// value change callbacks
YAPI.
Sleep
(
500
,
ref
errmsg
)
;
}
In programming environments where only the interface thread is allowed to interact with the user, it is
often appropriate to call
YAPI.HandleEvents()
from this thread.
17. Advanced programming
118
www.yoctopuce.com
Summary of Contents for Yocto-GPS
Page 1: ...Yocto GPS User s guide...
Page 2: ......
Page 6: ...22 Characteristics 557 Blueprint 559 Index 561...
Page 10: ...4 www yoctopuce com...
Page 14: ...8 www yoctopuce com...
Page 18: ...12 www yoctopuce com...
Page 22: ...16 www yoctopuce com...
Page 38: ...32 www yoctopuce com...
Page 42: ...36 www yoctopuce com...
Page 54: ...48 www yoctopuce com...
Page 72: ...66 www yoctopuce com...
Page 92: ...86 www yoctopuce com...
Page 122: ...116 www yoctopuce com...
Page 132: ...126 www yoctopuce com...
Page 564: ...22 Characteristics 558 www yoctopuce com...
Page 565: ......
Page 566: ......