2014-02-03 21:06:41 +01:00
|
|
|
<!--
|
|
|
|
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.
|
|
|
|
-->
|
|
|
|
|
2014-02-08 14:06:47 +01:00
|
|
|
<!-- Define the edit dialog -->
|
|
|
|
<script type="text/x-red" data-template-name="analog-in">
|
2014-02-03 21:06:41 +01:00
|
|
|
<div class="form-row">
|
2014-02-08 14:06:47 +01:00
|
|
|
<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>
|
2014-02-03 21:06:41 +01:00
|
|
|
</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>
|
2014-02-08 23:36:23 +01:00
|
|
|
<div class="form-row">
|
|
|
|
Input Scaling
|
|
|
|
</div>
|
|
|
|
<div class="form-row">
|
2014-02-12 23:27:08 +01:00
|
|
|
<div id="node-input-breakpoint-container-div" style="border-radius: 5px; height: 132px; padding: 5px; border: 1px solid #ccc; overflow-y:scroll;">
|
2014-02-09 11:52:56 +01:00
|
|
|
<ol id="node-input-breakpoint-container" style=" list-style-type:none; margin: 0;">
|
|
|
|
</ol>
|
2014-02-08 23:36:23 +01:00
|
|
|
</div>
|
2014-02-12 23:27:08 +01:00
|
|
|
</div>
|
|
|
|
<div class="form-row">
|
2014-02-08 23:36:23 +01:00
|
|
|
<a href="#" class="btn btn-mini" id="node-input-add-breakpoint" style="margin-top: 4px;"><i class="icon-plus"></i> Add Breakpoint</a>
|
2014-02-12 23:27:08 +01:00
|
|
|
<span style="float:right; margin-top:4px">
|
|
|
|
<input type="checkbox" id="node-input-averaging" style="display:inline-block; width:auto; vertical-align:top;"> Averaging
|
|
|
|
</span>
|
2014-02-08 23:36:23 +01:00
|
|
|
</div>
|
2014-02-09 11:52:56 +01:00
|
|
|
<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>
|
2014-02-03 21:06:41 +01:00
|
|
|
</script>
|
|
|
|
|
2014-02-08 14:06:47 +01:00
|
|
|
<!-- Add help text -->
|
|
|
|
<script type="text/x-red" data-help-name="analog-in">
|
|
|
|
<p>
|
2014-02-12 23:27:08 +01:00
|
|
|
Analogue input for the Beaglebone Black. Reads an analogue pin when triggered
|
2014-02-08 14:06:47 +01:00
|
|
|
</p>
|
|
|
|
<p>The output message topic is the node topic: the output message value is the
|
2014-02-12 23:27:08 +01:00
|
|
|
scaled analogue input or NaN if a read error occurs.
|
2014-02-09 11:52:56 +01:00
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
Simple linear scaling is defined by setting the output values required for input
|
2014-02-12 23:27:08 +01:00
|
|
|
values of 0 and 1. You can apply more complicated scaling, e.g. for sensor linearisation,
|
2014-02-09 11:52:56 +01:00
|
|
|
by defining breakpoints at intermediate input values, with the desired output for
|
|
|
|
each. Intermediate values are linearly interpolated.
|
2014-02-08 14:06:47 +01:00
|
|
|
</p>
|
2014-02-12 23:27:08 +01:00
|
|
|
<p>
|
|
|
|
To reduce the effect of noise, enable averaging. This will read the input pin
|
|
|
|
voltage ten times in rapid succession for each input message and output the mean value.
|
|
|
|
</p>
|
2014-02-03 21:06:41 +01:00
|
|
|
</script>
|
|
|
|
|
2014-02-08 14:06:47 +01:00
|
|
|
<!-- Register the node -->
|
2014-02-03 21:06:41 +01:00
|
|
|
<script type="text/javascript">
|
2014-02-08 14:06:47 +01:00
|
|
|
RED.nodes.registerType('analog-in', {
|
|
|
|
category: 'advanced-input', // the palette category
|
|
|
|
color:"#c6dbef",
|
|
|
|
defaults: { // defines the editable properties of the node
|
|
|
|
name: { value:"" }, // along with default values.
|
2014-02-03 21:06:41 +01:00
|
|
|
topic: { value:"", required:true },
|
2014-02-08 14:06:47 +01:00
|
|
|
pin: { value:"", required:true },
|
2014-02-12 23:27:08 +01:00
|
|
|
breakpoints: { value:[{input:0.0, output:0.0, mutable:false}, {input:1.0, output:1.0, mutable:false}] },
|
|
|
|
averaging: { value:false, required:true }
|
2014-02-03 21:06:41 +01:00
|
|
|
},
|
2014-02-08 14:06:47 +01:00
|
|
|
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" : "";
|
2014-02-08 23:36:23 +01:00
|
|
|
},
|
|
|
|
oneditprepare: function () {
|
2014-02-09 11:52:56 +01:00
|
|
|
function generateBreakpoint(breakpoint, insert) {
|
2014-02-12 23:27:08 +01:00
|
|
|
var container = $('<li/>', {style:"margin:0; padding:4px; padding-left 10px;"});
|
2014-02-09 11:52:56 +01:00
|
|
|
var row = $('<div/>').appendTo(container);
|
|
|
|
var breakpointField = $('<span/>').appendTo(row);
|
2014-02-08 23:36:23 +01:00
|
|
|
var inputValueField = $('<input/>',
|
2014-02-09 11:52:56 +01:00
|
|
|
{disabled:"", class:"node-input-breakpoint-input-value", type:"text", style:"margin-left:5px; margin-right:2px; width:36%;"}).appendTo(breakpointField);
|
|
|
|
if (breakpoint.mutable) {
|
|
|
|
inputValueField.removeAttr("disabled");
|
|
|
|
}
|
|
|
|
breakpointField.append(" => ");
|
|
|
|
var outputValueField = $('<input/>',
|
|
|
|
{class:"node-input-breakpoint-output-value", type:"text", style:"margin-left:0px; width:36%;"}).appendTo(breakpointField);
|
|
|
|
var finalSpan = $('<span/>', {style:"float:right; margin-top:3px; margin-right:10px;"}).appendTo(row);
|
|
|
|
var mutableFlag = $('<span/>', {class:"node-input-breakpoint-mutable", style:"display:hide;"}).appendTo(row);
|
|
|
|
if (breakpoint.mutable) {
|
|
|
|
mutableFlag.attr("mutable", "true");
|
|
|
|
var deleteButton = $('<a/>', {href:"#", class:"btn btn-mini", style:"margin-left: 5px;"}).appendTo(finalSpan);
|
|
|
|
$('<i/>', {class:"icon-remove"}).appendTo(deleteButton);
|
2014-02-08 23:36:23 +01:00
|
|
|
|
2014-02-09 11:52:56 +01:00
|
|
|
deleteButton.click(function() {
|
|
|
|
container.css({"background":"#fee"});
|
|
|
|
container.fadeOut(300, function() {
|
|
|
|
$(this).remove();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
mutableFlag.attr("mutable", "false");
|
|
|
|
}
|
|
|
|
if (insert === true) {
|
|
|
|
var last = $("#node-input-breakpoint-container").children().last();
|
|
|
|
var prev = last.prev();
|
2014-02-12 23:27:08 +01:00
|
|
|
inputValueField.val((Number(last.find(".node-input-breakpoint-input-value").val()) +
|
|
|
|
Number(prev.find(".node-input-breakpoint-input-value").val()))/2);
|
|
|
|
outputValueField.val((Number(last.find(".node-input-breakpoint-output-value").val()) +
|
|
|
|
Number(prev.find(".node-input-breakpoint-output-value").val()))/2);
|
2014-02-09 11:52:56 +01:00
|
|
|
last.before(container);
|
|
|
|
} else {
|
|
|
|
inputValueField.val(breakpoint.input);
|
|
|
|
outputValueField.val(breakpoint.output);
|
|
|
|
$("#node-input-breakpoint-container").append(container);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#node-input-add-breakpoint").click(function () {
|
|
|
|
generateBreakpoint({input:0, output:0, mutable:true}, true);
|
|
|
|
$("#node-input-breakpoint-container-div").scrollTop($("#node-input-breakpoint-container-div").get(0).scrollHeight);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var i = 0; i < this.breakpoints.length; i++) {
|
|
|
|
var breakpoint = this.breakpoints[i];
|
|
|
|
generateBreakpoint(breakpoint, false);
|
|
|
|
}
|
|
|
|
|
2014-02-12 23:27:08 +01:00
|
|
|
// Handle resizing the Input Scaling div when the dialog is resized - this isn't quite right!
|
2014-02-09 11:52:56 +01:00
|
|
|
function switchDialogResize(ev, ui) {
|
2014-02-12 23:27:08 +01:00
|
|
|
$("#node-input-breakpoint-container-div").css("height", (ui.size.height - 299) + "px");
|
2014-02-09 11:52:56 +01:00
|
|
|
};
|
2014-02-08 23:36:23 +01:00
|
|
|
|
2014-02-09 11:52:56 +01:00
|
|
|
$("#dialog").on("dialogresize", switchDialogResize);
|
|
|
|
$("#dialog").one("dialogopen", function (ev) {
|
|
|
|
var size = $("#dialog").dialog('option', 'sizeCache-switch');
|
|
|
|
if (size) {
|
|
|
|
switchDialogResize(null, { size:size });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$("#dialog").one("dialogclose", function(ev, ui) {
|
|
|
|
$("#dialog").off("dialogresize", switchDialogResize);
|
|
|
|
});
|
|
|
|
},
|
2014-02-08 23:36:23 +01:00
|
|
|
oneditsave: function() {
|
2014-02-09 11:52:56 +01:00
|
|
|
var breakpoints = $("#node-input-breakpoint-container").children();
|
|
|
|
var node = this;
|
|
|
|
node.breakpoints = [];
|
|
|
|
breakpoints.each(function (i) {
|
|
|
|
var breakpoint = $(this);
|
|
|
|
var r = {};
|
2014-02-12 23:27:08 +01:00
|
|
|
r.input = Number(breakpoint.find(".node-input-breakpoint-input-value").val());
|
|
|
|
r.output = Number(breakpoint.find(".node-input-breakpoint-output-value").val());
|
2014-02-09 11:52:56 +01:00
|
|
|
r.mutable = breakpoint.find(".node-input-breakpoint-mutable").attr("mutable") == "true";
|
|
|
|
node.breakpoints.push(r);
|
|
|
|
});
|
|
|
|
node.breakpoints = node.breakpoints.sort(function (a, b) { return a.input - b.input; });
|
|
|
|
}
|
2014-02-08 14:06:47 +01:00
|
|
|
});
|
2014-02-03 21:06:41 +01:00
|
|
|
</script>
|