Yoctopuce Yocto-I2C Mode D'emploi page 138

Table des Matières

Publicité

16. Utilisation du Yocto-I2C en JavaScript / EcmaScript
<!DOCTYPE html>
<html>
<head>
<meta
charset="UTF-8">
<title>Hello
World</title>
<script
src="../../lib/yocto_api.js"></script>
<script
src="../../lib/yocto_i2cport.js"></script>
<script>
let i2cPort;
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);
}
refresh();
}
async function refresh()
{
let serial = document.getElementById('serial').value;
if(serial == '') {
// by default use any connected module suitable for the demo
let anyI2c = YI2cPort.FirstI2cPort();
if(anyI2c) {
let module = await anyI2c.module();
serial = await module.get_serialNumber();
document.getElementById('serial').value = serial;
}
}
i2cPort = YI2cPort.FindI2cPort(serial+'.i2cPort');
if(await i2cPort.isOnline()) {
document.getElementById('msg').value = '';
// sample code reading MCP9804 temperature sensor
await i2cPort.set_i2cMode("400kbps");
await i2cPort.set_i2cVoltageLevel(YI2cPort.I2CVOLTAGELEVEL_3V3);
await i2cPort.reset();
let toSend = [0x05];
let received = await i2cPort.i2cSendAndReceiveArray(0x1f, toSend, 2);
tempReg = (received[0]
if(tempReg &
tempReg
}
else
{
tempReg
}
document.getElementById('temp').value
}
else
{
document.getElementById('msg').value
}
setTimeout(refresh, 500);
}
startDemo();
</script>
</head>
<body>
Module to use:
<input
<input
id='msg' style='color:red;border:none;' readonly><br>
temperature :
<input
<i>Note: Make sure voltage are configured
</body>
</html>
Aucune installation n'est nécessaire pout utiliser cet exemple, il suffit d'ouvrir la page HTML avec un
navigateur web.
130
<<
8)
+
received[1]
0x1000) {
-=
0x2000
//
perform sign extension
&=
0x0fff
//
clear
id='serial'>
id='temp' readonly><br>
status bits
=
(tempReg
/
16.0).toString();
=
'Module not connected';
properly!</i>
www.yoctopuce.com

Publicité

Table des Matières
loading

Table des Matières