node-poweredup 7.1.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/README.md +40 -40
  2. package/dist/browser/poweredup.js +1 -1
  3. package/dist/browser/poweredup.js.map +1 -1
  4. package/dist/node/consts.d.ts +41 -24
  5. package/dist/node/consts.js +41 -24
  6. package/dist/node/consts.js.map +1 -1
  7. package/dist/node/devices/colordistancesensor.d.ts +11 -0
  8. package/dist/node/devices/colordistancesensor.js +93 -1
  9. package/dist/node/devices/colordistancesensor.js.map +1 -1
  10. package/dist/node/devices/device.js +2 -0
  11. package/dist/node/devices/device.js.map +1 -1
  12. package/dist/node/devices/technicmediumhubtiltsensor.d.ts +25 -1
  13. package/dist/node/devices/technicmediumhubtiltsensor.js +70 -2
  14. package/dist/node/devices/technicmediumhubtiltsensor.js.map +1 -1
  15. package/dist/node/hubs/lpf2hub.js +5 -4
  16. package/dist/node/hubs/lpf2hub.js.map +1 -1
  17. package/dist/node/poweredup-node.js +24 -1
  18. package/dist/node/poweredup-node.js.map +1 -1
  19. package/docs/AbsoluteMotor.html +1 -1
  20. package/docs/BaseHub.html +1 -1
  21. package/docs/BasicMotor.html +1 -1
  22. package/docs/ColorDistanceSensor.html +841 -53
  23. package/docs/CurrentSensor.html +1 -1
  24. package/docs/Device.html +1 -1
  25. package/docs/DuploTrainBase.html +1 -1
  26. package/docs/DuploTrainBaseColorSensor.html +1 -1
  27. package/docs/DuploTrainBaseMotor.html +1 -1
  28. package/docs/DuploTrainBaseSpeaker.html +1 -1
  29. package/docs/DuploTraniBaseSpeedometer.html +1 -1
  30. package/docs/Hub.html +1 -1
  31. package/docs/HubLED.html +1 -1
  32. package/docs/LPF2Hub.html +1 -1
  33. package/docs/Light.html +1 -1
  34. package/docs/Mario.html +1 -1
  35. package/docs/MarioAccelerometer.html +1 -1
  36. package/docs/MarioBarcodeSensor.html +1 -1
  37. package/docs/MarioPantsSensor.html +1 -1
  38. package/docs/MediumLinearMotor.html +1 -1
  39. package/docs/MotionSensor.html +1 -1
  40. package/docs/MoveHub.html +1 -1
  41. package/docs/MoveHubMediumLinearMotor.html +1 -1
  42. package/docs/MoveHubTiltSensor.html +1 -1
  43. package/docs/PiezoBuzzer.html +1 -1
  44. package/docs/PoweredUP.html +23 -23
  45. package/docs/RemoteControl.html +1 -1
  46. package/docs/RemoteControlButton.html +1 -1
  47. package/docs/SimpleMediumLinearMotor.html +1 -1
  48. package/docs/TachoMotor.html +1 -1
  49. package/docs/Technic3x3ColorLightMatrix.html +1 -1
  50. package/docs/TechnicColorSensor.html +1 -1
  51. package/docs/TechnicDistanceSensor.html +1 -1
  52. package/docs/TechnicForceSensor.html +1 -1
  53. package/docs/TechnicLargeAngularMotor.html +1 -1
  54. package/docs/TechnicLargeLinearMotor.html +1 -1
  55. package/docs/TechnicMediumAngularMotor.html +1 -1
  56. package/docs/TechnicMediumHub.html +1 -1
  57. package/docs/TechnicMediumHubAccelerometerSensor.html +1 -1
  58. package/docs/TechnicMediumHubGyroSensor.html +1 -1
  59. package/docs/TechnicMediumHubTiltSensor.html +590 -2
  60. package/docs/TechnicSmallAngularMotor.html +1 -1
  61. package/docs/TechnicXLargeLinearMotor.html +1 -1
  62. package/docs/TiltSensor.html +1 -1
  63. package/docs/TrainMotor.html +1 -1
  64. package/docs/VoltageSensor.html +1 -1
  65. package/docs/WeDo2SmartHub.html +1 -1
  66. package/docs/consts.js.html +42 -25
  67. package/docs/devices_absolutemotor.js.html +1 -1
  68. package/docs/devices_basicmotor.js.html +1 -1
  69. package/docs/devices_colordistancesensor.js.html +94 -2
  70. package/docs/devices_currentsensor.js.html +1 -1
  71. package/docs/devices_device.js.html +3 -1
  72. package/docs/devices_duplotrainbasecolorsensor.js.html +1 -1
  73. package/docs/devices_duplotrainbasemotor.js.html +1 -1
  74. package/docs/devices_duplotrainbasespeaker.js.html +1 -1
  75. package/docs/devices_duplotrainbasespeedometer.js.html +1 -1
  76. package/docs/devices_hubled.js.html +1 -1
  77. package/docs/devices_light.js.html +1 -1
  78. package/docs/devices_marioaccelerometer.js.html +1 -1
  79. package/docs/devices_mariobarcodesensor.js.html +1 -1
  80. package/docs/devices_mariopantssensor.js.html +1 -1
  81. package/docs/devices_mediumlinearmotor.js.html +1 -1
  82. package/docs/devices_motionsensor.js.html +1 -1
  83. package/docs/devices_movehubmediumlinearmotor.js.html +1 -1
  84. package/docs/devices_movehubtiltsensor.js.html +1 -1
  85. package/docs/devices_piezobuzzer.js.html +1 -1
  86. package/docs/devices_remotecontrolbutton.js.html +1 -1
  87. package/docs/devices_simplemediumlinearmotor.js.html +1 -1
  88. package/docs/devices_tachomotor.js.html +1 -1
  89. package/docs/devices_technic3x3colorlightmatrix.js.html +1 -1
  90. package/docs/devices_techniccolorsensor.js.html +1 -1
  91. package/docs/devices_technicdistancesensor.js.html +1 -1
  92. package/docs/devices_technicforcesensor.js.html +1 -1
  93. package/docs/devices_techniclargeangularmotor.js.html +1 -1
  94. package/docs/devices_techniclargelinearmotor.js.html +1 -1
  95. package/docs/devices_technicmediumangularmotor.js.html +1 -1
  96. package/docs/devices_technicmediumhubaccelerometersensor.js.html +1 -1
  97. package/docs/devices_technicmediumhubgyrosensor.js.html +1 -1
  98. package/docs/devices_technicmediumhubtiltsensor.js.html +71 -3
  99. package/docs/devices_technicsmallangularmotor.js.html +1 -1
  100. package/docs/devices_technicxlargelinearmotor.js.html +1 -1
  101. package/docs/devices_tiltsensor.js.html +1 -1
  102. package/docs/devices_trainmotor.js.html +1 -1
  103. package/docs/devices_voltagesensor.js.html +1 -1
  104. package/docs/global.html +541 -48
  105. package/docs/hubs_basehub.js.html +1 -1
  106. package/docs/hubs_duplotrainbase.js.html +1 -1
  107. package/docs/hubs_hub.js.html +1 -1
  108. package/docs/hubs_lpf2hub.js.html +6 -5
  109. package/docs/hubs_mario.js.html +1 -1
  110. package/docs/hubs_movehub.js.html +1 -1
  111. package/docs/hubs_remotecontrol.js.html +1 -1
  112. package/docs/hubs_technicmediumhub.js.html +1 -1
  113. package/docs/hubs_technicsmallhub.js.html +1 -1
  114. package/docs/hubs_wedo2smarthub.js.html +1 -1
  115. package/docs/index.html +35 -35
  116. package/docs/poweredup-browser.js.html +1 -1
  117. package/docs/poweredup-node.js.html +25 -2
  118. package/package.json +12 -12
  119. package/src/consts.ts +41 -24
  120. package/src/devices/colordistancesensor.ts +96 -2
  121. package/src/devices/device.ts +1 -0
  122. package/src/devices/technicmediumhubtiltsensor.ts +75 -3
  123. package/src/hubs/lpf2hub.ts +5 -6
  124. package/src/poweredup-node.ts +4 -1
@@ -38,7 +38,7 @@ export class ColorDistanceSensor extends Device {
38
38
  break;
39
39
  }
40
40
  if (message[4] <= 10) {
41
- let distance = Math.floor(message[4] * 25.4);
41
+ let distance = Math.floor(message[4] * 25.4) - 20;
42
42
 
43
43
  if (distance < 0) {
44
44
  distance = 0;
@@ -54,6 +54,74 @@ export class ColorDistanceSensor extends Device {
54
54
  }
55
55
  break;
56
56
 
57
+ case Mode.DISTANCE_COUNT:
58
+ if (this.isWeDo2SmartHub) {
59
+ break;
60
+ }
61
+ if (message.length !== 8) {
62
+ // if mode of device has not changed to this._mode yet
63
+ break;
64
+ }
65
+ const count = message.readUInt32LE(4);
66
+ /**
67
+ * Emits when distance is reduced to less than 10 centimeters.
68
+ * @event ColorDistanceSensor#distanceCount
69
+ * @type {object}
70
+ * @param {number} number of distance events.
71
+ */
72
+ this.notify("distanceCount", { count });
73
+ break;
74
+
75
+ case Mode.REFLECT:
76
+ if (this.isWeDo2SmartHub) {
77
+ break;
78
+ }
79
+ const reflect = message[4];
80
+ /**
81
+ * Event measuring reflection change, emits when the sensor is activated.
82
+ * @event ColorDistanceSensor#reflect
83
+ * @type {object}
84
+ * @param {number} percentage from 0 to 100.
85
+ */
86
+ this.notify("reflect", { reflect });
87
+ break;
88
+
89
+ case Mode.AMBIENT:
90
+ if (this.isWeDo2SmartHub) {
91
+ break;
92
+ }
93
+ const ambient = message[4];
94
+ /**
95
+ * Event measuring abient light change, emits when the sensor is activated.
96
+ * @event ColorDistanceSensor#ambient
97
+ * @type {object}
98
+ * @param {number} percentage from 0 to 100.
99
+ */
100
+ this.notify("ambient", { ambient });
101
+ break;
102
+
103
+ case Mode.RGB_I:
104
+ if (this.isWeDo2SmartHub) {
105
+ break;
106
+ }
107
+ if (message.length !== 10) {
108
+ // if mode of device has not changed to this._mode yet
109
+ break;
110
+ }
111
+ const red = message.readUInt16LE(4);
112
+ const green = message.readUInt16LE(6);
113
+ const blue = message.readUInt16LE(8);
114
+ /**
115
+ * Emits when a color sensor is activated.
116
+ * @event ColorDistanceSensor#rgbIntensity
117
+ * @type {object}
118
+ * @param {number} red
119
+ * @param {number} green
120
+ * @param {number} blue
121
+ */
122
+ this.notify("rgbIntensity", { red, green, blue });
123
+ break;
124
+
57
125
  case Mode.COLOR_AND_DISTANCE:
58
126
  if (this.isWeDo2SmartHub) {
59
127
  break;
@@ -193,18 +261,40 @@ export class ColorDistanceSensor extends Device {
193
261
  });
194
262
  }
195
263
 
264
+ /**
265
+ * Set the distance count value.
266
+ * @method ColorDistanceSensor#setDistanceCount
267
+ * @param {count} distance count between 0 and 2^32
268
+ * @returns {Promise} Resolved upon successful issuance of the command.
269
+ */
270
+ public setDistanceCount (count: number) {
271
+ return new Promise<void>((resolve) => {
272
+ if (this.isWeDo2SmartHub) {
273
+ throw new Error("Setting distance count is not available on the WeDo 2.0 Smart Hub");
274
+ } else {
275
+ const payload = Buffer.alloc(4);
276
+ payload.writeUInt32LE(count % 2**32);
277
+ // no need to subscribe, can be set in different mode
278
+ this.writeDirect(0x02, payload);
279
+ }
280
+ return resolve();
281
+ });
282
+ }
196
283
 
197
284
  private _pfPowerToPWM (power: number) {
198
285
  return power & 15;
199
286
  }
200
287
 
201
-
202
288
  }
203
289
 
204
290
  export enum Mode {
205
291
  COLOR = 0x00,
206
292
  DISTANCE = 0x01,
293
+ DISTANCE_COUNT = 0x02,
294
+ REFLECT = 0x03,
295
+ AMBIENT = 0x04,
207
296
  LED = 0x05,
297
+ RGB_I = 0x06,
208
298
  PF_IR = 0x07,
209
299
  COLOR_AND_DISTANCE = 0x08
210
300
  }
@@ -212,6 +302,10 @@ export enum Mode {
212
302
  export const ModeMap: {[event: string]: number} = {
213
303
  "color": Mode.COLOR,
214
304
  "distance": Mode.DISTANCE,
305
+ "distanceCount": Mode.DISTANCE_COUNT,
306
+ "reflect": Mode.REFLECT,
307
+ "ambient": Mode.AMBIENT,
308
+ "rgbIntensity": Mode.RGB_I,
215
309
  "colorAndDistance": Mode.COLOR_AND_DISTANCE
216
310
  };
217
311
 
@@ -168,6 +168,7 @@ export class Device extends EventEmitter {
168
168
  }
169
169
 
170
170
  public finish (message: number) {
171
+ if((message & 0x10) === 0x10) return; // "busy/full"
171
172
  this._busy = (message & 0x01) === 0x01;
172
173
  while(this._finishedCallbacks.length > Number(this._busy)) {
173
174
  const callback = this._finishedCallbacks.shift();
@@ -10,6 +10,9 @@ import * as Consts from "../consts";
10
10
  */
11
11
  export class TechnicMediumHubTiltSensor extends Device {
12
12
 
13
+ protected _impactThreshold: number = 10; // guess of default value
14
+ protected _impactHoldoff: number = 10; // guess of default value
15
+
13
16
  constructor (hub: IDeviceInterface, portId: number) {
14
17
  super(hub, portId, ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR);
15
18
  }
@@ -21,26 +24,95 @@ export class TechnicMediumHubTiltSensor extends Device {
21
24
  case Mode.TILT:
22
25
  /**
23
26
  * Emits when a tilt sensor is activated.
27
+ *
24
28
  * @event TechnicMediumHubTiltSensor#tilt
25
29
  * @type {object}
26
30
  * @param {number} x
27
31
  * @param {number} y
28
32
  * @param {number} z
29
33
  */
30
- const z = -message.readInt16LE(4);
34
+ let z = -message.readInt16LE(4);
31
35
  const y = message.readInt16LE(6);
32
36
  const x = message.readInt16LE(8);
37
+
38
+ // workaround for calibration problem or bug in technicMediumHub firmware 1.1.00.0000
39
+ if(y === 90 || y === -90) {
40
+ z = Math.sign(y)*(z + 180);
41
+ if(z > 180) z -= 360;
42
+ if(z < -180) z += 360;
43
+ }
44
+
33
45
  this.notify("tilt", { x, y, z });
34
46
  break;
47
+
48
+ case Mode.IMPACT_COUNT:
49
+ if (message.length !== 8) {
50
+ // if mode of device has not changed to this._mode yet
51
+ break;
52
+ }
53
+ const count = message.readUInt32LE(4);
54
+ /**
55
+ * Emits when proper acceleration is above threshold (e.g. on impact when being thrown to the ground).
56
+ * @event TechnicMediumHubTiltSensor#impactCount
57
+ * @type {object}
58
+ * @param {number} number of impact events.
59
+ */
60
+ this.notify("tiltCount", { count });
61
+ break;
35
62
  }
36
63
  }
37
64
 
65
+ /**
66
+ * Set the impact count value.
67
+ * @method TechnicMediumHubTiltSensor#setImpactCount
68
+ * @param {count} impact count between 0 and 2^32
69
+ * @returns {Promise} Resolved upon successful issuance of the command.
70
+ */
71
+ public setImpactCount (count: number) {
72
+ return new Promise<void>((resolve) => {
73
+ const payload = Buffer.alloc(4);
74
+ payload.writeUInt32LE(count % 2**32);
75
+ // no need to subscribe, can be set in different mode
76
+ this.writeDirect(0x01, payload);
77
+ return resolve();
78
+ });
79
+ }
80
+
81
+ /**
82
+ * Set the impact threshold.
83
+ * @method TechnicMediumHubTiltSensor#setImpactThreshold
84
+ * @param {threshold} value between 1 and 127
85
+ * @returns {Promise} Resolved upon successful issuance of the command.
86
+ */
87
+ public setImpactThreshold (threshold: number) {
88
+ this._impactThreshold = threshold;
89
+ return new Promise<void>((resolve) => {
90
+ this.writeDirect(0x02, Buffer.from([this._impactThreshold, this._impactHoldoff]));
91
+ return resolve();
92
+ });
93
+ }
94
+
95
+ /**
96
+ * Set the impact holdoff time.
97
+ * @method TechnicMediumHubTiltSensor#setImpactHoldoff
98
+ * @param {holdoff} value between 1 and 127
99
+ * @returns {Promise} Resolved upon successful issuance of the command.
100
+ */
101
+ public setImpactHoldoff (holdoff: number) {
102
+ this._impactHoldoff = holdoff;
103
+ return new Promise<void>((resolve) => {
104
+ this.writeDirect(0x02, Buffer.from([this._impactThreshold, this._impactHoldoff]));
105
+ return resolve();
106
+ });
107
+ }
38
108
  }
39
109
 
40
110
  export enum Mode {
41
- TILT = 0x00
111
+ TILT = 0x00,
112
+ IMPACT_COUNT = 0x01
42
113
  }
43
114
 
44
115
  export const ModeMap: {[event: string]: number} = {
45
- "tilt": Mode.TILT
116
+ "tilt": Mode.TILT,
117
+ "impactCount": Mode.IMPACT_COUNT
46
118
  };
@@ -348,14 +348,13 @@ export class LPF2Hub extends BaseHub {
348
348
 
349
349
 
350
350
  private _parsePortAction (message: Buffer) {
351
+ for (let offset = 3; offset < message.length; offset += 2) {
352
+ const device = this._getDeviceByPortId(message[offset]);
351
353
 
352
- const portId = message[3];
353
- const device = this._getDeviceByPortId(portId);
354
-
355
- if (device) {
356
- device.finish(message[4]);
354
+ if (device) {
355
+ device.finish(message[offset+1]);
356
+ }
357
357
  }
358
-
359
358
  }
360
359
 
361
360
 
@@ -25,7 +25,10 @@ let wantScan = false;
25
25
  let discoveryEventAttached = false;
26
26
 
27
27
  const startScanning = () => {
28
- noble.startScanning();
28
+ noble.startScanning([
29
+ Consts.BLEService.LPF2_HUB,
30
+ Consts.BLEService.WEDO2_SMART_HUB
31
+ ]);
29
32
  };
30
33
 
31
34
  noble.on("stateChange", (state: string) => {