@@ -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
3893export enum Mode {
39- TILT = 0x00
94+ TILT = 0x00 ,
95+ DIRECTION = 0x01 ,
96+ CRASH = 0x02 ,
97+ CAL = 0x03
4098}
4199
42100export 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