7. Utilisation du Yocto-0-10V-Tx en JavaScript / EcmaScript
{
let serial = document.getElementById('serial').value;
if(serial == '') {
// by default use any connected module suitable for the demo
let anyOut = YVoltageOutput.FirstVoltageOutput();
if(anyOut) {
let module = await anyOut.module();
serial = await module.get_serialNumber();
document.getElementById('serial').value = serial;
}
}
vout1 = YVoltageOutput.FindVoltageOutput(serial+'.voltageOutput1');
vout2 = YVoltageOutput.FindVoltageOutput(serial+'.voltageOutput2');
if(await vout1.isOnline()) {
document.getElementById('msg').value = '';
} else {
document.getElementById('msg').value = 'Module not connected';
setTimeout(refresh, 500);
}
}
window.updateVoltage = function(voltage)
{
vout1.set_dutyCycle(voltage);
vout2.dutyCycleMove(voltage,3000); // smooth change
};
startDemo();
</head>
<body>
Module to use:
<input
<input
id='msg' style='color:red;border:none;' readonly><br>
<a
href='javascript:updateVoltage(0);'>Change output voltage to 0
<a
href='javascript:updateVoltage(0.333);'>Change output voltage to 3.333
<a
href='javascript:updateVoltage(5);'>Change output voltage to 5.0
<a
href='javascript:updateVoltage(7.5);'>Change output voltage to 7.5
<a
href='javascript:updateVoltage(10.0);'>Change output voltage to 10.0
</body>
</html>
Aucune installation n'est nécessaire pout utiliser cet exemple, il suffit d'ouvrir la page HTML avec un
navigateur web.
7.4. Contrôle de la partie module
Chaque module peut-être contrôlé d'une manière similaire, vous trouverez ci dessous un simple
programme d'exemple affichant les principaux paramètres d'un module et permettant d'activer la
balise de localisation.
"use
strict";
require('yoctolib-es2017/yocto_api.js');
async function startDemo(args)
{
await YAPI.LogUnhandledPromiseRejections();
// 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:
return;
}
// Select the relay to use
let
module
= YModule.FindModule(args[0]);
if(await module.isOnline()) {
if(args.length
if(args[1] == 'ON') {
await module.set_beacon(YModule.BEACON_ON);
} else {
await module.set_beacon(YModule.BEACON_OFF);
}
36
</script>
id='serial'>
> 1) {
// immediate change
'+errmsg.msg);
V</a><br>
V</a><br>
V</a><br>
V</a><br>
V</a><br>
www.yoctopuce.com