@redplc/node-red-gpio
Version:
Node-RED nodes for Raspberry Pi gpio using with redPlc nodes
110 lines (90 loc) • 3.84 kB
JavaScript
/**
* Copyright 2024 Derya Y. (iot.redplc@gmail.com).
*
* 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.
**/
;
module.exports = function(RED) {
const syslib = require("./syslib.js");
const gpiox = require("@iiot2k/gpiox");
const NOSET = 0;
if (gpiox === undefined)
throw new Error("driver error @iiot2k/gpiox");
RED.nodes.registerType("redplc-gpio-out", function(n) {
const node = this;
RED.nodes.createNode(node, n);
node.varname = "Q" + n.address;
node.name = "gpio-out " + node.varname;
node.tupdate = n.tupdate;
node.ctxvar = new Array(28);
node.ctxvars = new Array(28);
node.isdo = false;
node.iserror = false;
node.onwork = false;
syslib.setStatus(node, node.varname, "grey");
if (!syslib.createVariable(node, node.varname, node.ctxvar))
node.iserror = syslib.outError(node, node.varname + " duplicate", node.varname + " duplicate");
else if (n.gpioinit.length == 0)
node.iserror = syslib.setStatus(node, node.varname + " noselect", "grey");
else {
for (var idx = 0; idx <= 25; idx++) {
var gpiomode;
switch(parseInt(n.gpioinit[idx])) {
case 1: gpiomode = gpiox.GPIO_MODE_OUTPUT; break;
case 2: gpiomode = gpiox.GPIO_MODE_OUTPUT_SOURCE; break;
case 3: gpiomode = gpiox.GPIO_MODE_OUTPUT_SINK; break;
default: continue;
}
var pin = idx + 2; // gpio pin 2..27
node.isdo = true;
if (!gpiox.init_gpio(pin, gpiomode, parseInt(n.gpioactv[idx]))) {
node.iserror = syslib.outError(node, " pin " + pin + " " + gpiox.error_text(), " pin " + pin + " " + gpiox.error_text());
break;
}
node.ctxvar[pin] = gpiox.get_gpio(pin);
node.ctxvars[pin] = node.ctxvar[pin];
}
if (!node.isdo)
node.iserror = syslib.setStatus(node, node.varname + " noselect", "grey");
}
function update() {
for (var idx = 0; idx <= 25; idx++) {
if (n.gpioinit[idx] == NOSET)
continue;
var pin = idx + 2; // gpio pin 2..27
if (node.ctxvar[pin] != node.ctxvars[pin]) {
node.ctxvars[pin] = node.ctxvar[pin];
gpiox.set_gpio(pin, node.ctxvar[pin]);
}
}
node.onwork = false;
}
node.on("input", function (msg) {
if (node.iserror || node.onwork || (msg.payload !== true)) {
node.send(msg);
return;
}
node.onwork = true;
node.id_update = setTimeout(update, node.tupdate);
syslib.setStatus(node, node.varname);
node.send(msg);
});
node.on('close', function () {
clearTimeout(node.id_update);
syslib.deleteVariable(node, node.varname);
for (var idx = 0; idx <= 25; idx++)
if (n.gpioinit[idx] != NOSET)
gpiox.deinit_gpio(idx + 2);
});
});
}