UNPKG

nixfilter-rpi-gpio

Version:

Filter for sending binary signal sequences via the GPIO pin of a Raspberry Pi

79 lines (72 loc) 3.25 kB
#!/usr/bin/env node 'use strict'; var nixfilter, nixfilter_logicsignal, rpio; // Require the "nixfilter" module nixfilter = require('nixfilter'); // Import/Require the "nixfilter-logicsignal" module nixfilter_logicsignal = require('nixfilter-logicsignal'); // Import/Require the "rpio" module rpio = require('rpio'); // Define the filter and register it on the module nixfilter.filter(module, { description: 'Send logic/binary signals via a GPIO pin of a Raspberry Pi. The logic/binary signals are read as lines from STDIN, as comma-separated integer values representing durations in nanoseconds. Negative values indicate "inactive"/"low" levels, positive values indicate "active"/"high" levels. For example, the input line "360000,-1080000,+1080000,360000" would output a binary signal of 360µs "high" level, followed by 1080µs "low" level, followed by 1080µs "high" level, followed by 360µs "low" level.', input_reader: nixfilter_logicsignal.reader.logic_signal(), output_writer: null, add_arguments: function(argument_parser) { argument_parser.addArgument(['--latency', '-l'], { type: 'int', defaultValue: 65, help: 'The latency overhead (in microseconds) for every signal. This depends on the Raspberry Pi being used; on the Raspberry Pi 3B+, this is about 65. (default: 65)' }); argument_parser.addArgument(['--inactive_state', '-i'], { choices: ['low', 'high'], defaultValue: 'low', help: 'The GPIO pin\'s inactive state, if no signal is being sent (default: low)' }); argument_parser.addArgument(['gpio_pin_id'], { type: 'int', help: 'The numerical ID of the GPIO pin to use for outputting the signals' }); }, setup: function(args) { if (this.args.inactive_state === 'low') { this.inactive_state = rpio.LOW; this.active_state = rpio.HIGH; } else { this.inactive_state = rpio.HIGH; this.active_state = rpio.LOW; } // Set the GPIO pin to its "inactive" state and open it return rpio.open(this.args.gpio_pin_id, rpio.OUTPUT, this.inactive_state); }, output_timings: function(signal_timings) { var active_state, gpio_pin_id, i, inactive_state, j, len, len1, timing, timings; // Reduce timings by latency timings = [-200]; for (i = 0, len = signal_timings.length; i < len; i++) { timing = signal_timings[i]; timings.push(Math.max(Math.abs(Math.floor(timing / 1000)) - this.args.latency, 1) * (timing > 0 ? 1 : -1)); } // Setup local variables to ensure the "for" loop runs as fast as possible gpio_pin_id = this.args.gpio_pin_id; inactive_state = this.inactive_state; active_state = this.active_state; // Output the signal for (j = 0, len1 = timings.length; j < len1; j++) { timing = timings[j]; if (timing > 0) { rpio.write(gpio_pin_id, active_state); rpio.usleep(timing); } else { rpio.write(gpio_pin_id, inactive_state); rpio.usleep(-timing); } } // Set the GPIO pin to it's "inactive" state again rpio.write(gpio_pin_id, inactive_state); }, on_input: function(logic_signal) { this.output_timings(logic_signal.get_timings()); } }); //# sourceMappingURL=rpi_gpio_send.js.map