pigpiod html lint ===

This commit is contained in:
Dave Conway-Jones 2017-05-24 19:45:31 +01:00
parent a00fcb3726
commit 4768ecc16c
No known key found for this signature in database
GPG Key ID: 81B04231572A9A2D
5 changed files with 91 additions and 28 deletions

View File

@ -21,6 +21,13 @@
<label for="node-input-round">(optionally)</label>
round to <input type="text" id="node-input-round" placeholder="ignore" style="width:50px;"/> decimal places
</div>
<div class="form-row">
<label for="node-input-mult">Treat</label>
<select id="node-input-mult" style="width:60%; margin-right:5px;">
<option value="single">All msg as one stream.</option>
<option value="multi">Different msg.topic as individual streams.</option>
</select>
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
@ -31,6 +38,7 @@
<script type="text/x-red" data-help-name="smooth">
<p>A simple node to provide various functions across several previous values, including max, min, mean, high and low pass filters.</p>
<p>Messages arriving with different <code>msg.topic</code> can be treated as separate streams if so configured.</p>
<p>Max, Min and Mean work over a specified number of previous values.</p>
<p>The High and Low pass filters use a smoothing factor. The higher the number the more the smoothing. E.g. a value of 10 is
similar to an &alpha; of 0.1. It is analagous to an RC time constant - but there is no time component to this as the
@ -47,7 +55,8 @@
name: {value:""},
action: {value:"mean"},
count: {value:"10",required:true,validate:RED.validators.number()},
round: {value:""}
round: {value:""},
mult: {value:"single"}
},
inputs: 1,
outputs: 1,

View File

@ -8,48 +8,50 @@ module.exports = function(RED) {
this.round = n.round || false;
if (this.round == "true") { this.round = 0; }
this.count = Number(n.count);
this.mult = n.mult || "single";
var node = this;
var a = [];
var tot = 0;
var tot2 = 0;
var pop = 0;
var old = null;
var v = {};
this.on('input', function (msg) {
if (msg.hasOwnProperty("reset")) {
a = [];
tot = 0;
tot2 = 0;
pop = 0;
old = null;
var top = msg.topic || "_my_default_topic";
if (this.mult === "single") { top = "a"; }
if ((v.hasOwnProperty(top) !== true) || msg.hasOwnProperty("reset")) {
v[top] = {};
v[top].a = [];
v[top].tot = 0;
v[top].tot2 = 0;
v[top].pop = 0;
v[top].old = null;
v[top].count = this.count;
}
if (msg.hasOwnProperty("payload")) {
var n = Number(msg.payload);
if (!isNaN(n)) {
if ((node.action === "low") || (node.action === "high")) {
if (old == null) { old = n; }
old = old + (n - old) / node.count;
if (node.action === "low") { msg.payload = old; }
else { msg.payload = n - old; }
if (v[top].old == null) { v[top].old = n; }
v[top].old = v[top].old + (n - v[top].old) / v[top].count;
if (node.action === "low") { msg.payload = v[top].old; }
else { msg.payload = n - v[top].old; }
}
else {
a.push(n);
if (a.length > node.count) { pop = a.shift(); }
v[top].a.push(n);
if (v[top].a.length > v[top].count) { v[top].pop = v[top].a.shift(); }
if (node.action === "max") {
msg.payload = Math.max.apply(Math, a);
msg.payload = Math.max.apply(Math, v[top].a);
}
if (node.action === "min") {
msg.payload = Math.min.apply(Math, a);
msg.payload = Math.min.apply(Math, v[top].a);
}
if (node.action === "mean") {
tot = tot + n - pop;
msg.payload = tot / a.length;
v[top].tot = v[top].tot + n - v[top].pop;
msg.payload = v[top].tot / v[top].a.length;
}
if (node.action === "sd") {
tot = tot + n - pop;
tot2 = tot2 + (n*n) - (pop * pop);
if (a.length > 1) {
msg.payload = Math.sqrt((a.length * tot2 - tot * tot)/(a.length * (a.length - 1)));
v[top].tot = v[top].tot + n - v[top].pop;
v[top].tot2 = v[top].tot2 + (n*n) - (v[top].pop * v[top].pop);
if (v[top].a.length > 1) {
msg.payload = Math.sqrt((v[top].a.length * v[top].tot2 - v[top].tot * v[top].tot)/(v[top].a.length * (v[top].a.length - 1)));
}
else { msg.payload = 0; }
}

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-smooth",
"version" : "0.0.10",
"version" : "0.0.11",
"description" : "A Node-RED node that provides several simple smoothing algorithms for incoming data values.",
"dependencies" : {
},

View File

@ -458,7 +458,7 @@
else if (this.out === "ser") { return this.name || "Servo: "+bcm2pin[this.pin]; }
else {
var suf = "";
if (this.set == true) { suf = (this.level === "1") ? "¹ " : "₀ "; }
if (this.set === true) { suf = (this.level === "1") ? "¹ " : "₀ "; }
return this.name || "PIN: " + bcm2pin[this.pin] + suf ;
}
},

View File

@ -219,4 +219,56 @@ describe('smooth node', function() {
});
});
it('should average over a number of inputs on different topics', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
if (c === 3) { msg.should.have.a.property("payload", 1); }
if (c === 5) { msg.should.have.a.property("payload", 2); }
if (c === 6) { msg.should.have.a.property("payload", 3); }
if (c === 7) { msg.should.have.a.property("payload", 3); done(); }
});
n1.emit("input", {payload:2, topic:"A"});
n1.emit("input", {payload:1, topic:"A"});
n1.emit("input", {payload:0, topic:"A"});
n1.emit("input", {payload:3, topic:"B"});
n1.emit("input", {payload:4, topic:"B"});
n1.emit("input", {payload:7, topic:"A"});
n1.emit("input", {payload:1, topic:"B"});
});
});
it('should average over different topics if asked', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", mult:"true", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
if (c === 3) { msg.should.have.a.property("payload", 1); }
if (c === 5) { msg.should.have.a.property("payload", 4); }
if (c === 9) { msg.should.have.a.property("payload", 4); }
if (c === 11) { msg.should.have.a.property("payload", 5); done(); }
});
n1.emit("input", {payload:2, topic:"A"});
n1.emit("input", {payload:1, topic:"A"});
n1.emit("input", {payload:0, topic:"A"});
n1.emit("input", {payload:3, topic:"B"});
n1.emit("input", {payload:5, topic:"B"});
n1.emit("input", {payload:5, topic:"A"});
n1.emit("input", {payload:2, topic:"B"});
n1.emit("input", {payload:7, topic:"A"});
n1.emit("input", {payload:3, topic:"A"});
n1.emit("input", {payload:6, topic:"B"});
n1.emit("input", {payload:9, topic:"B"});
});
});
});