
17.5. Control of the RangeFinder function
A few lines of code are enough to use a Yocto-RangeFinder. Here is the skeleton of a Java code
snippet to use the RangeFinder function.
[
...
]
// Retrieving the object representing the module (connected here locally by USB)
YAPI.
EnableUSBHost
(this)
;
YAPI.
RegisterHub
(
"usb"
)
;
rangefinder = YRangeFinder.
FindRangeFinder
(
"YRNGFND1-123456.rangeFinder"
)
;
// Hot-plug is easy: just check that the device is online
if
(
rangefinder.
isOnline
())
{
//Use rangefinder.get_currentValue()
...
}
[
...
]
Let us look at these lines in more details.
YAPI.EnableUSBHost
The
YAPI.EnableUSBHost
function initializes the API with the Context of the current application.
This function takes as argument an object of the
android.content.Context
class (or of a
subclass). If you intend to connect your application only to other machines through the network, this
function is facultative.
YAPI.RegisterHub
The
yAPI.RegisterHub
function initializes the Yoctopuce API and indicates where the modules
should be looked for. The parameter is the address of the virtual hub able to see the devices. If the
string "usb" is passed as parameter, the API works with modules locally connected to the machine. If
the initialization does not succeed, an exception is thrown.
YRangeFinder.FindRangeFinder
The
YRangeFinder.FindRangeFinder
function allows you to find a range finder from the
serial number of the module on which it resides and from its function name. You can use logical
names as well, as long as you have initialized them. Let us imagine a Yocto-RangeFinder module
with serial number
YRNGFND1-123456
which you have named "
MyModule
", and for which you have
given the
rangeFinder
function the name "
MyFunction
". The following five calls are strictly equivalent,
as long as "
MyFunction
" is defined only once.
rangefinder = YRangeFinder.
FindRangeFinder
(
"YRNGFND1-123456.rangeFinder"
)
rangefinder = YRangeFinder.
FindRangeFinder
(
"YRNGFND1-123456.MyFunction"
)
rangefinder = YRangeFinder.
FindRangeFinder
(
"MyModule.rangeFinder"
)
rangefinder = YRangeFinder.
FindRangeFinder
(
"MyModule.MyFunction"
)
rangefinder = YRangeFinder.
FindRangeFinder
(
"MyFunction"
)
YRangeFinder.FindRangeFinder
returns an object which you can then use at will to control
the range finder.
isOnline
The
isOnline()
method of the object returned by
YRangeFinder.FindRangeFinder
allows
you to know if the corresponding module is present and in working order.
get_currentValue
The
get_currentValue()
method
of
the
object
returned
by
YRangeFinder.FindRangeFinder
allows you to obtain the distance measured by the sensor,
in mm or in inches depending on the sensor configuration.
17. Using the Yocto-RangeFinder with Android
106
www.yoctopuce.com
Содержание Yocto-RangeFinder
Страница 1: ...Yocto RangeFinder User s guide...
Страница 2: ......
Страница 18: ...12 www yoctopuce com...
Страница 26: ...20 www yoctopuce com...
Страница 42: ...36 www yoctopuce com...
Страница 70: ...gcc lyocto lm lpthread lusb 1 0 lstdc 10 Using Yocto RangeFinder with C 64 www yoctopuce com...
Страница 90: ...84 www yoctopuce com...
Страница 120: ...114 www yoctopuce com...
Страница 130: ...124 www yoctopuce com...
Страница 158: ...21 High level API Reference On failure throws an exception or returns a negative error code 152 www yoctopuce com...
Страница 484: ...478 www yoctopuce com...
Страница 488: ...482 www yoctopuce com...
Страница 490: ...23 Characteristics 484 www yoctopuce com...
Страница 491: ......
Страница 492: ......