Skip to content

Commit 74297a3

Browse files
committed
Add additional modes to (WeDo-)TiltSensor
1 parent e4c68e5 commit 74297a3

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

src/consts.ts

+44
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,47 @@ export enum MarioColor {
703703
BROWN = 0x6a00,
704704
CYAN = 0x4201,
705705
}
706+
707+
708+
/**
709+
* @typedef TiltDirection
710+
* @param {number} NEUTRAL 0
711+
* @param {number} BACKWARD 3
712+
* @param {number} RIGHT 5
713+
* @param {number} LEFT 7
714+
* @param {number} FORWARD 9
715+
* @param {number} UNKNOWN 10
716+
*/
717+
export enum TiltDirection {
718+
NEUTRAL = 0,
719+
BACKWARD = 3,
720+
RIGHT = 5,
721+
LEFT = 7,
722+
FORWARD = 9,
723+
UNKNOWN = 10,
724+
}
725+
726+
727+
/**
728+
* @typedef CommandFeedback
729+
* @param {number} TRANSMISSION_PENDING 0x00 waiting for previous commands to complete transmission or execution
730+
* @param {number} TRANSMISSION_BUSY 0x10 waiting for device to acknowledge reception
731+
* @param {number} TRANSMISSION_DISCARDED 0x44 interrupt command has been recieved or device disconnected
732+
* @param {number} EXECUTION_PENDING 0x20 device is waiting for previous command to complete
733+
* @param {number} EXECUTION_BUSY 0x21 device is executing the command
734+
* @param {number} EXECUTION_DISCARDED 0x24 device discarded the command e.g. due to interrupt
735+
* @param {number} EXECUTION_COMPLETED 0x22 device reported successful completion of command
736+
* @param {number} FEEDBACK_MISSING 0x66 device disconnected or failed to report feedback
737+
* @param {number} FEEDBACK_DISABLED 0x26 feedback not implemented for this command
738+
*/
739+
export enum CommandFeedback {
740+
TRANSMISSION_PENDING = 0x00,
741+
TRANSMISSION_BUSY = 0x10,
742+
TRANSMISSION_DISCARDED = 0x44,
743+
EXECUTION_PENDING = 0x20,
744+
EXECUTION_BUSY = 0x21,
745+
EXECUTION_DISCARDED = 0x24,
746+
EXECUTION_COMPLETED = 0x22,
747+
FEEDBACK_MISSING = 0x66,
748+
FEEDBACK_DISABLED = 0x26,
749+
}

src/devices/tiltsensor.ts

+66-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ export class TiltSensor extends Device {
1616

1717
public receive (message: Buffer) {
1818
const mode = this._mode;
19+
let x = 0;
20+
let y = 0;
21+
let z = 0;
1922

2023
switch (mode) {
2124
case Mode.TILT:
22-
const x = message.readInt8(this.isWeDo2SmartHub ? 2 : 4);
23-
const y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5);
25+
if (message.length !== (this.isWeDo2SmartHub ? 4 : 6)) {
26+
// if mode of device has not changed to this._mode yet
27+
break;
28+
}
29+
x = message.readInt8(this.isWeDo2SmartHub ? 2 : 4);
30+
y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5);
2431
/**
2532
* Emits when a tilt sensor is activated.
2633
* @event TiltSensor#tilt
@@ -30,15 +37,69 @@ export class TiltSensor extends Device {
3037
*/
3138
this.notify("tilt", { x, y });
3239
break;
40+
case Mode.DIRECTION:
41+
const dir = message.readInt8(this.isWeDo2SmartHub ? 2 : 4);
42+
/**
43+
* Emits when the tilt sensor direction changes.
44+
* @event TiltSensor#direction
45+
* @type {object}
46+
* @param {TiltDirection} dir
47+
*/
48+
this.notify("direction", { dir });
49+
break;
50+
case Mode.CRASH:
51+
if (message.length !== (this.isWeDo2SmartHub ? 5 : 7)) {
52+
// if mode of device has not changed to this._mode yet
53+
break;
54+
}
55+
x = message.readUInt8(this.isWeDo2SmartHub ? 2 : 4);
56+
y = message.readUInt8(this.isWeDo2SmartHub ? 3 : 5);
57+
z = message.readUInt8(this.isWeDo2SmartHub ? 4 : 6);
58+
/**
59+
* Emits when proper acceleration is above threshold (e.g. on impact when being thrown to the ground).
60+
* @event TiltSensor#impactCount
61+
* @type {object}
62+
* @param {number} x
63+
* @param {number} y
64+
* @param {number} z
65+
*/
66+
this.notify("impactCount", { x, y, z });
67+
break;
68+
case Mode.CAL:
69+
if (message.length !== (this.isWeDo2SmartHub ? 5 : 7)) {
70+
// if mode of device has not changed to this._mode yet
71+
break;
72+
}
73+
const ax = message.readInt8(this.isWeDo2SmartHub ? 2 : 4)*Math.PI/180;
74+
const ay = message.readInt8(this.isWeDo2SmartHub ? 3 : 5)*Math.PI/180;
75+
const az = message.readInt8(this.isWeDo2SmartHub ? 4 : 6)*Math.PI/180;
76+
x = Math.round(1000*Math.sqrt(2)*Math.sin(ax)*Math.cos(ay)*Math.cos(az))
77+
y = Math.round(1000*Math.sqrt(2)*Math.cos(ax)*Math.sin(ay)*Math.cos(az))
78+
z = Math.round(1000*Math.sqrt(2)*Math.cos(ax)*Math.cos(ay)*Math.sin(az))
79+
/**
80+
* Emits when tilt sensor detects acceleration. Measured in mG.
81+
* @event TiltSensor#accel
82+
* @type {object}
83+
* @param {number} x
84+
* @param {number} y
85+
* @param {number} z
86+
*/
87+
this.notify("accel", { x, y, z });
88+
break;
3389
}
3490
}
35-
3691
}
3792

3893
export enum Mode {
39-
TILT = 0x00
94+
TILT = 0x00,
95+
DIRECTION = 0x01,
96+
CRASH = 0x02,
97+
CAL = 0x03
4098
}
4199

42100
export const ModeMap: {[event: string]: number} = {
43-
"tilt": Mode.TILT
101+
"tilt": Mode.TILT,
102+
"direction": Mode.DIRECTION,
103+
"impactCount": Mode.CRASH,
104+
"accel": Mode.CAL
44105
};

0 commit comments

Comments
 (0)