devices/tiltsensor.js

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModeMap = exports.Mode = exports.TiltSensor = void 0;
const device_1 = require("./device");
const Consts = __importStar(require("../consts"));
/**
 * @class TiltSensor
 * @extends Device
 */
class TiltSensor extends device_1.Device {
    constructor(hub, portId) {
        super(hub, portId, exports.ModeMap, Consts.DeviceType.TILT_SENSOR);
    }
    receive(message) {
        const mode = this._mode;
        let x = 0;
        let y = 0;
        let z = 0;
        switch (mode) {
            case Mode.TILT:
                if (message.length !== (this.isWeDo2SmartHub ? 4 : 6)) {
                    // if mode of device has not changed to this._mode yet
                    break;
                }
                x = message.readInt8(this.isWeDo2SmartHub ? 2 : 4);
                y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5);
                /**
                 * Emits when a tilt sensor is activated.
                 * @event TiltSensor#tilt
                 * @type {object}
                 * @param {number} x
                 * @param {number} y
                 */
                this.notify("tilt", { x, y });
                break;
            case Mode.DIRECTION:
                const dir = message.readInt8(this.isWeDo2SmartHub ? 2 : 4);
                /**
                 * Emits when the tilt sensor direction changes.
                 * @event TiltSensor#direction
                 * @type {object}
                 * @param {TiltDirection} dir
                 */
                this.notify("direction", { dir });
                break;
            case Mode.CRASH:
                if (message.length !== (this.isWeDo2SmartHub ? 5 : 7)) {
                    // if mode of device has not changed to this._mode yet
                    break;
                }
                x = message.readUInt8(this.isWeDo2SmartHub ? 2 : 4);
                y = message.readUInt8(this.isWeDo2SmartHub ? 3 : 5);
                z = message.readUInt8(this.isWeDo2SmartHub ? 4 : 6);
                /**
                 * Emits when proper acceleration is above threshold (e.g. on impact when being thrown to the ground).
                 * @event TiltSensor#impactCount
                 * @type {object}
                 * @param {number} x
                 * @param {number} y
                 * @param {number} z
                 */
                this.notify("impactCount", { x, y, z });
                break;
            case Mode.CAL:
                if (message.length !== (this.isWeDo2SmartHub ? 5 : 7)) {
                    // if mode of device has not changed to this._mode yet
                    break;
                }
                const ax = message.readInt8(this.isWeDo2SmartHub ? 2 : 4) * Math.PI / 180;
                const ay = message.readInt8(this.isWeDo2SmartHub ? 3 : 5) * Math.PI / 180;
                const az = message.readInt8(this.isWeDo2SmartHub ? 4 : 6) * Math.PI / 180;
                x = Math.round(1000 * Math.sqrt(2) * Math.sin(ax) * Math.cos(ay) * Math.cos(az));
                y = Math.round(1000 * Math.sqrt(2) * Math.cos(ax) * Math.sin(ay) * Math.cos(az));
                z = Math.round(1000 * Math.sqrt(2) * Math.cos(ax) * Math.cos(ay) * Math.sin(az));
                /**
                 * Emits when tilt sensor detects acceleration. Measured in mG.
                 * @event TiltSensor#accel
                 * @type {object}
                 * @param {number} x
                 * @param {number} y
                 * @param {number} z
                 */
                this.notify("accel", { x, y, z });
                break;
        }
    }
}
exports.TiltSensor = TiltSensor;
var Mode;
(function (Mode) {
    Mode[Mode["TILT"] = 0] = "TILT";
    Mode[Mode["DIRECTION"] = 1] = "DIRECTION";
    Mode[Mode["CRASH"] = 2] = "CRASH";
    Mode[Mode["CAL"] = 3] = "CAL";
})(Mode || (exports.Mode = Mode = {}));
exports.ModeMap = {
    "tilt": Mode.TILT,
    "direction": Mode.DIRECTION,
    "impactCount": Mode.CRASH,
    "accel": Mode.CAL
};
//# sourceMappingURL=tiltsensor.js.map