UNPKG

@iiot2k/node-red-gpio

Version:

Node-RED nodes for Raspberry Pi gpio

189 lines (153 loc) 6.03 kB
/** * Copyright 2025 Derya Y. (iiot2k@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. **/ "use strict"; module.exports = function(RED) { const syslib = require("./syslib.js"); const gpiox = require("@iiot2k/gpiox"); if (gpiox === undefined) throw new Error("driver error @iiot2k/gpiox"); RED.nodes.registerType("iiot2k-gpio-out", function(n) { const node = this; RED.nodes.createNode(node, n); node.gpio = parseInt(n.gpio); node.name = "gpio" + node.gpio; switch(n.mode) { case "output": node.mode = gpiox.GPIO_MODE_OUTPUT; break; case "sink": node.mode = gpiox.GPIO_MODE_OUTPUT_SINK; break; case "source": node.mode = gpiox.GPIO_MODE_OUTPUT_SOURCE; break; } const error_text = () => gpiox.error_text(); const status_text = (val) => (val ? "1" : "0"); const status_color = (val) => val ? "green" : "grey"; if (!gpiox.init_gpio(node.gpio, node.mode, n.activate)) node.iserror = syslib.outError(node, error_text(), error_text()); else { node.iserror = false; syslib.setStatus(node, status_text(n.activate), status_color(n.activate)); } const check_parameter = (p) => { var pval = Number(p); if (isNaN(pval)) { syslib.outError(node, "invalid parameter"); return undefined; } else return pval; } const check_return = (ret) => { if (!ret) syslib.outError(node, gpiox.error_text()); return ret; } node.on("input", (msg) => { if (node.iserror) return; var pval; if (msg.hasOwnProperty('payload')) { pval = check_parameter(msg.payload); if (pval === undefined) return; pval = (pval > 0) ? 1 : 0; if (!check_return(gpiox.set_gpio(node.gpio, pval))) return; } else if (msg.hasOwnProperty('toggle')) { if (!check_return(gpiox.toggle_gpio(node.gpio))) return; } else if (msg.hasOwnProperty('blink')) { pval = check_parameter(msg.blink); if (pval === undefined) return; if (pval < 0) pval = 0; if (!check_return(gpiox.blink_gpio(node.gpio, pval))) return; if (pval > 0) { syslib.setStatus(node, "blink " + pval + "ms", "blue"); return; } } else if (msg.hasOwnProperty('blink_stop')) { check_return(gpiox.stop_blink_gpio(node.gpio)); } else if (msg.hasOwnProperty('pulse')) { pval = check_parameter(msg.pulse); if (pval === undefined) return; if (pval < 0) pval = 0; if (!check_return(gpiox.pulse_gpio(node.gpio, pval))) return; if (pval > 0) { syslib.setStatus(node, "pulse " + pval + "ms", "blue"); return; } } else if (msg.hasOwnProperty('pulse_stop')) { check_return(gpiox.stop_pulse_gpio(node.gpio)); } else if (msg.hasOwnProperty('pwm')) { var duty_cycle = gpiox.get_pwm_dutycycle(node.gpio); var frequency = gpiox.get_pwm_frequency(node.gpio); if (msg.pwm.hasOwnProperty('duty')) { duty_cycle = check_parameter(msg.pwm.duty); if (duty_cycle === undefined) return; } if (msg.pwm.hasOwnProperty('freq')) { frequency = check_parameter(msg.pwm.freq); if (frequency === undefined) return; } if (duty_cycle < 0) duty_cycle = 0; else if (duty_cycle > gpiox.DUTY_MAX) duty_cycle = gpiox.DUTY_MAX; if (frequency < 0) frequency = 0; else if (frequency > gpiox.FREQ_MAX) frequency = gpiox.FREQ_MAX; if (!check_return(gpiox.pwm_gpio(node.gpio, frequency, duty_cycle))) return; if (frequency > 0) { syslib.setStatus(node, "pwm " + frequency + "Hz, " + duty_cycle + "%", "blue"); return; } } else if (msg.hasOwnProperty('pwm_stop')) { check_return(gpiox.stop_pwm_gpio(node.gpio)); } var gpio_state = gpiox.get_gpio(node.gpio); if (gpio_state !== undefined) syslib.setStatus(node, status_text(gpio_state), status_color(gpio_state)); else syslib.outError(node, error_text()); }); node.on('close', () => { gpiox.deinit_gpio(node.gpio); }); }); }