mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add PWM support to Pi GPIO Node - pin 12 (GPIO1)
(only pin that has hardware pwm support) Note: It will interfere with any other audio output as they share same hardware/timers.
This commit is contained in:
parent
4249cf5d69
commit
ed9951f065
@ -58,7 +58,7 @@
|
|||||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
<input type="text" id="node-input-name" placeholder="Name">
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-tips">Tip: Only Digital I/O is supported - input must be 0 or 1.</div>
|
<div class="form-tips">Tip: Only Digital Input is supported - input must be 0 or 1.</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="rpi-gpio in">
|
<script type="text/x-red" data-help-name="rpi-gpio in">
|
||||||
@ -113,7 +113,7 @@
|
|||||||
<script type="text/x-red" data-template-name="rpi-gpio out">
|
<script type="text/x-red" data-template-name="rpi-gpio out">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-pin"><i class="fa fa-circle"></i> GPIO Pin</label>
|
<label for="node-input-pin"><i class="fa fa-circle"></i> GPIO Pin</label>
|
||||||
<select type="text" id="node-input-pin" style="width: 200px;">
|
<select type="text" id="node-input-pin" style="width: 275px;">
|
||||||
<option value='' disabled selected style='display:none;'>select pin</option>
|
<option value='' disabled selected style='display:none;'>select pin</option>
|
||||||
<option value="3">3 - SDA1 </option>
|
<option value="3">3 - SDA1 </option>
|
||||||
<option value="5">5 - SCL1 </option>
|
<option value="5">5 - SCL1 </option>
|
||||||
@ -121,7 +121,7 @@
|
|||||||
<option value="8">8 - TxD </option>
|
<option value="8">8 - TxD </option>
|
||||||
<option value="10">10 - RxD </option>
|
<option value="10">10 - RxD </option>
|
||||||
<option value="11">11 - GPIO0</option>
|
<option value="11">11 - GPIO0</option>
|
||||||
<option value="12">12 - GPIO1</option>
|
<option value="12">12 - GPIO1 (PWM)</option>
|
||||||
<option value="13">13 - GPIO2</option>
|
<option value="13">13 - GPIO2</option>
|
||||||
<option value="15">15 - GPIO3</option>
|
<option value="15">15 - GPIO3</option>
|
||||||
<option value="16">16 - GPIO4</option>
|
<option value="16">16 - GPIO4</option>
|
||||||
@ -135,14 +135,21 @@
|
|||||||
</select>
|
</select>
|
||||||
<span id="pitype"></span>
|
<span id="pitype"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row" id="node-set-pwm">
|
||||||
|
<label> Type</label>
|
||||||
|
<select id="node-input-out" style="width: 275px;">
|
||||||
|
<option value="out">Digital output</option>
|
||||||
|
<option value="pwm">PWM output</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row" id="node-set-tick">
|
||||||
<label> </label>
|
<label> </label>
|
||||||
<input type="checkbox" id="node-input-set" style="display: inline-block; width: auto; vertical-align: top;">
|
<input type="checkbox" id="node-input-set" style="display: inline-block; width: auto; vertical-align: top;">
|
||||||
<label for="node-input-set" style="width: 70%;">Initialise pin state ?</label>
|
<label for="node-input-set" style="width: 70%;">Initialise pin state ?</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row" id="node-set-state">
|
<div class="form-row" id="node-set-state">
|
||||||
<label for="node-input-level"> </label>
|
<label for="node-input-level"> </label>
|
||||||
<select id="node-input-level" style="width: 300px;">
|
<select id="node-input-level" style="width: 275px;">
|
||||||
<option value="0">initial level of pin - low (0)</option>
|
<option value="0">initial level of pin - low (0)</option>
|
||||||
<option value="1">initial level of pin - high (1)</option>
|
<option value="1">initial level of pin - high (1)</option>
|
||||||
</select>
|
</select>
|
||||||
@ -152,13 +159,15 @@
|
|||||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
<input type="text" id="node-input-name" placeholder="Name">
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-tips">Tip: Only Digital I/O is supported - input must be 0 or 1.</div>
|
<div class="form-tips" id="dig-tip"><b>Tip</b>: For digital output - input must be 0 or 1.</div>
|
||||||
|
<div class="form-tips" id="pwm-tip"><b>Tip</b>: For PWM output - input must be between 0 to 1023.</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="rpi-gpio out">
|
<script type="text/x-red" data-help-name="rpi-gpio out">
|
||||||
<p>Raspberry Pi output node. Expects a <b>msg.payload</b> with either a 0 or 1 (or true or false). Requires the gpio command to work.</p>
|
<p>Raspberry Pi output node. Expects a <b>msg.payload</b> with either a 0 or 1 (or true or false). Requires the gpio command to work.</p>
|
||||||
<p>Will set the selected physical pin high or low depending on the value passed in.</p>
|
<p>Will set the selected physical pin high or low depending on the value passed in.</p>
|
||||||
<p>The initial value of the pin at deploy time can also be set to 0 or 1.</p>
|
<p>The initial value of the pin at deploy time can also be set to 0 or 1.</p>
|
||||||
|
<p>Use of PWM on Pin 12 - GPIO1 will interfere with any other audio playback.</p>
|
||||||
<p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p>
|
<p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -169,20 +178,23 @@
|
|||||||
defaults: {
|
defaults: {
|
||||||
name: { value:"" },
|
name: { value:"" },
|
||||||
pin: { value:"",required:true,validate:RED.validators.number() },
|
pin: { value:"",required:true,validate:RED.validators.number() },
|
||||||
set: { value:false },
|
set: { value:"" },
|
||||||
level: { value:"0" }
|
level: { value:"0" },
|
||||||
|
out: { value:"out" }
|
||||||
},
|
},
|
||||||
inputs:1,
|
inputs:1,
|
||||||
outputs:0,
|
outputs:0,
|
||||||
icon: "rpi.png",
|
icon: "rpi.png",
|
||||||
align: "right",
|
align: "right",
|
||||||
label: function() {
|
label: function() {
|
||||||
return this.name||"Pin: "+this.pin;
|
if ((this.pin == 12) && (this.out === "pwm")) { return this.name || "PWM: 12"; }
|
||||||
|
else { return this.name||"Pin: "+this.pin ; }
|
||||||
},
|
},
|
||||||
labelStyle: function() {
|
labelStyle: function() {
|
||||||
return this.name?"node_label_italic":"";
|
return this.name?"node_label_italic":"";
|
||||||
},
|
},
|
||||||
oneditprepare: function() {
|
oneditprepare: function() {
|
||||||
|
if (!$("#node-input-out").val()) { $("#node-input-out").val("out"); }
|
||||||
$.getJSON('rpi-gpio/'+this.id,function(data) {
|
$.getJSON('rpi-gpio/'+this.id,function(data) {
|
||||||
$('#pitype').text(data.type);
|
$('#pitype').text(data.type);
|
||||||
if (data.type === "Model B+") {
|
if (data.type === "Model B+") {
|
||||||
@ -200,13 +212,44 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$("#node-input-set").change(function() {
|
var hidepwm = function() {
|
||||||
|
if ($('#node-input-pin').val() == 12) {
|
||||||
|
$('#node-set-pwm').show();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$('#node-set-pwm').hide();
|
||||||
|
$('#node-input-out').val("out");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$("#node-input-pin").change(function () { hidepwm(); });
|
||||||
|
hidepwm();
|
||||||
|
|
||||||
|
var hidestate = function () {
|
||||||
|
if ($("#node-input-out").val() === "pwm") {
|
||||||
|
$('#node-set-tick').hide();
|
||||||
|
$('#node-set-state').hide();
|
||||||
|
$('#node-input-set').prop('checked', false);
|
||||||
|
$("#dig-tip").hide();
|
||||||
|
$("#pwm-tip").show();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$('#node-set-tick').show();
|
||||||
|
$("#dig-tip").show();
|
||||||
|
$("#pwm-tip").hide();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$("#node-input-out").change(function () { hidestate(); });
|
||||||
|
hidestate();
|
||||||
|
|
||||||
|
var setstate = function () {
|
||||||
if ($('#node-input-set').is(":checked")) {
|
if ($('#node-input-set').is(":checked")) {
|
||||||
$("#node-set-state").show();
|
$("#node-set-state").show();
|
||||||
} else {
|
} else {
|
||||||
$("#node-set-state").hide();
|
$("#node-set-state").hide();
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
$("#node-input-set").change(function () { setstate(); });
|
||||||
|
setstate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
@ -139,13 +139,15 @@ module.exports = function(RED) {
|
|||||||
this.pin = pintable[n.pin];
|
this.pin = pintable[n.pin];
|
||||||
this.set = n.set || false;
|
this.set = n.set || false;
|
||||||
this.level = n.level || 0;
|
this.level = n.level || 0;
|
||||||
|
this.out = n.out || "out";
|
||||||
var node = this;
|
var node = this;
|
||||||
|
(node.out === "pwm") ? (node.op = "pwm") : (node.op = "write");
|
||||||
|
|
||||||
if (node.pin !== undefined) {
|
if (node.pin !== undefined) {
|
||||||
exec(gpioCommand+" mode "+node.pin+" out", function(err,stdout,stderr) {
|
exec(gpioCommand+" mode "+node.pin+" "+node.out, function(err,stdout,stderr) {
|
||||||
if (err) { node.error(err); }
|
if (err) { node.error(err); }
|
||||||
else {
|
else {
|
||||||
if (node.set) {
|
if (node.set && (node.out === "out")) {
|
||||||
exec(gpioCommand+" write "+node.pin+" "+node.level, function(err,stdout,stderr) {
|
exec(gpioCommand+" write "+node.pin+" "+node.level, function(err,stdout,stderr) {
|
||||||
if (err) { node.error(err); }
|
if (err) { node.error(err); }
|
||||||
});
|
});
|
||||||
@ -154,12 +156,14 @@ module.exports = function(RED) {
|
|||||||
if (msg.payload === "true") { msg.payload = true; }
|
if (msg.payload === "true") { msg.payload = true; }
|
||||||
if (msg.payload === "false") { msg.payload = false; }
|
if (msg.payload === "false") { msg.payload = false; }
|
||||||
var out = Number(msg.payload);
|
var out = Number(msg.payload);
|
||||||
if ((out === 0)|(out === 1)) {
|
var limit = 1;
|
||||||
exec(gpioCommand+" write "+node.pin+" "+out, function(err,stdout,stderr) {
|
if (node.out === "pwm") { limit = 1023; }
|
||||||
|
if ((out >= 0) && (out <= limit)) {
|
||||||
|
exec(gpioCommand+" "+node.op+" "+node.pin+" "+out, function(err,stdout,stderr) {
|
||||||
if (err) { node.error(err); }
|
if (err) { node.error(err); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else { node.warn("Invalid input - not 0 or 1"); }
|
else { node.warn("Invalid input: "+out); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user