
To give it a try, go into one of the example directory (for instance example_nodejs/Doc-Inventory).
You will see that it include an application description file (package.json) and a source file (demo.js).
To download and setup the libraries needed by this example, just run:
npm install
Once done, you can start the example file using:
node demo.js
Using a local copy of the Yoctopuce library with node.js
If for some reason you need to make changes to the Yoctopuce library, you can easily configure your
project to use the local copy in the
lib/
subdirectory rather than the official npm package. In order
to do so, simply type the following command in your project directory:
npm link ../../lib
Using the Yoctopuce library within a browser (HTML)
For HTML examples, it is even simpler: there is nothing to install. Each example is a single HTML file
that you can open in a browser to try it. In this context, loading the Yoctopuce library is no different
from any standard HTML script include tag.
Using the Yoctoluce library on older JavaScript engines
If you need to run this library on older JavaScript engines, you can use Babel
3
to transpile your code
and the library into older JavaScript standards. To install Babel with typical settings, simply use:
npm instal -g babel-cli
npm instal babel-preset-env
You would typically ask Babel to put the transpiled files in another directory, named
compat
for
instance. Your files and all files of the Yoctopuce library should be transpiled, as follow:
babel --presets env demo.js --out-dir compat/
babel --presets env ../../lib --out-dir compat/
Although this approach is based on node.js toolchain, it actually works as well for transpiling
JavaScript files for use in a browser. The only thing that you cannot do so easily is transpiling
JavaScript code embedded directly in an HTML page. You have to use an external script file for
using EcmaScript 2017 syntax with Babel.
Babel has many smart features, such as a watch mode that will automatically refresh transpiled files
whenever the source file is changed, but this is beyond the scope of this note. You will find more in
Babel documentation.
Backward-compatibility with the old JavaScript library
This new library is not fully backward-compatible with the old JavaScript library, because there is no
way to transparently map the old blocking API to the new asynchronous API. The method names
however are the same, and old synchronous code can easily be made asynchronous just by adding
the proper
await
keywords before the method calls. For instance, simply replace:
beaconState = module.
get_beacon
()
;
by
3
http://babeljs.io
8. Using Yocto-RangeFinder with JavaScript / EcmaScript
www.yoctopuce.com
39
Содержание 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: ......