mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Added BBB-analog-in
Added a triggered analogue input node
This commit is contained in:
parent
439ca32e09
commit
62d26c04fb
85
hardware/BBB/144-analog-in.html
Normal file
85
hardware/BBB/144-analog-in.html
Normal file
@ -0,0 +1,85 @@
|
||||
<!--
|
||||
Copyright 2014 Maxwell R Hadley
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<!-- First, the content of the edit dialog is defined. -->
|
||||
|
||||
<script type="text/x-red" data-template-name="BBB-analog-in">
|
||||
|
||||
<!-- Each of the following divs creates a field in the edit dialog. -->
|
||||
<!-- Generally, there should be an input for each property of the node. -->
|
||||
<!-- The for and id attributes identify the corresponding property -->
|
||||
<!-- (with the 'node-input-' prefix). -->
|
||||
<!-- The available icon classes are defined in Twitter Bootstrap -->
|
||||
<div class="form-row">
|
||||
<label for="node-input-pin"><i class="icon-asterisk"></i>Input pin</label>
|
||||
<select type="text" id="node-input-pin" style="width: 150px;">
|
||||
<option value="">select pin</option>
|
||||
<option value="P9_39">AIN0 (P9 pin 39)</option>
|
||||
<option value="P9_40">AIN1 (P9 pin 40)</option>
|
||||
<option value="P9_37">AIN2 (P9 pin 37)</option>
|
||||
<option value="P9_38">AIN3 (P9 pin 38)</option>
|
||||
<option value="P9_33">AIN4 (P9 pin 33)</option>
|
||||
<option value="P9_36">AIN5 (P9 pin 36)</option>
|
||||
<option value="P9_35">AIN6 (P9 pin 35)</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
|
||||
<input type="text" id="node-input-topic" placeholder="Topic">
|
||||
</div>
|
||||
|
||||
<!-- By convention, most nodes have a 'name' property. The following div -->
|
||||
<!-- provides the necessary field. -->
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
|
||||
<!-- Next, some simple help text is provided for the node. -->
|
||||
<script type="text/x-red" data-help-name="BBB-analog-in">
|
||||
<!-- data-help-name identifies the node type this help is for -->
|
||||
<!-- This content appears in the Info sidebar when a node is selected -->
|
||||
<!-- The first <p> is used as the pop-up tool tip when hovering over a -->
|
||||
<!-- node in the palette. -->
|
||||
<p>Analogue input for the Beaglebone Black. Reads an anlogue pin when triggered</p>
|
||||
<p>The output message topic is the node topic: the output message value is the
|
||||
analogue input in the range [0-1), or NaN if an read error occurs (errors are logged)</p>
|
||||
</script>
|
||||
|
||||
<!-- Finally, the node type is registered along with all of its properties -->
|
||||
<!-- The example below shows a small subset of the properties that can be set-->
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('BBB-analog-in',{
|
||||
category: 'advanced-input', // the palette category
|
||||
color:"#c6abef",
|
||||
defaults: { // defines the editable properties of the node
|
||||
name: { value:"" }, // along with default values.
|
||||
topic: { value:"", required:true },
|
||||
pin: { value:"", required:true },
|
||||
},
|
||||
inputs:1, // set the number of inputs - only 0 or 1
|
||||
outputs:1, // set the number of outputs - 0 to n
|
||||
icon: "arrow-in.png", // set the icon (held in public/icons)
|
||||
label: function() { // sets the default label contents
|
||||
return this.name || "analog-in: " + this.pin;
|
||||
},
|
||||
labelStyle: function() { // sets the class to apply to the label
|
||||
return this.name?"node_label_italic":"";
|
||||
}
|
||||
});
|
||||
</script>
|
62
hardware/BBB/144-analog-in.js
Normal file
62
hardware/BBB/144-analog-in.js
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright 2014 Maxwell R Hadley
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
// Require main module
|
||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||
|
||||
// Require bonescript
|
||||
try {
|
||||
var bs = require("bonescript");
|
||||
} catch(err) {
|
||||
require("util").log("[BBB-analog-in] Error: cannot find module 'bonescript'");
|
||||
}
|
||||
|
||||
// The main node definition - most things happen in here
|
||||
function AnalogInputNode(n) {
|
||||
// Create a RED node
|
||||
RED.nodes.createNode(this, n);
|
||||
|
||||
// Store local copies of the node configuration (as defined in the .html)
|
||||
this.topic = n.topic;
|
||||
this.pin = n.pin;
|
||||
|
||||
// Define 'node' to allow us to access 'this' from within callbacks (the 'var' is essential -
|
||||
// otherwise there is only one 'node' for all instances of AnalogInputNode!)
|
||||
var node = this;
|
||||
|
||||
// A callback function variable seems to be more reliable than a lambda ?!
|
||||
var cbFun = function (x) {
|
||||
var msg = {};
|
||||
msg.topic = node.topic;
|
||||
msg.payload = x.value;
|
||||
if (isNaN(x.value)) {
|
||||
this.log(x.err);
|
||||
}
|
||||
node.send(msg);
|
||||
};
|
||||
|
||||
// If we have a valid pin, set the input event handler to Bonescript's analogRead
|
||||
if (["P9_39", "P9_40", "P9_37", "P9_38", "P9_33", "P9_36", "P9_35"].indexOf(node.pin) >= 0) {
|
||||
node.on("input", function (msg) { bs.analogRead(node.pin, cbFun) });
|
||||
} else {
|
||||
node.error("Unconfigured input pin");
|
||||
}
|
||||
}
|
||||
|
||||
// Register the node by name. This must be called before overriding any of the Node functions.
|
||||
RED.nodes.registerType("BBB-analog-in", AnalogInputNode);
|
||||
|
||||
// AnalogInputNode.prototype.close = function () { };
|
Loading…
x
Reference in New Issue
Block a user