node-poweredup/docs/devices_colordistancesensor.js.html
Nathan Kellenicki bcc59ce81c
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Added maxpower
2020-05-24 10:15:30 -07:00

414 lines
21 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>node-poweredup Source: devices/colordistancesensor.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.simplex.css">
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top navbar-inverse">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">node-poweredup</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="topNavigation">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
<ul class="dropdown-menu ">
<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="BoostMoveHub.html">BoostMoveHub</a></li><li><a href="ColorDistanceSensor.html">ColorDistanceSensor</a></li><li><a href="ControlPlusHub.html">ControlPlusHub</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="Light.html">Light</a></li><li><a href="LPF2Hub.html">LPF2Hub</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="PUPHub.html">PUPHub</a></li><li><a href="PUPRemote.html">PUPRemote</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="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="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>
</li>
<li class="dropdown">
<a href="events.list.html" class="dropdown-toggle" data-toggle="dropdown">Events<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="AbsoluteMotor.html#event:absolute">AbsoluteMotor#event:absolute</a></li><li><a href="AbsoluteMotor.html#event:rotate">AbsoluteMotor#event:rotate</a></li><li><a href="BoostMoveHub.html#event:attach">BoostMoveHub#event:attach</a></li><li><a href="BoostMoveHub.html#event:button">BoostMoveHub#event:button</a></li><li><a href="BoostMoveHub.html#event:detach">BoostMoveHub#event:detach</a></li><li><a href="BoostMoveHub.html#event:disconnect">BoostMoveHub#event:disconnect</a></li><li><a href="ColorDistanceSensor.html#event:color">ColorDistanceSensor#event:color</a></li><li><a href="ColorDistanceSensor.html#event:colorAndDistance">ColorDistanceSensor#event:colorAndDistance</a></li><li><a href="ColorDistanceSensor.html#event:distance">ColorDistanceSensor#event:distance</a></li><li><a href="ControlPlusHub.html#event:attach">ControlPlusHub#event:attach</a></li><li><a href="ControlPlusHub.html#event:button">ControlPlusHub#event:button</a></li><li><a href="ControlPlusHub.html#event:detach">ControlPlusHub#event:detach</a></li><li><a href="ControlPlusHub.html#event:disconnect">ControlPlusHub#event:disconnect</a></li><li><a href="CurrentSensor.html#event:current">CurrentSensor#event:current</a></li><li><a href="DuploTrainBase.html#event:button">DuploTrainBase#event:button</a></li><li><a href="DuploTrainBaseColorSensor.html#event:color">DuploTrainBaseColorSensor#event:color</a></li><li><a href="DuploTrainBaseColorSensor.html#event:reflect">DuploTrainBaseColorSensor#event:reflect</a></li><li><a href="DuploTrainBaseColorSensor.html#event:rgb">DuploTrainBaseColorSensor#event:rgb</a></li><li><a href="DuploTrainBaseSpeedometer.html#event:speed">DuploTrainBaseSpeedometer#event:speed</a></li><li><a href="Hub.html#event:attach">Hub#event:attach</a></li><li><a href="Hub.html#event:button">Hub#event:button</a></li><li><a href="Hub.html#event:detach">Hub#event:detach</a></li><li><a href="Hub.html#event:disconnect">Hub#event:disconnect</a></li><li><a href="LPF2Hub.html#event:button">LPF2Hub#event:button</a></li><li><a href="MediumLinearMotor.html#event:rotate">MediumLinearMotor#event:rotate</a></li><li><a href="MotionSensor.html#event:distance">MotionSensor#event:distance</a></li><li><a href="MoveHub.html#event:button">MoveHub#event:button</a></li><li><a href="MoveHubMediumLinearMotor.html#event:rotate">MoveHubMediumLinearMotor#event:rotate</a></li><li><a href="MoveHubTiltSensor.html#event:tilt">MoveHubTiltSensor#event:tilt</a></li><li><a href="PUPHub.html#event:attach">PUPHub#event:attach</a></li><li><a href="PUPHub.html#event:button">PUPHub#event:button</a></li><li><a href="PUPHub.html#event:detach">PUPHub#event:detach</a></li><li><a href="PUPHub.html#event:disconnect">PUPHub#event:disconnect</a></li><li><a href="PUPRemote.html#event:attach">PUPRemote#event:attach</a></li><li><a href="PUPRemote.html#event:button">PUPRemote#event:button</a></li><li><a href="PUPRemote.html#event:detach">PUPRemote#event:detach</a></li><li><a href="PUPRemote.html#event:disconnect">PUPRemote#event:disconnect</a></li><li><a href="RemoteControl.html#event:button">RemoteControl#event:button</a></li><li><a href="RemoteControlButton.html#event:button">RemoteControlButton#event:button</a></li><li><a href="TachoMotor.html#event:rotate">TachoMotor#event:rotate</a></li><li><a href="TechnicColorSensor.html#event:ambient">TechnicColorSensor#event:ambient</a></li><li><a href="TechnicColorSensor.html#event:color">TechnicColorSensor#event:color</a></li><li><a href="TechnicColorSensor.html#event:reflect">TechnicColorSensor#event:reflect</a></li><li><a href="TechnicDistanceSensor.html#event:distance">TechnicDistanceSensor#event:distance</a></li><li><a href="TechnicDistanceSensor.html#event:fastDistance">TechnicDistanceSensor#event:fastDistance</a></li><li><a href="TechnicForceSensor.html#event:force">TechnicForceSensor#event:force</a></li><li><a href="TechnicForceSensor.html#event:tapped">TechnicForceSensor#event:tapped</a></li><li><a href="TechnicForceSensor.html#event:touch">TechnicForceSensor#event:touch</a></li><li><a href="TechnicLargeAngularMotor.html#event:absolute">TechnicLargeAngularMotor#event:absolute</a></li><li><a href="TechnicLargeAngularMotor.html#event:rotate">TechnicLargeAngularMotor#event:rotate</a></li><li><a href="TechnicLargeLinearMotor.html#event:absolute">TechnicLargeLinearMotor#event:absolute</a></li><li><a href="TechnicLargeLinearMotor.html#event:rotate">TechnicLargeLinearMotor#event:rotate</a></li><li><a href="TechnicMediumAngularMotor.html#event:absolute">TechnicMediumAngularMotor#event:absolute</a></li><li><a href="TechnicMediumAngularMotor.html#event:rotate">TechnicMediumAngularMotor#event:rotate</a></li><li><a href="TechnicMediumHub.html#event:button">TechnicMediumHub#event:button</a></li><li><a href="TechnicMediumHubAccelerometerSensor.html#event:accel">TechnicMediumHubAccelerometerSensor#event:accel</a></li><li><a href="TechnicMediumHubGyroSensor.html#event:gyro">TechnicMediumHubGyroSensor#event:gyro</a></li><li><a href="TechnicMediumHubTiltSensor.html#event:tilt">TechnicMediumHubTiltSensor#event:tilt</a></li><li><a href="TechnicXLargeLinearMotor.html#event:absolute">TechnicXLargeLinearMotor#event:absolute</a></li><li><a href="TechnicXLargeLinearMotor.html#event:rotate">TechnicXLargeLinearMotor#event:rotate</a></li><li><a href="TiltSensor.html#event:tilt">TiltSensor#event:tilt</a></li><li><a href="VoltageSensor.html#event:voltage">VoltageSensor#event:voltage</a></li><li><a href="WeDo2SmartHub.html#event:button">WeDo2SmartHub#event:button</a></li>
</ul>
</li>
</ul>
<div class="col-sm-3 col-md-3">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
<div class="input-group-btn">
<button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="container" id="toc-content">
<div class="row">
<div class="col-md-12">
<div id="main">
<h1 class="page-title">Source: devices/colordistancesensor.js</h1>
<section>
<article>
<pre
class="sunlight-highlight-javascript linenums">"use strict";
var __importStar = (this &amp;&amp; this.__importStar) || function (mod) {
if (mod &amp;&amp; mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const device_1 = require("./device");
const Consts = __importStar(require("../consts"));
/**
* @class ColorDistanceSensor
* @extends Device
*/
class ColorDistanceSensor extends device_1.Device {
constructor(hub, portId) {
super(hub, portId, exports.ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR);
}
receive(message) {
const mode = this._mode;
switch (mode) {
case Mode.COLOR:
if (message[this.isWeDo2SmartHub ? 2 : 4] &lt;= 10) {
const color = message[this.isWeDo2SmartHub ? 2 : 4];
/**
* Emits when a color sensor is activated.
* @event ColorDistanceSensor#color
* @type {object}
* @param {Color} color
*/
this.notify("color", { color });
}
break;
case Mode.DISTANCE:
if (this.isWeDo2SmartHub) {
break;
}
if (message[4] &lt;= 10) {
const distance = Math.floor(message[4] * 25.4) - 20;
/**
* Emits when a distance sensor is activated.
* @event ColorDistanceSensor#distance
* @type {object}
* @param {number} distance Distance, in millimeters.
*/
this.notify("distance", { distance });
}
break;
case Mode.COLOR_AND_DISTANCE:
if (this.isWeDo2SmartHub) {
break;
}
let distance = message[5];
const partial = message[7];
if (partial > 0) {
distance += 1.0 / partial;
}
distance = Math.floor(distance * 25.4) - 20;
/**
* A combined color and distance event, emits when the sensor is activated.
* @event ColorDistanceSensor#colorAndDistance
* @type {object}
* @param {Color} color
* @param {number} distance Distance, in millimeters.
*/
if (message[4] &lt;= 10) {
const color = message[4];
this.notify("colorAndDistance", { color, distance });
}
break;
}
}
/**
* Switches the IR receiver into extended channel mode. After setting this, use channels 5-8 instead of 1-4 for this receiver.
*
* NOTE: Calling this with channel 5-8 with switch off extended channel mode for this receiver.
* @method ColorDistanceSensor#setPFExtendedChannel
* @param {number} channel Channel number, between 1-8
* @returns {Promise} Resolved upon successful issuance of the command.
*/
setPFExtendedChannel(channel) {
let address = 0;
if (channel >= 4) {
channel -= 4;
address = 1;
}
const message = Buffer.alloc(2);
// Send "Extended toggle address command"
message[0] = ((channel - 1) &lt;&lt; 4) + (address &lt;&lt; 3);
message[1] = 6 &lt;&lt; 4;
return this.sendPFIRMessage(message);
}
/**
* Set the power of a Power Functions motor via IR
* @method ColorDistanceSensor#setPFPower
* @param {number} channel Channel number, between 1-4
* @param {string} output Outport port, "RED" (A) or "BLUE" (B)
* @param {number} power -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
* @returns {Promise} Resolved upon successful issuance of the command.
*/
setPFPower(channel, output, power) {
let address = 0;
if (channel > 4) {
channel -= 4;
address = 1;
}
const message = Buffer.alloc(2);
// Send "Single output mode"
message[0] = ((channel - 1) &lt;&lt; 4) + (address &lt;&lt; 3) + (output === "RED" ? 4 : 5);
message[1] = this._pfPowerToPWM(power) &lt;&lt; 4;
return this.sendPFIRMessage(message);
}
/**
* Start Power Functions motors running via IR
*
* NOTE: This command is designed for bang-bang style operation. To keep the motors running, the sensor needs to be within range of the IR receiver constantly.
* @method ColorDistanceSensor#startPFMotors
* @param {Buffer} channel Channel number, between 1-4
* @param {Buffer} powerA -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
* @param {Buffer} powerB -7 (full reverse) to 7 (full forward). 0 is stop. 8 is brake.
* @returns {Promise} Resolved upon successful issuance of the command.
*/
startPFMotors(channel, powerBlue, powerRed) {
let address = 0;
if (channel > 4) {
channel -= 4;
address = 1;
}
const message = Buffer.alloc(2);
// Send "Combo PWM mode"
message[0] = (((channel - 1) + 4 + (address &lt;&lt; 3)) &lt;&lt; 4) + this._pfPowerToPWM(powerBlue);
message[1] = this._pfPowerToPWM(powerRed) &lt;&lt; 4;
return this.sendPFIRMessage(message);
}
/**
* Send a raw Power Functions IR command
* @method ColorDistanceSensor#sendPFIRMessage
* @param {Buffer} message 2 byte payload making up a Power Functions protocol command. NOTE: Only specify nibbles 1-3, nibble 4 should be zeroed.
* @returns {Promise} Resolved upon successful issuance of the command.
*/
sendPFIRMessage(message) {
if (this.isWeDo2SmartHub) {
throw new Error("Power Functions IR is not available on the WeDo 2.0 Smart Hub");
}
else {
const payload = Buffer.alloc(2);
payload[0] = (message[0] &lt;&lt; 4) + (message[1] >> 4);
payload[1] = message[0] >> 4;
this.subscribe(Mode.PF_IR);
return this.writeDirect(0x07, payload);
}
}
/**
* Set the color of the LED on the sensor via a color value.
* @method ColorDistanceSensor#setColor
* @param {Color} color
* @returns {Promise} Resolved upon successful issuance of the command.
*/
setColor(color) {
return new Promise((resolve, reject) => {
if (color === false) {
color = 0;
}
if (this.isWeDo2SmartHub) {
throw new Error("Setting LED color is not available on the WeDo 2.0 Smart Hub");
}
else {
this.subscribe(Mode.LED);
this.writeDirect(0x05, Buffer.from([color]));
}
return resolve();
});
}
_pfPowerToPWM(power) {
return power &amp; 15;
}
}
exports.ColorDistanceSensor = ColorDistanceSensor;
var Mode;
(function (Mode) {
Mode[Mode["COLOR"] = 0] = "COLOR";
Mode[Mode["DISTANCE"] = 1] = "DISTANCE";
Mode[Mode["LED"] = 5] = "LED";
Mode[Mode["PF_IR"] = 7] = "PF_IR";
Mode[Mode["COLOR_AND_DISTANCE"] = 8] = "COLOR_AND_DISTANCE";
})(Mode = exports.Mode || (exports.Mode = {}));
exports.ModeMap = {
"color": Mode.COLOR,
"distance": Mode.DISTANCE,
"colorAndDistance": Mode.COLOR_AND_DISTANCE
};
var Output;
(function (Output) {
Output["RED"] = "RED";
Output["BLUE"] = "BLUE";
})(Output = exports.Output || (exports.Output = {}));
//# sourceMappingURL=colordistancesensor.js.map</pre>
</article>
</section>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="modal fade" id="searchResults">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Search results</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
<footer>
<span class="copyright">
node-poweredup by Nathan Kellenicki licensed under the MIT license.
</span>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>
on Sun May 24th 2020
using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
</footer>
<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/toc.js"></script>
<script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>
<script>
$( function () {
$( "[id*='$']" ).each( function () {
var $this = $( this );
$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
} );
$( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
var $this = $( this );
var example = $this.find( "code" );
exampleText = example.html();
var lang = /{@lang (.*?)}/.exec( exampleText );
if ( lang && lang[1] ) {
exampleText = exampleText.replace( lang[0], "" );
example.html( exampleText );
lang = lang[1];
} else {
var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
lang = langClassMatch ? langClassMatch[1] : "javascript";
}
if ( lang ) {
$this
.addClass( "sunlight-highlight-" + lang )
.addClass( "linenums" )
.html( example.html() );
}
} );
Sunlight.highlightAll( {
lineNumbers : true,
showMenu : true,
enableDoclinks : true
} );
$.catchAnchorLinks( {
navbarOffset: 10
} );
$( "#toc" ).toc( {
anchorName : function ( i, heading, prefix ) {
return $( heading ).attr( "id" ) || ( prefix + i );
},
selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
showAndHide : false,
smoothScrolling: true
} );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
$( '.dropdown-toggle' ).dropdown();
$( "table" ).each( function () {
var $this = $( this );
$this.addClass('table');
} );
} );
</script>
<!--Navigation and Symbol Display-->
<!--Google Analytics-->
<script type="text/javascript">
$(document).ready(function() {
SearcherDisplay.init();
});
</script>
</body>
</html>