From 4cc92c6d37cbc923a9c9afc5f195170cbb5f4d29 Mon Sep 17 00:00:00 2001 From: bene Date: Sun, 9 Jan 2022 15:59:15 +0100 Subject: [PATCH] add impact count mode to tiltsensor --- src/devices/technicmediumhubtiltsensor.ts | 70 ++++++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/src/devices/technicmediumhubtiltsensor.ts b/src/devices/technicmediumhubtiltsensor.ts index b421a72..e7e6233 100644 --- a/src/devices/technicmediumhubtiltsensor.ts +++ b/src/devices/technicmediumhubtiltsensor.ts @@ -10,6 +10,9 @@ import * as Consts from "../consts"; */ export class TechnicMediumHubTiltSensor extends Device { + protected _impactThreshold: number = 10; // guess of default value + protected _impactHoldoff: number = 10; // guess of default value + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR); } @@ -21,26 +24,87 @@ export class TechnicMediumHubTiltSensor extends Device { case Mode.TILT: /** * Emits when a tilt sensor is activated. + * * @event TechnicMediumHubTiltSensor#tilt * @type {object} * @param {number} x * @param {number} y * @param {number} z */ - const z = -message.readInt16LE(4); + let z = -message.readInt16LE(4); const y = message.readInt16LE(6); const x = message.readInt16LE(8); 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. + */ + public setImpactCount (count: number) { + 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. + */ + public setImpactThreshold (threshold: number) { + 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. + */ + public setImpactHoldoff (holdoff: number) { + this._impactHoldoff = holdoff; + return new Promise((resolve) => { + this.writeDirect(0x02, Buffer.from([this._impactThreshold, this._impactHoldoff])); + return resolve(); + }); + } } export enum Mode { - TILT = 0x00 + TILT = 0x00, + IMPACT_COUNT = 0x01 } export const ModeMap: {[event: string]: number} = { - "tilt": Mode.TILT + "tilt": Mode.TILT, + "impactCount": Mode.IMPACT_COUNT };