20. Advanced programming
The preceding chapters have introduced, in each available language, the basic programming
functions which can be used with your Yocto-Display module. This chapter presents in a more
generic manner a more advanced use of your module. Examples are provided in the language which
is the most popular among Yoctopuce customers, that is C#. Nevertheless, you can find complete
examples illustrating the concepts presented here in the programming libraries of each language.
To remain as concise as possible, examples provided in this chapter do not perform any error
handling. Do not copy them "as is" in a production application.
20.1. Event programming
The methods to manage Yoctopuce modules which we presented to you in preceding chapters were
polling functions, consisting in permanently asking the API if something had changed. While easy to
understand, this programming technique is not the most efficient, nor the most reactive. Therefore,
the Yoctopuce programming API also provides an event programming model. This technique
consists in asking the API to signal by itself the important changes as soon as they are detected.
Each time a key parameter is modified, the API calls a callback function which you have defined in
advance.
Detecting module arrival and departure
Hot-plug
management is important when you work with USB modules because, sooner or later, you
will have to connect or disconnect a module when your application is running. The API is designed to
manage module unexpected arrival or departure in a transparent way. But your application must take
this into account if it wants to avoid pretending to use a disconnected module.
Event programming is particularly useful to detect module connection/disconnection. Indeed, it is
simpler to be told of new connections rather than to have to permanently list the connected modules
to deduce which ones just arrived and which ones left. To be warned as soon as a module is
connected, you need three pieces of code.
The callback
The callback is the function which is called each time a new Yoctopuce module is connected. It takes
as parameter the relevant module.
static
void
deviceArrival
(
YModule
m
)
{
Console
.
WriteLine
(
"New module : "
+
m
.
get_serialNumber
());
}
www.yoctopuce.com
125
Summary of Contents for Yocto-Display
Page 1: ...Yocto Display User Manual...
Page 2: ......
Page 40: ...34 www yoctopuce com...
Page 52: ...46 www yoctopuce com...
Page 84: ...78 www yoctopuce com...
Page 92: ...86 www yoctopuce com...
Page 130: ...124 www yoctopuce com...
Page 422: ...416 www yoctopuce com...