Skip to content

Commit

Permalink
Add missing modes to (WeDo-)TiltSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Debenben committed Oct 8, 2022
1 parent e4c68e5 commit b58e608
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 6 deletions.
44 changes: 44 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,47 @@ export enum MarioColor {
BROWN = 0x6a00,
CYAN = 0x4201,
}


/**
* @typedef TiltDirection
* @param {number} NEUTRAL 0
* @param {number} BACKWARD 3
* @param {number} RIGHT 5
* @param {number} LEFT 7
* @param {number} FORWARD 9
* @param {number} UNKNOWN 10
*/
export enum TiltDirection {
NEUTRAL = 0,
BACKWARD = 3,
RIGHT = 5,
LEFT = 7,
FORWARD = 9,
UNKNOWN = 10,
}


/**
* @typedef CommandFeedback
* @param {number} TRANSMISSION_PENDING 0x00 waiting for previous commands to complete transmission or execution
* @param {number} TRANSMISSION_BUSY 0x10 waiting for device to acknowledge reception
* @param {number} TRANSMISSION_DISCARDED 0x44 interrupt command has been recieved or device disconnected
* @param {number} EXECUTION_PENDING 0x20 device is waiting for previous command to complete
* @param {number} EXECUTION_BUSY 0x21 device is executing the command
* @param {number} EXECUTION_DISCARDED 0x24 device discarded the command e.g. due to interrupt
* @param {number} EXECUTION_COMPLETED 0x22 device reported successful completion of command
* @param {number} FEEDBACK_MISSING 0x66 device disconnected or failed to report feedback
* @param {number} FEEDBACK_DISABLED 0x26 feedback not implemented for this command
*/
export enum CommandFeedback {
TRANSMISSION_PENDING = 0x00,
TRANSMISSION_BUSY = 0x10,
TRANSMISSION_DISCARDED = 0x44,
EXECUTION_PENDING = 0x20,
EXECUTION_BUSY = 0x21,
EXECUTION_DISCARDED = 0x24,
EXECUTION_COMPLETED = 0x22,
FEEDBACK_MISSING = 0x66,
FEEDBACK_DISABLED = 0x26,
}
72 changes: 66 additions & 6 deletions src/devices/tiltsensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ export class TiltSensor extends Device {

public receive (message: Buffer) {
const mode = this._mode;
let x = 0;
let y = 0;
let z = 0;

switch (mode) {
case Mode.TILT:
const x = message.readInt8(this.isWeDo2SmartHub ? 2 : 4);
const y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5);
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
Expand All @@ -30,15 +37,68 @@ export class TiltSensor extends Device {
*/
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;
}
}

}

export enum Mode {
TILT = 0x00
TILT = 0x00,
DIRECTION = 0x01,
CRASH = 0x02,
CAL = 0x03
}

export const ModeMap: {[event: string]: number} = {
"tilt": Mode.TILT
"tilt": Mode.TILT,
"direction": Mode.DIRECTION,
"impactCount": Mode.CRASH,
"accel": Mode.CAL
};

0 comments on commit b58e608

Please sign in to comment.