diff --git a/DOCS.md b/DOCS.md
index cba2ceb..7bf3463 100644
--- a/DOCS.md
+++ b/DOCS.md
@@ -86,6 +86,8 @@ Emits when a LPF2 Hub device is found.
* ["color" (port, color)](#LPF2Hub+event_color)
* ["tilt" (port, x, y)](#LPF2Hub+event_tilt)
* ["rotate" (port, rotation)](#LPF2Hub+event_rotate)
+ * ["attach" (port, type)](#Hub+event_attach)
+ * ["detach" (port)](#Hub+event_detach)
@@ -261,6 +263,29 @@ Emits when a rotation sensor is activated.
| port | string
|
| rotation | number
|
+
+
+### "attach" (port, type)
+Emits when a motor or sensor is attached to the Hub.
+
+**Kind**: event emitted by [LPF2Hub
](#LPF2Hub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| type | number
| A number representing one of the peripheral consts. |
+
+
+
+### "detach" (port)
+Emits when an attached motor or sensor is detached from the Hub.
+
+**Kind**: event emitted by [LPF2Hub
](#LPF2Hub)
+
+| Param | Type |
+| --- | --- |
+| port | string
|
+
## WeDo2Hub ⇐ [Hub
](#Hub)
@@ -283,6 +308,8 @@ Emits when a rotation sensor is activated.
* ["color" (port, color)](#WeDo2Hub+event_color)
* ["tilt" (port, x, y)](#WeDo2Hub+event_tilt)
* ["rotate" (port, rotation)](#WeDo2Hub+event_rotate)
+ * ["attach" (port, type)](#Hub+event_attach)
+ * ["detach" (port)](#Hub+event_detach)
@@ -457,6 +484,29 @@ Emits when a rotation sensor is activated.
| port | string
|
| rotation | number
|
+
+
+### "attach" (port, type)
+Emits when a motor or sensor is attached to the Hub.
+
+**Kind**: event emitted by [WeDo2Hub
](#WeDo2Hub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| type | number
| A number representing one of the peripheral consts. |
+
+
+
+### "detach" (port)
+Emits when an attached motor or sensor is detached from the Hub.
+
+**Kind**: event emitted by [WeDo2Hub
](#WeDo2Hub)
+
+| Param | Type |
+| --- | --- |
+| port | string
|
+
## Hub ⇐ EventEmitter
@@ -470,6 +520,8 @@ Emits when a rotation sensor is activated.
* [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise
* [.sleep(delay)](#Hub+sleep) ⇒ Promise
* [.wait(commands)](#Hub+wait) ⇒ Promise
+ * ["attach" (port, type)](#Hub+event_attach)
+ * ["detach" (port)](#Hub+event_detach)
@@ -538,3 +590,26 @@ This is a helper method to make it easier to wait for concurrent commands to com
| --- | --- | --- |
| commands | Array.<Promise.<any>>
| Array of executing commands. |
+
+
+### "attach" (port, type)
+Emits when a motor or sensor is attached to the Hub.
+
+**Kind**: event emitted by [Hub
](#Hub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| type | number
| A number representing one of the peripheral consts. |
+
+
+
+### "detach" (port)
+Emits when an attached motor or sensor is detached from the Hub.
+
+**Kind**: event emitted by [Hub
](#Hub)
+
+| Param | Type |
+| --- | --- |
+| port | string
|
+
diff --git a/README.md b/README.md
index a7c8ff9..43b218a 100644
--- a/README.md
+++ b/README.md
@@ -127,6 +127,8 @@ Emits when a LPF2 Hub device is found.
* ["color" (port, color)](#LPF2Hub+event_color)
* ["tilt" (port, x, y)](#LPF2Hub+event_tilt)
* ["rotate" (port, rotation)](#LPF2Hub+event_rotate)
+ * ["attach" (port, type)](#Hub+event_attach)
+ * ["detach" (port)](#Hub+event_detach)
@@ -302,6 +304,29 @@ Emits when a rotation sensor is activated.
| port | string
|
| rotation | number
|
+
+
+### "attach" (port, type)
+Emits when a motor or sensor is attached to the Hub.
+
+**Kind**: event emitted by [LPF2Hub
](#LPF2Hub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| type | number
| A number representing one of the peripheral consts. |
+
+
+
+### "detach" (port)
+Emits when an attached motor or sensor is detached from the Hub.
+
+**Kind**: event emitted by [LPF2Hub
](#LPF2Hub)
+
+| Param | Type |
+| --- | --- |
+| port | string
|
+
## WeDo2Hub ⇐ [Hub
](#Hub)
@@ -324,6 +349,8 @@ Emits when a rotation sensor is activated.
* ["color" (port, color)](#WeDo2Hub+event_color)
* ["tilt" (port, x, y)](#WeDo2Hub+event_tilt)
* ["rotate" (port, rotation)](#WeDo2Hub+event_rotate)
+ * ["attach" (port, type)](#Hub+event_attach)
+ * ["detach" (port)](#Hub+event_detach)
@@ -498,6 +525,29 @@ Emits when a rotation sensor is activated.
| port | string
|
| rotation | number
|
+
+
+### "attach" (port, type)
+Emits when a motor or sensor is attached to the Hub.
+
+**Kind**: event emitted by [WeDo2Hub
](#WeDo2Hub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| type | number
| A number representing one of the peripheral consts. |
+
+
+
+### "detach" (port)
+Emits when an attached motor or sensor is detached from the Hub.
+
+**Kind**: event emitted by [WeDo2Hub
](#WeDo2Hub)
+
+| Param | Type |
+| --- | --- |
+| port | string
|
+
## Hub ⇐ EventEmitter
@@ -511,6 +561,8 @@ Emits when a rotation sensor is activated.
* [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise
* [.sleep(delay)](#Hub+sleep) ⇒ Promise
* [.wait(commands)](#Hub+wait) ⇒ Promise
+ * ["attach" (port, type)](#Hub+event_attach)
+ * ["detach" (port)](#Hub+event_detach)
@@ -579,3 +631,26 @@ This is a helper method to make it easier to wait for concurrent commands to com
| --- | --- | --- |
| commands | Array.<Promise.<any>>
| Array of executing commands. |
+
+
+### "attach" (port, type)
+Emits when a motor or sensor is attached to the Hub.
+
+**Kind**: event emitted by [Hub
](#Hub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| type | number
| A number representing one of the peripheral consts. |
+
+
+
+### "detach" (port)
+Emits when an attached motor or sensor is detached from the Hub.
+
+**Kind**: event emitted by [Hub
](#Hub)
+
+| Param | Type |
+| --- | --- |
+| port | string
|
+
diff --git a/hub.ts b/hub.ts
index 770dd75..6ecee48 100644
--- a/hub.ts
+++ b/hub.ts
@@ -217,10 +217,23 @@ export class Hub extends EventEmitter {
port.type = type;
if (this.autoSubscribe) {
this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
+ /**
+ * Emits when a motor or sensor is attached to the Hub.
+ * @event Hub#attach
+ * @param {string} port
+ * @param {number} type A number representing one of the peripheral consts.
+ */
+ this.emit("attach", port.id, type);
}
} else {
port.type = Consts.Devices.UNKNOWN;
debug(`Port ${port.id} disconnected`);
+ /**
+ * Emits when an attached motor or sensor is detached from the Hub.
+ * @event Hub#detach
+ * @param {string} port
+ */
+ this.emit("detach", port.id);
}
}
diff --git a/lpf2.ts b/lpf2.ts
index 8493bb4..cad88c4 100644
--- a/lpf2.ts
+++ b/lpf2.ts
@@ -64,6 +64,8 @@ export class LPF2 extends EventEmitter {
return;
}
+ console.log(peripheral);
+
peripheral.removeAllListeners();
noble.stopScanning();
noble.startScanning();