obniz
Version:
obniz sdk for javascript
365 lines (364 loc) • 11 kB
JavaScript
"use strict";
/**
* @packageDocumentation
* @module Parts.Grove_GestureSensor
*/
Object.defineProperty(exports, "__esModule", { value: true });
class Grove_GestureSensor {
constructor() {
this.GESTURE_RIGHT = 'GESTURE_RIGHT';
this.GESTURE_LEFT = 'GESTURE_LEFT';
this.GESTURE_UP = 'GESTURE_UP';
this.GESTURE_DOWN = 'GESTURE_DOWN';
this.GESTURE_FORWARD = 'GESTURE_FORWARD';
this.GESTURE_BACKWARD = 'GESTURE_BACKWARD';
this.GESTURE_CLOCKWISE = 'GESTURE_CLOCKWISE';
this.GESTURE_COUNT_CLOCKWISE = 'GESTURE_COUNT_CLOCKWISE';
this.ic2Address = 0x73;
this.PAJ7620_ADDR_BASE = 0x00;
this.PAJ7620_REGITER_BANK_SEL = this.PAJ7620_ADDR_BASE + 0xef;
this.PAJ7620_BANK0 = 0 << 0;
this.PAJ7620_BANK1 = 1 << 0;
this.GES_RIGHT_FLAG = 1 << 0; // PAJ7620_VAL(1,0)
this.GES_LEFT_FLAG = 1 << 1; // PAJ7620_VAL(1,1)
this.GES_UP_FLAG = 1 << 2; // PAJ7620_VAL(1,2)
this.GES_DOWN_FLAG = 1 << 3; // PAJ7620_VAL(1,3)
this.GES_FORWARD_FLAG = 1 << 4; // PAJ7620_VAL(1,4)
this.GES_BACKWARD_FLAG = 1 << 5; // PAJ7620_VAL(1,5)
this.GES_CLOCKWISE_FLAG = 1 << 6; // PAJ7620_VAL(1,6)
this.GES_COUNT_CLOCKWISE_FLAG = 1 << 7; // PAJ7620_VAL(1,7)
this.GES_WAVE_FLAG = 1 << 8; // PAJ7620_VAL(1,0)
this.initRegisterArray = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
];
this.keys = ['vcc', 'gnd', 'sda', 'scl', 'i2c', 'grove'];
this.requiredKeys = [];
}
static info() {
return {
name: 'Grove_GestureSensor',
};
}
onchange(value) {
// do nothing.
}
wired(obniz) {
this.obniz = obniz;
const speed = 400000;
if (this.params.grove) {
this.i2c = this.params.grove.getI2c(speed, '5v');
}
else {
this.obniz.setVccGnd(this.params.vcc, this.params.gnd, '5v');
this.params.mode = 'master';
this.params.clock = speed;
this.params.pull = '5v';
this.i2c = this.obniz.getI2CWithConfig(this.params);
}
this.i2c.onerror = (err) => {
// console.log('Error:', err);
};
// await obniz.wait(700);
this.initWait();
}
async initWait() {
// wakeup check
await this.checkWakeUpWait();
// initRegister
await this.initRegisterWait();
while (true) {
// get data
this.i2c.write(this.ic2Address, [0x43]);
const resArray = await this.i2c.readWait(this.ic2Address, 1);
const res = resArray[0];
if (this.onchange) {
if (this.GES_RIGHT_FLAG === res) {
// console.log("GES_RIGHT_FLAG");
this.onchange(this.GESTURE_RIGHT);
}
else if (this.GES_LEFT_FLAG === res) {
// console.log("GES_LEFT_FLAG");
this.onchange(this.GESTURE_LEFT);
}
else if (this.GES_UP_FLAG === res) {
// console.log("GES_UP_FLAG");
this.onchange(this.GESTURE_UP);
}
else if (this.GES_DOWN_FLAG === res) {
// console.log("GES_DOWN_FLAG");
this.onchange(this.GESTURE_DOWN);
}
else if (this.GES_FORWARD_FLAG === res) {
// console.log("GES_FORWARD_FLAG");
this.onchange(this.GESTURE_FORWARD);
}
else if (this.GES_BACKWARD_FLAG === res) {
// console.log("GES_BACKWARD_FLAG");
this.onchange(this.GESTURE_BACKWARD);
}
else if (this.GES_CLOCKWISE_FLAG === res) {
// console.log("GES_CLOCKWISE_FLAG");
this.onchange(this.GESTURE_CLOCKWISE);
}
else if (this.GES_COUNT_CLOCKWISE_FLAG === res) {
// console.log("GES_COUNT_CLOCKWISE_FLAG");
this.onchange(this.GESTURE_COUNT_CLOCKWISE);
}
}
await this.obniz.wait(1000);
}
}
async checkWakeUpWait() {
// wakeup check
this.i2c.write(this.ic2Address, [
this.PAJ7620_REGITER_BANK_SEL,
this.PAJ7620_BANK0,
]);
let res = [0, 0];
try {
res = await this.i2c.readWait(this.ic2Address, 1);
}
catch (e) {
// console.log(e);
}
if (res[0] === 0x20) {
// console.log("wake-up finish.");
}
}
async initRegisterWait() {
// console.log("initRegister!!");
for (let i = 0; i < this.initRegisterArray.length; i++) {
this.i2c.write(this.ic2Address, this.initRegisterArray[i]);
}
// Set up gaming mode.
this.i2c.write(this.ic2Address, [
this.PAJ7620_REGITER_BANK_SEL,
this.PAJ7620_BANK1,
]);
// near mode 240 fps
this.i2c.write(this.ic2Address, [0x65, 0x12]);
this.i2c.write(this.ic2Address, [
this.PAJ7620_REGITER_BANK_SEL,
this.PAJ7620_BANK0,
]);
}
}
exports.default = Grove_GestureSensor;