Source: devices/technicmediumhubtiltsensor.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.TechnicMediumHubTiltSensor = void 0;
const device_1 = require("./device");
const Consts = __importStar(require("../consts"));
/**
 * @class TechnicMediumHubTiltSensor
 * @extends Device
 */
class TechnicMediumHubTiltSensor extends device_1.Device {
    constructor(hub, portId) {
        super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR);
        this._impactThreshold = 10; // guess of default value
        this._impactHoldoff = 10; // guess of default value
    }
    receive(message) {
        const mode = this._mode;
        switch (mode) {
            case Mode.TILT:
                /**
                 * Emits when a tilt sensor is activated.
                 *
                 * @event TechnicMediumHubTiltSensor#tilt
                 * @type {object}
                 * @param {number} x
                 * @param {number} y
                 * @param {number} z
                 */
                let z = -message.readInt16LE(4);
                const y = message.readInt16LE(6);
                const x = message.readInt16LE(8);
                // workaround for calibration problem or bug in technicMediumHub firmware 1.1.00.0000
                if (y === 90 || y === -90) {
                    z = Math.sign(y) * (z + 180);
                    if (z > 180)
                        z -= 360;
                    if (z < -180)
                        z += 360;
                }
                this.notify("tilt", { x, y, z });
                break;
            case Mode.IMPACT_COUNT:
                if (message.length !== 8) {
                    // if mode of device has not changed to this._mode yet
                    break;
                }
                const count = message.readUInt32LE(4);
                /**
                 * Emits when proper acceleration is above threshold (e.g. on impact when being thrown to the ground).
                 * @event TechnicMediumHubTiltSensor#impactCount
                 * @type {object}
                 * @param {number} number of impact events.
                 */
                this.notify("tiltCount", { count });
                break;
        }
    }
    /**
     * Set the impact count value.
     * @method TechnicMediumHubTiltSensor#setImpactCount
     * @param {count} impact count between 0 and 2^32
     * @returns {Promise} Resolved upon successful issuance of the command.
     */
    setImpactCount(count) {
        return new Promise((resolve) => {
            const payload = Buffer.alloc(4);
            payload.writeUInt32LE(count % 2 ** 32);
            // no need to subscribe, can be set in different mode
            this.writeDirect(0x01, payload);
            return resolve();
        });
    }
    /**
     * Set the impact threshold.
     * @method TechnicMediumHubTiltSensor#setImpactThreshold
     * @param {threshold} value between 1 and 127
     * @returns {Promise} Resolved upon successful issuance of the command.
     */
    setImpactThreshold(threshold) {
        this._impactThreshold = threshold;
        return new Promise((resolve) => {
            this.writeDirect(0x02, Buffer.from([this._impactThreshold, this._impactHoldoff]));
            return resolve();
        });
    }
    /**
     * Set the impact holdoff time.
     * @method TechnicMediumHubTiltSensor#setImpactHoldoff
     * @param {holdoff} value between 1 and 127
     * @returns {Promise} Resolved upon successful issuance of the command.
     */
    setImpactHoldoff(holdoff) {
        this._impactHoldoff = holdoff;
        return new Promise((resolve) => {
            this.writeDirect(0x02, Buffer.from([this._impactThreshold, this._impactHoldoff]));
            return resolve();
        });
    }
}
exports.TechnicMediumHubTiltSensor = TechnicMediumHubTiltSensor;
var Mode;
(function (Mode) {
    Mode[Mode["TILT"] = 0] = "TILT";
    Mode[Mode["IMPACT_COUNT"] = 1] = "IMPACT_COUNT";
})(Mode || (exports.Mode = Mode = {}));
exports.ModeMap = {
    "tilt": Mode.TILT,
    "impactCount": Mode.IMPACT_COUNT
};
//# sourceMappingURL=technicmediumhubtiltsensor.js.map