Skip to content

Instantly share code, notes, and snippets.

@XakazukinX
Created March 2, 2019 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XakazukinX/ca426d88331b8eb70a44790e543a9f98 to your computer and use it in GitHub Desktop.
Save XakazukinX/ca426d88331b8eb70a44790e543a9f98 to your computer and use it in GitHub Desktop.
i2c-GP2Y0E03 // source https://jsbin.com/veqoxilahe
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
<title>i2c-GP2Y0E03</title>
<script src="../../polyfill/blePolyfill.js"></script>
<script src="../../drivers/i2c-GP2Y0E03.js"></script>
<script src="./main.js"></script>
<style>
p {
color: blue;
text-align: center;
font-size: 24px;
}
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div><input type="button" id="BLECONN" style="width:100%;background-color:#03A9F4;color:#fff;font-weight: bold;" value="BLE接続"></div>
<div><input type="button" id="BLEDISS" style="width:100%;background-color:#FF5722;color:#fff;font-weight: bold;" value="BLE切断" hidden></div>
<P><a href="http://akizukidenshi.com/catalog/g/gI-07547/" target="_blank">Distance Measuring Sensor(GP2Y0E03)</a></P>
<a href="./schematic.png" target="_blank"><img src="./schematic.png" height="350px"></a>
<p id="distance">init</p>
<script id="jsbin-javascript">
"use strict";
const DEVICE_UUID = "928a3d40-e8bf-4b2b-b443-66d2569aed50";
let connectButton;
var valelem;
window.addEventListener(
"load",
function() {
valelem = document.getElementById("distance");
connectButton = document.querySelector("#BLECONN");
connectButton.addEventListener("click", mainFunction);
},
false
);
async function mainFunction() {
var sensor_unit;
try {
var bleDevice = await navigator.bluetooth.requestDevice({
filters: [{ services: [DEVICE_UUID] }] });
var i2cAccess = await navigator.requestI2CAccess(bleDevice);
connectButton.hidden = true;
var port = i2cAccess.ports.get(1);
sensor_unit = new GP2Y0E03(port, 0x40);
await sensor_unit.init();
while (1) {
try {
var distance = await sensor_unit.read();
if (distance != null) {
valelem.innerHTML = "Distance:" + distance + "cm";
} else {
valelem.innerHTML = "out of range";
}
} catch (err) {
console.log("READ ERROR:" + err);
}
await sleep(500);
}
} catch (err) {
console.log("GP2Y0E03 init error");
}
}
function sleep(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
</script>
<script id="jsbin-source-html" type="text/html"><!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
<title>i2c-GP2Y0E03</title>
<script src="../../polyfill/blePolyfill.js"><\/script>
<script src="../../drivers/i2c-GP2Y0E03.js"><\/script>
<script src="./main.js"><\/script>
<style>
p {
color: blue;
text-align: center;
font-size: 24px;
}
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div><input type="button" id="BLECONN" style="width:100%;background-color:#03A9F4;color:#fff;font-weight: bold;" value="BLE接続"></div>
<div><input type="button" id="BLEDISS" style="width:100%;background-color:#FF5722;color:#fff;font-weight: bold;" value="BLE切断" hidden></div>
<P><a href="http://akizukidenshi.com/catalog/g/gI-07547/" target="_blank">Distance Measuring Sensor(GP2Y0E03)</a></P>
<a href="./schematic.png" target="_blank"><img src="./schematic.png" height="350px"></a>
<p id="distance">init</p>
</body>
</html>
</script>
<script id="jsbin-source-javascript" type="text/javascript">"use strict";
const DEVICE_UUID = "928a3d40-e8bf-4b2b-b443-66d2569aed50";
let connectButton;
var valelem;
window.addEventListener(
"load",
function() {
valelem = document.getElementById("distance");
connectButton = document.querySelector("#BLECONN");
connectButton.addEventListener("click", mainFunction);
},
false
);
async function mainFunction() {
var sensor_unit;
try {
var bleDevice = await navigator.bluetooth.requestDevice({
filters: [{ services: [DEVICE_UUID] }] });
var i2cAccess = await navigator.requestI2CAccess(bleDevice);
connectButton.hidden = true;
var port = i2cAccess.ports.get(1);
sensor_unit = new GP2Y0E03(port, 0x40);
await sensor_unit.init();
while (1) {
try {
var distance = await sensor_unit.read();
if (distance != null) {
valelem.innerHTML = "Distance:" + distance + "cm";
} else {
valelem.innerHTML = "out of range";
}
} catch (err) {
console.log("READ ERROR:" + err);
}
await sleep(500);
}
} catch (err) {
console.log("GP2Y0E03 init error");
}
}
function sleep(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
</script></body>
</html>
"use strict";
const DEVICE_UUID = "928a3d40-e8bf-4b2b-b443-66d2569aed50";
let connectButton;
var valelem;
window.addEventListener(
"load",
function() {
valelem = document.getElementById("distance");
connectButton = document.querySelector("#BLECONN");
connectButton.addEventListener("click", mainFunction);
},
false
);
async function mainFunction() {
var sensor_unit;
try {
var bleDevice = await navigator.bluetooth.requestDevice({
filters: [{ services: [DEVICE_UUID] }] });
var i2cAccess = await navigator.requestI2CAccess(bleDevice);
connectButton.hidden = true;
var port = i2cAccess.ports.get(1);
sensor_unit = new GP2Y0E03(port, 0x40);
await sensor_unit.init();
while (1) {
try {
var distance = await sensor_unit.read();
if (distance != null) {
valelem.innerHTML = "Distance:" + distance + "cm";
} else {
valelem.innerHTML = "out of range";
}
} catch (err) {
console.log("READ ERROR:" + err);
}
await sleep(500);
}
} catch (err) {
console.log("GP2Y0E03 init error");
}
}
function sleep(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment