// Setup the API to use the VirtualHub on local machine
let
errmsg =
new
YErrorMsg
()
;
if(await
YAPI.
RegisterHub
(
'127.0.0.1'
, errmsg
)
!= YAPI.
SUCCESS
)
{
console.
log
(
'Cannot contact VirtualHub on 127.0.0.1: '
+errmsg.
msg
)
;
return
;
}
// Select specified device, or use first available one
let
serial = process.
argv
[
process.
argv
.
length
-
1
]
;
if(
serial
[
8
]
!=
'-'
)
{
// by default use any connected module suitable for the demo
let
anysensor = YGps.
FirstGps
()
;
if(
anysensor
)
{
let
module =
await
anysensor.
module
()
;
serial =
await
module.
get_serialNumber
()
;
}
else
{
console.
log
(
'No matching sensor connected, check cable !'
)
;
return
;
}
}
console.
log
(
'Using device '
+serial
)
;
gps = YGps.
FindGps
(
".gps"
)
;
refresh
()
;
}
async
function
refresh
()
{
if
(
!
await
gps.
isOnline
())
{
console.
log
(
'Module not connected'
)
;
}
else
if
(await
gps.
get_isFixed
()
!= YGps.
ISFIXED_TRUE
)
{
console.
log
(
'fixing...'
)
;
}
else
{
console.
log
(
'Position : '
+
(await
gps.
get_latitude
())
+
' '
+
(await
gps.
get_longitude
()))
;
}
setTimeout
(
refresh,
500
)
;
}
startDemo
()
;
As explained at the beginning of this chapter, you need to have Node.js and jspm installed to try this
example. When done, you can type the following two commands to automatically download and
install the dependencies for building this example:
npm install
jspm install
You can the start the sample code within Node.js using the following command, replacing the [...] by
the arguments that you want to pass to the demo code:
jspm run src/demo.js
[
...
]
Same example, but this time running in a browser
If you want to see how to use the library within a browser, switch to the directory
example_html/
Doc-GettingStarted-Yocto-GPS
. You will find there a subdirectory
src
as well with a very similar
source code (below), but with a few changes compared to the Node.js version since it has to interact
through an HTML page rather than through the JavaScript console.
import
{
YAPI, YErrorMsg, YGps
}
from
'yoctolib-es'
;
async
function
startDemo
()
{
await
YAPI.
LogUnhandledPromiseRejections
()
;
await
YAPI.
DisableExceptions
()
;
// Setup the API to use the VirtualHub on local machine
let
errmsg =
new
YErrorMsg
()
;
if(await
YAPI.
RegisterHub
(
'127.0.0.1'
, errmsg
)
!= YAPI.
SUCCESS
)
{
alert
(
'Cannot contact VirtualHub on 127.0.0.1: '
+errmsg.
msg
)
;
}
7. Using Yocto-GPS with JavaScript / EcmaScript
42
www.yoctopuce.com
Содержание Yocto-GPS
Страница 1: ...Yocto GPS User s guide...
Страница 2: ......
Страница 6: ...22 Characteristics 557 Blueprint 559 Index 561...
Страница 10: ...4 www yoctopuce com...
Страница 14: ...8 www yoctopuce com...
Страница 18: ...12 www yoctopuce com...
Страница 22: ...16 www yoctopuce com...
Страница 38: ...32 www yoctopuce com...
Страница 42: ...36 www yoctopuce com...
Страница 54: ...48 www yoctopuce com...
Страница 72: ...66 www yoctopuce com...
Страница 92: ...86 www yoctopuce com...
Страница 122: ...116 www yoctopuce com...
Страница 132: ...126 www yoctopuce com...
Страница 159: ...20 High level API Reference On failure throws an exception or returns a negative error code www yoctopuce com 153...
Страница 564: ...22 Characteristics 558 www yoctopuce com...
Страница 565: ......
Страница 566: ......