From 3f48077c4f9291da5937b1d17d02c3a20426e8c1 Mon Sep 17 00:00:00 2001 From: tomaszni Date: Fri, 16 Nov 2018 12:33:53 +0100 Subject: [PATCH] Extend LPF2Hub events with "colorAndDistance" - emitting both color and distance from Boost sensor at the same time, just like the sensor information is delivered. --- DOCS.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpf2hub.ts | 17 +++++++++++++++-- 3 files changed, 127 insertions(+), 2 deletions(-) diff --git a/DOCS.md b/DOCS.md index 8a4121d..4637f11 100644 --- a/DOCS.md +++ b/DOCS.md @@ -425,6 +425,7 @@ Emits when an attached motor or sensor is detached from the Hub. * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["tilt" (port, x, y)](#LPF2Hub+event_tilt) * ["rotate" (port, rotation)](#LPF2Hub+event_rotate) * ["attach" (port, type)](#Hub+event_attach) @@ -698,6 +699,19 @@ Emits when a color sensor is activated. | port | string | | | color | number | A number representing one of the LED color consts. | + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| color | number | A number representing one of the LED color consts. | +| distance | number | Distance, in millimeters. | + ### "tilt" (port, x, y) @@ -776,6 +790,7 @@ Emits when an attached motor or sensor is detached from the Hub. * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["tilt" (port, x, y)](#LPF2Hub+event_tilt) * ["attach" (port, type)](#Hub+event_attach) * ["detach" (port)](#Hub+event_detach) @@ -1034,6 +1049,19 @@ Emits when a color sensor is activated. | port | string | | | color | number | A number representing one of the LED color consts. | + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [PUPHub](#PUPHub) + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| color | number | A number representing one of the LED color consts. | +| distance | number | Distance, in millimeters. | + ### "tilt" (port, x, y) @@ -1095,6 +1123,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise * ["button" (button, state)](#LPF2Hub+event_button) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) @@ -1285,6 +1314,19 @@ Emits when a button is pressed. | button | string | | | state | number | A number representing one of the button state consts. | + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [PUPRemote](#PUPRemote) + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| color | number | A number representing one of the LED color consts. | +| distance | number | Distance, in millimeters. | + ## DuploTrainBase ⇐ LPF2Hub @@ -1311,6 +1353,7 @@ Emits when a button is pressed. * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["speed" (port, speed)](#LPF2Hub+event_speed) @@ -1542,6 +1585,19 @@ Emits when a color sensor is activated. | port | string | | | color | number | A number representing one of the LED color consts. | + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| color | number | A number representing one of the LED color consts. | +| distance | number | Distance, in millimeters. | + ### "speed" (port, speed) diff --git a/README.md b/README.md index 0226272..fc1b123 100644 --- a/README.md +++ b/README.md @@ -508,6 +508,7 @@ Emits when an attached motor or sensor is detached from the Hub. * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["tilt" (port, x, y)](#LPF2Hub+event_tilt) * ["rotate" (port, rotation)](#LPF2Hub+event_rotate) * ["attach" (port, type)](#Hub+event_attach) @@ -781,6 +782,19 @@ Emits when a color sensor is activated. | port | string | | | color | number | A number representing one of the LED color consts. | + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| color | number | A number representing one of the LED color consts. | +| distance | number | Distance, in millimeters. | + ### "tilt" (port, x, y) @@ -859,6 +873,7 @@ Emits when an attached motor or sensor is detached from the Hub. * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["tilt" (port, x, y)](#LPF2Hub+event_tilt) * ["attach" (port, type)](#Hub+event_attach) * ["detach" (port)](#Hub+event_detach) @@ -1117,6 +1132,19 @@ Emits when a color sensor is activated. | port | string | | | color | number | A number representing one of the LED color consts. | + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [PUPHub](#PUPHub) + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| color | number | A number representing one of the LED color consts. | +| distance | number | Distance, in millimeters. | + ### "tilt" (port, x, y) @@ -1178,6 +1206,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise * ["button" (button, state)](#LPF2Hub+event_button) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) @@ -1368,6 +1397,19 @@ Emits when a button is pressed. | button | string | | | state | number | A number representing one of the button state consts. | + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [PUPRemote](#PUPRemote) + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| color | number | A number representing one of the LED color consts. | +| distance | number | Distance, in millimeters. | + ## DuploTrainBase ⇐ LPF2Hub @@ -1394,6 +1436,7 @@ Emits when a button is pressed. * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["speed" (port, speed)](#LPF2Hub+event_speed) @@ -1625,6 +1668,19 @@ Emits when a color sensor is activated. | port | string | | | color | number | A number representing one of the LED color consts. | + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| color | number | A number representing one of the LED color consts. | +| distance | number | Distance, in millimeters. | + ### "speed" (port, speed) diff --git a/lpf2hub.ts b/lpf2hub.ts index c241e63..9b16044 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -299,10 +299,23 @@ export class LPF2Hub extends Hub { const partial = data[7]; if (partial > 0) { - distance += 1 / partial; + distance += 1.0 / partial; } - this.emit("distance", port.id, Math.floor(distance * 25.4) - 20); + distance = Math.floor(distance * 25.4) - 20; + + this.emit("distance", port.id, distance); + + /** + * A combined color and distance event, emits when the sensor is activated. + * @event LPF2Hub#colorAndDistance + * @param {string} port + * @param {number} color A number representing one of the LED color consts. + * @param {number} distance Distance, in millimeters. + */ + if (data[4] <= 10) { + this.emit("colorAndDistance", port.id, data[4], distance); + } break; } case Consts.Devices.WEDO2_TILT: