Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCF8591 Example 作成 #48

Merged
merged 4 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/pcf8591/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1"
/>
<title>i2c-PCF8591</title>
<script src="https://r.chirimen.org/polyfill.js"></script>
<script src="./main.js" type="module"></script>
<style>
p {
color: blue;
text-align: center;
font-size: 24px;
}

img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>

<body>
<p>
<a>PCF8591</a>
</p>
<img src="./schematic.png" height="400px" />
<p id="value">initializing...</p>
<p>
DAC Voltage (Range: 0V - 3.3V)<br />
<input type="number" name="voltage" id="voltage" value="3.3" />V
<input type="button" value="Set" id="setVoltage" />
</p>
</body>
</html>
35 changes: 35 additions & 0 deletions examples/pcf8591/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import PCF8591 from "https://unpkg.com/@chirimen/pcf8591?module";

main();

let pcf8591;
let dacVoltage;

async function main() {
const value = document.getElementById("value");
const i2cAccess = await navigator.requestI2CAccess();
const port = i2cAccess.ports.get(1);
pcf8591 = new PCF8591(port, 0x48);
await pcf8591.init();
dacVoltage = document.getElementById("voltage").value;
await pcf8591.setDAC(dacVoltage);

while (true) {
let output = "";

// PCF8591 has 4 channels
for (let channel = 0; channel < 4; channel++) {
const voltage = await pcf8591.readADC(channel);
output += `CH${channel}: ${voltage.toFixed(3)}V<br>`;
}
value.innerHTML = output;

await sleep(500);
}
}

async function setDACVoltage() {
dacVoltage = document.getElementById("voltage").value;
await pcf8591.setDAC(dacVoltage);
}
document.getElementById("setVoltage").addEventListener("click", setDACVoltage);
5 changes: 5 additions & 0 deletions examples/pcf8591/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "i2c-pcf8591-example",
"version": "1.0.0",
"private": true
}
Binary file added examples/pcf8591/schematic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions node-examples/pcf8591/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { requestI2CAccess } = require("node-web-i2c");
const PCF8591 = require("@chirimen/pcf8591");
const { promisify } = require("util");
const sleep = promisify(setTimeout);

main();

async function main() {
const i2cAccess = await requestI2CAccess();
const port = i2cAccess.ports.get(1);
const pcf8591 = new PCF8591(port, 0x48);
await pcf8591.init();

// Set DAC voltage (Range: 0V - 3.3V)
await pcf8591.setDAC(3.3);

while (true) {
let output = "";

// PCF8591 has 4 channels
for (let channel = 0; channel < 4; channel++) {
const voltage = await pcf8591.readADC(channel);
output += `CH${channel}:${voltage.toFixed(3)}V `;
}
console.log(output);

await sleep(500);
}
}
9 changes: 9 additions & 0 deletions node-examples/pcf8591/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "i2c-pcf8591-node-example",
"version": "1.0.0",
"private": true,
"dependencies": {
"@chirimen/pcf8591": "latest",
"node-web-i2c": "latest"
}
}
5 changes: 5 additions & 0 deletions node-examples/pcf8591/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# PCF8591 8bit AD,DA コンバーター

## 配線図

![配線図](./schematic.png "schematic")
Binary file added node-examples/pcf8591/schematic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.