
int
main
(
int
argc,
const
char
* argv
[])
{
NSError
*error;
if
(
argc <
2
)
{
usage
()
;
}
@autoreleasepool
{
// Setup the API to use local USB devices
if([
YAPI RegisterHub:
@"usb"
: &error
]
!= YAPI_SUCCESS
)
{
NSLog
(
@"RegisterHub error: %@"
,
[
error localizedDescription
])
;
return
1
;
}
NSString
*target =
[
NSString
stringWithUTF8String:argv
[
1
]]
;
YRangeFinder *rf;
YLightSensor *ir;
YTemperature *tmp;
if
([
target isEqualToString:
@"any"
])
{
rf =
[
YRangeFinder FirstRangeFinder
]
;
if
(
rf ==
NULL
)
{
NSLog
(
@"No module connected (check USB cable)"
)
;
return
1
;
}
}
else
{
rf =
[
YRangeFinder FindRangeFinder:
[
target stringByAppendingString:
@".rangeFinder1"
]
]
;
}
if
(
!
[
rf isOnline
])
{
NSLog
(
@"Module not connected (check identification and USB cable)"
)
;
}
NSString
*serial =
[[
rf module
]
serialNumber
]
;
ir =
[
YLightSensor FindLightSensor:
[
serial stringByAppendingString:
@".lightSensor1"
]]
;
tmp =
[
YTemperature FindTemperature:
[
serial stringByAppendingString:
@".temperature1"
]]
;
while(
1
)
{
if(
!
[
rf isOnline
])
{
NSLog
(
@"Module not connected (check identification and USB cable)
\n
"
)
;
break
;
}
NSLog
(
@"Distance : %f
\n
"
,
[
rf get_currentValue
])
;
NSLog
(
@"Ambiant IR : %f
\n
"
,
[
ir get_currentValue
])
;
NSLog
(
@"Temperature : %f
\n
"
,
[
tmp get_currentValue
])
;
NSLog
(
@" (press Ctrl-C to exit)
\n
"
)
;
[
YAPI Sleep:
1000
:
NULL
]
;
}
[
YAPI FreeAPI
]
;
}
return
0
;
}
There are only a few really important lines in this example. We will look at them in details.
yocto_api.h et yocto_rangefinder.h
These two import files provide access to the functions allowing you to manage Yoctopuce modules.
yocto_api.h
must always be used,
yocto_rangefinder.h
is necessary to manage modules
containing a range finder, such as Yocto-RangeFinder.
[YAPI RegisterHub]
The
[YAPI RegisterHub]
function initializes the Yoctopuce API and indicates where the
modules should be looked for. When used with the parameter
@"usb"
, it will use the modules
locally connected to the computer running the library. If the initialization does not succeed, this
function returns a value different from
YAPI_SUCCESS
and
errmsg
contains the error message.
[RangeFinder FindRangeFinder]
The
[RangeFinder 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
11. Using Yocto-RangeFinder with Objective-C
66
www.yoctopuce.com
Summary of Contents for Yocto-RangeFinder
Page 1: ...Yocto RangeFinder User s guide...
Page 2: ......
Page 18: ...12 www yoctopuce com...
Page 26: ...20 www yoctopuce com...
Page 42: ...36 www yoctopuce com...
Page 70: ...gcc lyocto lm lpthread lusb 1 0 lstdc 10 Using Yocto RangeFinder with C 64 www yoctopuce com...
Page 90: ...84 www yoctopuce com...
Page 120: ...114 www yoctopuce com...
Page 130: ...124 www yoctopuce com...
Page 484: ...478 www yoctopuce com...
Page 488: ...482 www yoctopuce com...
Page 490: ...23 Characteristics 484 www yoctopuce com...
Page 491: ......
Page 492: ......