@@ -16,11 +16,18 @@ export class TiltSensor extends Device {
16
16
17
17
public receive ( message : Buffer ) {
18
18
const mode = this . _mode ;
19
+ let x = 0 ;
20
+ let y = 0 ;
21
+ let z = 0 ;
19
22
20
23
switch ( mode ) {
21
24
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 ) ;
24
31
/**
25
32
* Emits when a tilt sensor is activated.
26
33
* @event TiltSensor#tilt
@@ -30,15 +37,69 @@ export class TiltSensor extends Device {
30
37
*/
31
38
this . notify ( "tilt" , { x, y } ) ;
32
39
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 ;
33
89
}
34
90
}
35
-
36
91
}
37
92
38
93
export enum Mode {
39
- TILT = 0x00
94
+ TILT = 0x00 ,
95
+ DIRECTION = 0x01 ,
96
+ CRASH = 0x02 ,
97
+ CAL = 0x03
40
98
}
41
99
42
100
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
44
105
} ;
0 commit comments