mbedJSで加速度センサーテスト2

<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<script src="/rom/m/mbedJS.all-min.js"></script>
<script type="text/javascript">
var ax=0,ay=0,az=0;
function start()
{
  var UINT14_MAX=16383;
  //Callback
  var stat=0;
  var mcu=new mbedJS.Mcu(location.host,
  {
    onNew:function()
    {
      var i2c=new mbedJS.I2C(mcu, [mbedJS.PinName.p9, mbedJS.PinName.p10],
      {
        onNew:function()
        {
          i2c.frequency(100000);
        },
        onFrequency:function()
        {
          stat=0;
          i2c.write(0x3A, [0x2A, 0x01], false);
        },
        onWrite:function(v)
        {
          //console.log("onWrite:", stat, v);
          switch(stat){
            case 0:
              i2c.write(0x3A, [0x01], true);
              stat++;
            break;
            case 1:
              i2c.read(0x3A, 2, false);
              stat++;
            break;
            case 3:
              i2c.read(0x3A, 2, false);
              stat++;
            break;
            case 5:
              i2c.read(0x3A, 2, false);
              stat++;
            break;
            default:
              i2c.stop();
            break;
          }
        },
        onRead:function(v)
        {
          //console.log("onRead:", stat, v);
          switch(stat){
            case 2:
              ax = (v.data[0] << 6) | (v.data[1] >> 2);
              if (ax > UINT14_MAX/2) ax -= UINT14_MAX;
              i2c.write(0x3A, [0x03], true);
              stat++;
            break;
            case 4:
              ay = (v.data[0] << 6) | (v.data[1] >> 2);
              if (ay > UINT14_MAX/2) ay -= UINT14_MAX;
              i2c.write(0x3A, [0x05], true);
              stat++;
            break;
            case 6:
              az = (v.data[0] << 6) | (v.data[1] >> 2);
              if (az > UINT14_MAX/2) az -= UINT14_MAX;
              data.innerHTML = "ax = " + (ax / 4096) + "<br />"
                             + "ay = " + (ay / 4096) + "<br />"
                             + "az = " + (az / 4096);
              
              i2c.write(0x3A, [0x01], true);
              stat=1;
            break;
            default:
              i2c.stop();
            break;
          }
        },
        onStop:function()
        {
          console.log("onStop:", stat);
          mcu.close();
        }
      });
    },
    onClose:function(){
    },
    onError:function(){
      alert("Error!");
    }
  });
}
</script>
</head>
<body onload="start()">
  <h1 id="title">accel</h1>
  <div id="data"></div>
  <br />
    <!-- three.jsを読み込む -->
    <script src="/local/three.js"></script>
    <script>
      var scene = new THREE.Scene();
      var camera = new THREE.PerspectiveCamera(75, 600 / 400, 1, 1000);
      camera.position.set(0, 0, 15);
      /*
      camera.position.set(0, 10, 15);
      camera.rotation.x = -Math.PI / 5;
      */
 
      if (window.WebGLRenderingContext) {
        renderer = new THREE.WebGLRenderer();
      } else {
        renderer = new THREE.CanvasRenderer();
      }
      renderer.setSize(320, 200); // Canvasのサイズ設定
      document.body.appendChild(renderer.domElement);
 
      var directionalLight = new THREE.DirectionalLight('#ffffff', 1);
      directionalLight.position.set(0, 7, 10);
      scene.add(directionalLight); // シーンに追加
 
      var geometry = new THREE.CubeGeometry(10, 10, 10);
      var material = new THREE.MeshPhongMaterial({color: '#dd3b6f'});
      cube = new THREE.Mesh(geometry, material);
      cube.position.set(0, 0, 0);
      scene.add(cube);
 
      function render() {
        requestAnimationFrame(render);
 
        //cube.rotation.x += 0.01; // 追加
        //cube.rotation.y += 0.01; // 追加
        
        //cube.rotation.x = ax / 4000;
        //cube.rotation.y = ay / 4000;
        //cube.rotation.z = az / 4000;
        
        cube.rotation.set(
          Math.PI * (-ay / 8192),
          0,
          Math.PI * (-ax / 8192)
          );
 
        renderer.render(scene, camera);
      }
      render();
    </script>
</body>
</html>


Report

Please log in to post comments.