Skip to content

Instantly share code, notes, and snippets.

@tadfmac
Created October 16, 2017 13:02
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 tadfmac/960283e4b1c2bbcfb7cef203e6e24214 to your computer and use it in GitHub Desktop.
Save tadfmac/960283e4b1c2bbcfb7cef203e6e24214 to your computer and use it in GitHub Desktop.
CHIRIMEN-DCFAN

HTML

<div id="temp">1</div>
<script src="https://mz4u.net/libs/gc2/polyfill.js"></script>
<script src="https://mz4u.net/libs/gc2/i2c-ADT7410.js"></script>

JavaScript

(async() => {
  var sleep = (ms) => {
    return new Promise((resolve) => setTimeout(resolve, ms));
  };
  var temp = document.getElementById("temp");
  var gpioAccess = await navigator.requestGPIOAccess();
  var port = gpioAccess.ports.get(26);
  await port.export("out");
  var i2cAccess = await navigator.requestI2CAccess();
  var i2cPort = i2cAccess.ports.get(1);
  var adt7410 = new ADT7410(i2cPort, 0x48);
  await adt7410.init();
  while (1) {
    var value = await adt7410.read();
    temp.innerHTML = value;
    temp.style.color = (value > 30) ? "red" : "black";
    if (value > 32) {
      port.write(1);
    } else {
      port.write(0);
    }
    await sleep(1000);
  }
})();
@satakagi
Copy link

アロー関数式とか知らない・苦手な人向け(自分だw)のコードはこんな感じですかね。

JAVASCRIPT

function  sleep(ms){
  return new Promise( function(resolve) {
    setTimeout(resolve, ms);
  });
}


async function mainFunc() {
  var temp = document.getElementById("temp");
  var gpioAccess = await navigator.requestGPIOAccess();
  var port = gpioAccess.ports.get(26);
  await port.export("out");
  var i2cAccess = await navigator.requestI2CAccess();
  var i2cPort = i2cAccess.ports.get(1);
  var adt7410 = new ADT7410(i2cPort, 0x48);
  await adt7410.init();
  while (1) {
    var value = await adt7410.read();
    temp.innerHTML = value;
    temp.style.color = (value > 30) ? "red" : "black";
    if (value > 32) {
      port.write(1);
    } else {
      port.write(0);
    }
    await sleep(1000);
  }
}
mainFunc();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment