240 lines
14 KiB
HTML
240 lines
14 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: devices/device.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: devices/device.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Device = void 0;
|
|
const events_1 = require("events");
|
|
const Consts = __importStar(require("../consts"));
|
|
/**
|
|
* @class Device
|
|
* @extends EventEmitter
|
|
*/
|
|
class Device extends events_1.EventEmitter {
|
|
constructor(hub, portId, modeMap = {}, type = Consts.DeviceType.UNKNOWN) {
|
|
super();
|
|
this.autoSubscribe = true;
|
|
this.values = {};
|
|
this._busy = false;
|
|
this._finishedCallbacks = [];
|
|
this._connected = true;
|
|
this._modeMap = {};
|
|
this._isVirtualPort = false;
|
|
this._eventTimer = null;
|
|
this._hub = hub;
|
|
this._portId = portId;
|
|
this._type = type;
|
|
this._modeMap = modeMap;
|
|
this._isWeDo2SmartHub = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB);
|
|
this._isVirtualPort = this.hub.isPortVirtual(portId);
|
|
const eventAttachListener = (event) => {
|
|
if (event === "detach") {
|
|
return;
|
|
}
|
|
if (this.autoSubscribe) {
|
|
if (this._modeMap[event] !== undefined) {
|
|
this.subscribe(this._modeMap[event]);
|
|
}
|
|
}
|
|
};
|
|
const deviceDetachListener = (device) => {
|
|
if (device.portId === this.portId) {
|
|
this._connected = false;
|
|
this.hub.removeListener("detach", deviceDetachListener);
|
|
this.emit("detach");
|
|
}
|
|
};
|
|
for (const event in this._modeMap) {
|
|
if (this.hub.listenerCount(event) > 0) {
|
|
eventAttachListener(event);
|
|
}
|
|
}
|
|
this.hub.on("newListener", eventAttachListener);
|
|
this.on("newListener", eventAttachListener);
|
|
this.hub.on("detach", deviceDetachListener);
|
|
}
|
|
/**
|
|
* @readonly
|
|
* @property {boolean} connected Check if the device is still attached.
|
|
*/
|
|
get connected() {
|
|
return this._connected;
|
|
}
|
|
/**
|
|
* @readonly
|
|
* @property {Hub} hub The Hub the device is attached to.
|
|
*/
|
|
get hub() {
|
|
return this._hub;
|
|
}
|
|
get portId() {
|
|
return this._portId;
|
|
}
|
|
/**
|
|
* @readonly
|
|
* @property {string} portName The port the device is attached to.
|
|
*/
|
|
get portName() {
|
|
return this.hub.getPortNameForPortId(this.portId);
|
|
}
|
|
/**
|
|
* @readonly
|
|
* @property {number} type The type of the device
|
|
*/
|
|
get type() {
|
|
return this._type;
|
|
}
|
|
get typeName() {
|
|
return Consts.DeviceTypeNames[this.type];
|
|
}
|
|
/**
|
|
* @readonly
|
|
* @property {number} mode The mode the device is currently in
|
|
*/
|
|
get mode() {
|
|
return this._mode;
|
|
}
|
|
get isWeDo2SmartHub() {
|
|
return this._isWeDo2SmartHub;
|
|
}
|
|
/**
|
|
* @readonly
|
|
* @property {boolean} isVirtualPort Is this device attached to a virtual port (ie. a combined device)
|
|
*/
|
|
get isVirtualPort() {
|
|
return this._isVirtualPort;
|
|
}
|
|
writeDirect(mode, data) {
|
|
if (this.isWeDo2SmartHub) {
|
|
return this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
|
}
|
|
else {
|
|
return this.send(Buffer.concat([Buffer.from([0x81, this.portId, 0x11, 0x51, mode]), data]), Consts.BLECharacteristic.LPF2_ALL);
|
|
}
|
|
}
|
|
send(data, characteristic = Consts.BLECharacteristic.LPF2_ALL) {
|
|
this._ensureConnected();
|
|
return this.hub.send(data, characteristic);
|
|
}
|
|
subscribe(mode) {
|
|
this._ensureConnected();
|
|
if (mode !== this._mode) {
|
|
this._mode = mode;
|
|
this.hub.subscribe(this.portId, this.type, mode);
|
|
}
|
|
}
|
|
unsubscribe(mode) {
|
|
this._ensureConnected();
|
|
}
|
|
receive(message) {
|
|
this.notify("receive", { message });
|
|
}
|
|
notify(event, values) {
|
|
this.values[event] = values;
|
|
this.emit(event, values);
|
|
if (this.hub.listenerCount(event) > 0) {
|
|
this.hub.emit(event, this, values);
|
|
}
|
|
}
|
|
requestUpdate() {
|
|
this.send(Buffer.from([0x21, this.portId, 0x00]));
|
|
}
|
|
finish(message) {
|
|
if ((message & 0x10) === 0x10)
|
|
return; // "busy/full"
|
|
this._busy = (message & 0x01) === 0x01;
|
|
while (this._finishedCallbacks.length > Number(this._busy)) {
|
|
const callback = this._finishedCallbacks.shift();
|
|
if (callback) {
|
|
callback();
|
|
}
|
|
}
|
|
}
|
|
setEventTimer(timer) {
|
|
this._eventTimer = timer;
|
|
}
|
|
cancelEventTimer() {
|
|
if (this._eventTimer) {
|
|
clearTimeout(this._eventTimer);
|
|
this._eventTimer = null;
|
|
}
|
|
}
|
|
_ensureConnected() {
|
|
if (!this.connected) {
|
|
throw new Error("Device is not connected");
|
|
}
|
|
}
|
|
}
|
|
exports.Device = Device;
|
|
//# sourceMappingURL=device.js.map</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AbsoluteMotor.html">AbsoluteMotor</a></li><li><a href="BaseHub.html">BaseHub</a></li><li><a href="BasicMotor.html">BasicMotor</a></li><li><a href="ColorDistanceSensor.html">ColorDistanceSensor</a></li><li><a href="CurrentSensor.html">CurrentSensor</a></li><li><a href="Device.html">Device</a></li><li><a href="DuploTrainBase.html">DuploTrainBase</a></li><li><a href="DuploTrainBaseColorSensor.html">DuploTrainBaseColorSensor</a></li><li><a href="DuploTrainBaseMotor.html">DuploTrainBaseMotor</a></li><li><a href="DuploTrainBaseSpeaker.html">DuploTrainBaseSpeaker</a></li><li><a href="DuploTraniBaseSpeedometer.html">DuploTraniBaseSpeedometer</a></li><li><a href="Hub.html">Hub</a></li><li><a href="HubLED.html">HubLED</a></li><li><a href="LPF2Hub.html">LPF2Hub</a></li><li><a href="Light.html">Light</a></li><li><a href="Mario.html">Mario</a></li><li><a href="MarioAccelerometer.html">MarioAccelerometer</a></li><li><a href="MarioBarcodeSensor.html">MarioBarcodeSensor</a></li><li><a href="MarioPantsSensor.html">MarioPantsSensor</a></li><li><a href="MediumLinearMotor.html">MediumLinearMotor</a></li><li><a href="MotionSensor.html">MotionSensor</a></li><li><a href="MoveHub.html">MoveHub</a></li><li><a href="MoveHubMediumLinearMotor.html">MoveHubMediumLinearMotor</a></li><li><a href="MoveHubTiltSensor.html">MoveHubTiltSensor</a></li><li><a href="PiezoBuzzer.html">PiezoBuzzer</a></li><li><a href="PoweredUP.html">PoweredUP</a></li><li><a href="RemoteControl.html">RemoteControl</a></li><li><a href="RemoteControlButton.html">RemoteControlButton</a></li><li><a href="SimpleMediumLinearMotor.html">SimpleMediumLinearMotor</a></li><li><a href="TachoMotor.html">TachoMotor</a></li><li><a href="Technic3x3ColorLightMatrix.html">Technic3x3ColorLightMatrix</a></li><li><a href="TechnicColorSensor.html">TechnicColorSensor</a></li><li><a href="TechnicDistanceSensor.html">TechnicDistanceSensor</a></li><li><a href="TechnicForceSensor.html">TechnicForceSensor</a></li><li><a href="TechnicLargeAngularMotor.html">TechnicLargeAngularMotor</a></li><li><a href="TechnicLargeLinearMotor.html">TechnicLargeLinearMotor</a></li><li><a href="TechnicMediumAngularMotor.html">TechnicMediumAngularMotor</a></li><li><a href="TechnicMediumHub.html">TechnicMediumHub</a></li><li><a href="TechnicMediumHubAccelerometerSensor.html">TechnicMediumHubAccelerometerSensor</a></li><li><a href="TechnicMediumHubGyroSensor.html">TechnicMediumHubGyroSensor</a></li><li><a href="TechnicMediumHubTiltSensor.html">TechnicMediumHubTiltSensor</a></li><li><a href="TechnicSmallAngularMotor.html">TechnicSmallAngularMotor</a></li><li><a href="TechnicXLargeLinearMotor.html">TechnicXLargeLinearMotor</a></li><li><a href="TiltSensor.html">TiltSensor</a></li><li><a href="TrainMotor.html">TrainMotor</a></li><li><a href="VoltageSensor.html">VoltageSensor</a></li><li><a href="WeDo2SmartHub.html">WeDo2SmartHub</a></li></ul><h3>Events</h3><ul><li><a href="AbsoluteMotor.html#event:absolute">absolute</a></li><li><a href="AbsoluteMotor.html#event:rotate">rotate</a></li><li><a href="ColorDistanceSensor.html#event:ambient">ambient</a></li><li><a href="ColorDistanceSensor.html#event:color">color</a></li><li><a href="ColorDistanceSensor.html#event:colorAndDistance">colorAndDistance</a></li><li><a href="ColorDistanceSensor.html#event:distance">distance</a></li><li><a href="ColorDistanceSensor.html#event:distanceCount">distanceCount</a></li><li><a href="ColorDistanceSensor.html#event:reflect">reflect</a></li><li><a href="ColorDistanceSensor.html#event:rgbIntensity">rgbIntensity</a></li><li><a href="CurrentSensor.html#event:current">current</a></li><li><a href="DuploTrainBase.html#event:button">button</a></li><li><a href="DuploTrainBaseColorSensor.html#event:color">color</a></li><li><a href="DuploTrainBaseColorSensor.html#event:intensity">intensity</a></li><li><a href="DuploTrainBaseColorSensor.html#event:reflect">reflect</a></li><li><a href="DuploTrainBaseColorSensor.html#event:rgb">rgb</a></li><li><a href="DuploTrainBaseSpeedometer.html#event:speed">speed</a></li><li><a href="Hub.html#event:attach">attach</a></li><li><a href="Hub.html#event:button">button</a></li><li><a href="Hub.html#event:detach">detach</a></li><li><a href="Hub.html#event:disconnect">disconnect</a></li><li><a href="LPF2Hub.html#event:button">button</a></li><li><a href="Mario.html#event:button">button</a></li><li><a href="MarioAccelerometer.html#event:accel">accel</a></li><li><a href="MarioAccelerometer.html#event:gest">gest</a></li><li><a href="MarioBarcodeSensor.html#event:barcode">barcode</a></li><li><a href="MarioBarcodeSensor.html#event:rgb">rgb</a></li><li><a href="MarioPantsSensor.html#event:pants">pants</a></li><li><a href="MediumLinearMotor.html#event:rotate">rotate</a></li><li><a href="MotionSensor.html#event:distance">distance</a></li><li><a href="MoveHub.html#event:button">button</a></li><li><a href="MoveHubMediumLinearMotor.html#event:rotate">rotate</a></li><li><a href="MoveHubTiltSensor.html#event:tilt">tilt</a></li><li><a href="PoweredUP.html#event:discover">discover</a></li><li><a href="RemoteControl.html#event:button">button</a></li><li><a href="RemoteControlButton.html#event:remoteButton">remoteButton</a></li><li><a href="TachoMotor.html#event:rotate">rotate</a></li><li><a href="TechnicColorSensor.html#event:ambient">ambient</a></li><li><a href="TechnicColorSensor.html#event:color">color</a></li><li><a href="TechnicColorSensor.html#event:reflect">reflect</a></li><li><a href="TechnicDistanceSensor.html#event:distance">distance</a></li><li><a href="TechnicDistanceSensor.html#event:fastDistance">fastDistance</a></li><li><a href="TechnicForceSensor.html#event:force">force</a></li><li><a href="TechnicForceSensor.html#event:tapped">tapped</a></li><li><a href="TechnicForceSensor.html#event:touch">touch</a></li><li><a href="TechnicLargeAngularMotor.html#event:absolute">absolute</a></li><li><a href="TechnicLargeAngularMotor.html#event:rotate">rotate</a></li><li><a href="TechnicLargeLinearMotor.html#event:absolute">absolute</a></li><li><a href="TechnicLargeLinearMotor.html#event:rotate">rotate</a></li><li><a href="TechnicMediumAngularMotor.html#event:absolute">absolute</a></li><li><a href="TechnicMediumAngularMotor.html#event:rotate">rotate</a></li><li><a href="TechnicMediumHub.html#event:button">button</a></li><li><a href="TechnicMediumHubAccelerometerSensor.html#event:accel">accel</a></li><li><a href="TechnicMediumHubGyroSensor.html#event:gyro">gyro</a></li><li><a href="TechnicMediumHubTiltSensor.html#event:impactCount">impactCount</a></li><li><a href="TechnicMediumHubTiltSensor.html#event:tilt">tilt</a></li><li><a href="TechnicSmallAngularMotor.html#event:absolute">absolute</a></li><li><a href="TechnicSmallAngularMotor.html#event:rotate">rotate</a></li><li><a href="TechnicXLargeLinearMotor.html#event:absolute">absolute</a></li><li><a href="TechnicXLargeLinearMotor.html#event:rotate">rotate</a></li><li><a href="TiltSensor.html#event:tilt">tilt</a></li><li><a href="VoltageSensor.html#event:voltage">voltage</a></li><li><a href="WeDo2SmartHub.html#event:button">button</a></li></ul><h3><a href="global.html">Global</a></h3>
|
|
</nav>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Thu Apr 25 2024 17:10:11 GMT-0700 (Pacific Daylight Time)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|