obniz
Version:
obniz sdk for javascript
100 lines (69 loc) • 2.61 kB
Markdown
x,y,z 3 dimention accelaration sensor.
This is used for determin direction and angle of a device.

Connect pins to an obniz and specify io.
name | type | required | default | description
--- | --- | --- | --- | ---
x | `number(obniz io)` | yes | | value of x
y | `number(obniz io)` | yes | | value of y
z | `number(obniz io)` | yes | | value of z
vcc | `number(obniz io)` | | | Power supply. 5v
gnd | `number(obniz io)` | | | Power supply. gnd
self_test | `number(obniz io)` | | | high for enter test mode
enable | `number(obniz io)` | | | low for disable device.
```javascript
// Javascript Example
var sensor = obniz.wired("KXR94-2050", { vcc:0, gnd:1, x:2, y:3, z:4, enable:5, self_test:6 });
sensor.onChange = function(values){
console.log("x:" + values.x);
console.log("y:" + values.y);
console.log("z:" + values.z);
}
```
Specifying a callback function for one of value changes of x,y,z.
The value is regarding gravity. 1 measn 9.8m^2. The value will be -2<= and <= +2.
```javascript
// Javascript Example
var sensor = obniz.wired("KXR94-2050", { vcc:0, gnd:1, x:2, y:3, z:4, enable:5, self_test:6 });
sensor.onChange = function(values){
console.log("x:" + values.x);
console.log("y:" + values.y);
console.log("z:" + values.z);
}
```
Specifying a callback function for value change.
This is useful when you only want to watch one of them.
Getting a current three acceleration value.
This function not contact to an obniz. It return last notified value from an obniz.
Notice: You should insert a wait() in infinity loop.
```javascript
// Javascript Example
var sensor = obniz.wired("KXR94-2050", { vcc:0, gnd:1, x:2, y:3, z:4, enable:5, self_test:6 });
while (true) {
let values = sensor.get();
console.log("x:" + values.x);
console.log("y:" + values.y);
console.log("z:" + values.z);
await obniz.wait(30);
}
```
Getting a current three acceleration value.
This function will contact to an obniz to retrive current value.
```javascript
// Javascript Example
var sensor = obniz.wired("KXR94-2050", { vcc:0, gnd:1, x:2, y:3, z:4, enable:5, self_test:6 });
while (true) {
let values = await sensor.getWait();
console.log("x:" + values.x);
console.log("y:" + values.y);
console.log("z:" + values.z);
}
```