narita2 / -internship

internship
0 stars 0 forks source link

焦電素子、人感センサ(KP-IR412)の学習 #6

Open narita2 opened 6 years ago

narita2 commented 6 years ago

素子とは

  • 電気回路などの構成要素
  • 装置・電子回路などの構成要素となる個々の部品
  • 独立した固有の機能をもつ
    • エネルギーの発生・変換などの機能をもつ能動素子(トランジスタ・圧電素子など)と、抵抗・コンデンサーなどの受動素子に分かれる

焦電素子とは


赤外線センサ

赤外線とは

narita2 commented 6 years ago

人感センサCによる記述

C言語での記述を元にJS

(HIGH)

(LOW)

narita2 commented 6 years ago

i2c-grove-light

'use strict';

window.addEventListener('load', function (){
  var head = document.querySelector('#head');

  navigator.requestI2CAccess().then((i2cAccess)=>{
    var port = i2cAccess.ports.get(1);
    var grovelight = new GROVELIGHT(port,0x29);
    grovelight.init().then(()=>{
      setInterval(()=>{
        grovelight.read().then((value)=>{
//            console.log('value:', value);
          head.innerHTML = value ? value : head.innerHTML;
        });
      },200);
    });
  }).catch(e=> console.error('error', e));
}, false);

readGpioValue

'use strict';

window.addEventListener('load', function (){
  navigator.requestGPIOAccess().then(
    function(gpioAccess){
      console.log("GPIO ready!");
      return gpioAccess;
    }).then(gpio=>{
      var port = gpio.ports.get(5);
      port.export("in").then(()=>{
        setInterval(()=>{
          port.read().then((value)=>{
            console.log("gpio= "+value);
          });
        },1000);
      });
  }).catch(error=>{
    console.log("Failed to get GPIO access catch: " + error.message);
  });
}, false);

narita2 commented 6 years ago

未完成

<!--
Created using JS Bin
http://jsbin.com

Copyright (c) 2018 by anonymous (http://jsbin.com/calahisimu/6/edit)

Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<style id="jsbin-css">

#viewMessage{
  width:60px;
  height:60px;
  border-radius:30px;
  background-color:black;
}

</style>
</head>
<body>

  <div id="viewMessage"></div>
  <script src="https://mz4u.net/libs/gc2/polyfill.js"></script>

<script id="jsbin-javascript">

(async ()=>{
    var sleep = (ms)=>{
        return new Promise((resolve)=>setTimeout(resolve,ms));
    };

    var viewMessage = document.getElementById("viewMessage");

    var gpioAccess = navigator.requestGPIOAccess();
    //var v = 0;
    var port = gpioAccess.ports.get(12); //人感センサのポート
    await port.export("in");//入力モードで初期化

  function detection(v){
    if(v === 0){//
        //port.write(0);
      viewMessage.style.backgroundColor = "black";
    }else{
        //port.write(1);
      viewMessage.style.backgroundColor = "red";
    }
  }

//   await port.export("in");
//   port.onchange = (val)=>{
//       val ^= 1;
//     detection(val);
//   }

 while(1){
   var val = await port.read();
   val ^= 0;//プルダウンに接続したので
   detection(val);
   await sleep(100);
 }
})();

</script>
</body>
</html>
narita2 commented 6 years ago

プログラム概要 試

  1. CSSにある図と対応づける(id)
  2. Web GPIOを利用するために最初のAPI呼び出し
  3. GPIOポート番号(12)を指定してportオブジェクト取得
  4. 取得したGPIOポートを「入力モード」で初期化
  5. 人感センサが反応したときの処理

    CSSにある図の色を変える

narita2 commented 6 years ago

Promise.all()

narita2 commented 6 years ago
3
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com

Copyright (c) 2018 by anonymous (http://jsbin.com/qomekonare/3/edit)

Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<style id="jsbin-css">
#view{
  width:60px;
  height:60px;
  border-radius:30px;
  background-color:black;
}
</style>
</head>
<body>
  <div id="view"></div>
  <script src="https://mz4u.net/libs/gc2/polyfill.js"></script>
<script id="jsbin-javascript">
(async ()=>{
//   var sleep = (ms)=>{ // 一定時間待つ関数
//     return new Promise((resolve)=>setTimeout(resolve,ms));
//   };

  var view = document.getElementById("view"); // 図と対応づけ
  var gpioAccess = await navigator.requestGPIOAccess(); // Web GPIO利用のための最初のAPI呼び出し
  var port = gpioAccess.ports.get(12); // GPIOポート番号12番を指定
  await port.export("in"); //(GPIOポート12番を)入力モードで初期化

  function detection(v){
    if(port.read() === 0){
      port.write(0);
      view.style.backgroundColor = "black";
    }else{
      port.write(1);
      view.style.backgroundColor = "red";
    }
  }

//   while(1){
//     var val = await port.read();//12番の状態を読み込む
//     val ^= 0;
//     detection(val);
//     await sleep(100);
//   }

  port.onchange = (val)=>{ // port(GPIO12番)の状態を取得する,引数はval...無名・アロー関数
    detection(val);
  }

})();
</script>
</body>
</html>
narita2 commented 6 years ago

4

<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com

Copyright (c) 2018 by anonymous (http://jsbin.com/qomekonare/5/edit)

Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<style id="jsbin-css">
#view{
  width:60px;
  height:60px;
  border-radius:30px;
  background-color:black;
}
</style>
</head>
<body>
  <div id="view"></div>
  <script src="https://mz4u.net/libs/gc2/polyfill.js"></script>
<script id="jsbin-javascript">
(async ()=>{
  var view = document.getElementById("view"); // 図と対応づけ
  var gpioAccess = await navigator.requestGPIOAccess(); // Web GPIO利用のための最初のAPI呼び出し
  var port = gpioAccess.ports.get(12); // GPIOポート番号12番を指定
  await port.export("in"); //(GPIOポート12番を)入力モードで初期化

  function detection(v){
    if(port.read() === 0){
      port.write(0);
      view.style.backgroundColor = "blue";
    }else{
      port.write(1);
      view.style.backgroundColor = "red";
    }
  }

  port.onchange = (val)=>{ // port(GPIO12番)の状態を取得する,引数はval...無名・アロー関数
    val ^= 0;
    detection(val);
  }

})();
</script>
</body>
</html>
narita2 commented 6 years ago

5

<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com

Copyright (c) 2018 by anonymous (http://jsbin.com/zolisupefi/4/edit)

Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<style id="jsbin-css">
#view{
  width:60px;
  height:60px;
  border-radius:30px;
  background-color:black;
}

</style>
</head>
<body>
  <div id="view"></div>
  <script src="https://mz4u.net/libs/gc2/polyfill.js"></script>
<script id="jsbin-javascript">
(async ()=>{ //1
  var sleep = (ms)=>{ //一定時間待つ関数
    return new Promise((resolve)=>setTimeout(resolve,ms));
  }
  var view = document.getElementById("view");//図と対応づけ
  var gpioAccess = await navigator.requestGPIOAccess(); //Web GPIO利用のための最初のAPI呼び出し
  var port = gpioAccess.ports.get(12);//GPIOポート番号12番を指定
  await port.export("in");//(GPIOポート12番を)入力モードで初期化

  function detection(){
    if(port.read === 0){
      port.write(0);
      view.style.backgroundColor = "black";
    }else{
      port.write(1);
      view.style.backgroundColor = "red";
    }
  }

  while(1){
    var val = await port.read();//12番の状態を読み込む
    detection(val);
    val ^= 0;
    await sleep(100);
  }

})();

</script>
</body>
</html>
narita2 commented 6 years ago
原型完成 ・・・1

以下は修正 試作(HUMAN-SENSOR ver)

CSS含む

HTML(index.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>HUMAN-SENSOR</title>
<script src="https://mz4u.net/libs/gc2/polyfill.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;
}
#view{
width:60px;
height:60px;
border-radius:30px;
background-color:black;
margin-left:auto;
margin-right:auto;
}
</style>
</head>
<body>
<p>HUMAN-SENSOR:GPIO-12</p>
<div id="view"></div>
<img src=".png" height="400px">
</body>
</html>

JS(main.js)

'use strict';

navigator.requestGPIOAccess().then((gpioAccess)=>{ // WebGPIOのため
  var view = document.getElementById('view');
  var dPort = gpioAccess.ports.get(12);//人感センサ
  return Promise.all([ 
    dPort.export("in")
  ]).then(()=>{
      dPort.onchange = function(v){
      if(v === 1){
        view.style.backgroundColor = "red";
      }else{
        view.style.backgroundColor = "blue";
      }
    }
  });
});
narita2 commented 6 years ago
原型完成 ・・・27日

以下は修正 試作(KP-IR412 ver)

HTML (index.html)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>KP-IR412</title>
    <script src="https://mz4u.net/libs/gc2/polyfill.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;
        }
        #view{
          width:60px;
          height:60px;
          border-radius:30px;
          background-color:black;
          margin-left:auto;
          margin-right:auto;
        }
    </style>
  </head>
  <body>
    <p><a href="http://eleshop.jp/shop/g/gE3336G/" target="_blank">Human body infrared sensing element KP-IR412</a></p>
    <div id="view"></div>
    <img src=".png" height="400px">
  </body>
</html>

JS

'use strict';

navigator.requestGPIOAccess().then((gpioAccess)=>{
  var view = document.getElementById('view');
  var dPort = gpioAccess.ports.get(12);
  return Promise.all([ 
    dPort.export("in")
  ]).then(()=>{
      dPort.onchange = function(v){
      if(v === 1){
        view.style.backgroundColor = "red";
      }else{
        view.style.backgroundColor = "blue";
      }
    }
  });
});
narita2 commented 6 years ago
JavaScript 8/28(自)

Promise.allの引数を配列から変更(動作未確認)

'use strict';

navigator.requestGPIOAccess().then((gpioAccess)=>{
  var view = document.getElementById('view');
  var dPort = gpioAccess.ports.get(12);
  return Promise.all(dPort.export("in")).then(()=>{
      dPort.onchange = function(v){
      if(v === 1){
        view.style.backgroundColor = "red";
      }else{
        view.style.backgroundColor = "blue";
      }
    }
  });
});
narita2 commented 6 years ago

<正規 修正版>

HTML <正規>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
    <title>KP-IR412</title>
    <script src="https://mz4u.net/libs/gc2/polyfill.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>
    <p><a href="http://eleshop.jp/shop/g/gE3336G/" target="_blank">人体赤外線感知素子
KP-IR412</a></p>
    <!--このあたりに回路図とか、パーツURLなどが入る。他の/gc/gpio/配下のサンプル参照-->
    <img src="./schematic.png" height="400px">
    <h1><div id="sensor">---</div></h1>
  </body>
</html>

JavaScript <正規>

'use strict';

navigator.requestGPIOAccess().then((gpioAccess)=>{
  var sensor = document.getElementById('sensor');
  var dPort = gpioAccess.ports.get(12);
  return Promise.all([dPort.export("in")]).then(()=>{
      dPort.onchange = function(v){
      if(v === 1){
        sensor.innerHTML = "ON";
      }else{
        sensor.innerHTML = "OFF";
      }
    }
  });
});
narita2 commented 6 years ago

JS 微調整

document.getElementById()を代入する変数名を変更

'use strict';

navigator.requestGPIOAccess().then((gpioAccess)=>{
  var sensor = document.getElementById('sensor');
  var dPort = gpioAccess.ports.get(12);
  return Promise.all([dPort.export("in")]).then(()=>{
      dPort.onchange = function(v){
      if(v === 1){
        sensor.innerHTML = "ON";
      }else{
        sensor.innerHTML = "OFF";
      }
    }
  });
});
narita2 commented 6 years ago

git

初心者向けGithubへのPullRequest方法 (検索)

narita2 commented 6 years ago

schematic