UNPKG

@iiot2k/gpiox

Version:

Input/Output on any gpio library

52 lines (40 loc) 1.39 kB
/* * example generate pwm on gpio output * * run: * > node pwm_gpio.js * */ "use strict"; const gpiox = require("../gpiox"); const PIN_INPUT = 21; const PIN_OUTPUT = 20; const FREQUENCY = 200; // Hz const DUTY_CYCLE1 = 30; // % const DUTY_CYCLE2 = 50; // % const DUTY_CYCLE3 = 70; // % const DEBOUNCE_US = 1000; // us console.log("*** pwm gpio node.js example ***"); console.log("stop program with Ctrl+C"); // init gpio gpiox.init_gpio(PIN_INPUT, gpiox.GPIO_MODE_INPUT_PULLUP, DEBOUNCE_US); gpiox.init_gpio(PIN_OUTPUT, gpiox.GPIO_MODE_OUTPUT, 0); // depends on input generate pwm output if (gpiox.get_gpio(PIN_INPUT)) gpiox.pwm_gpio(PIN_OUTPUT, FREQUENCY, DUTY_CYCLE1); else gpiox.pwm_gpio(PIN_OUTPUT, FREQUENCY, DUTY_CYCLE2); console.log("frequency", gpiox.get_pwm_frequency(PIN_OUTPUT), "duty cycle", gpiox.get_pwm_dutycycle(PIN_OUTPUT)); // after 5 sec change duty cycle var id_tout = setTimeout(function() { gpiox.pwm_gpio(PIN_OUTPUT, FREQUENCY, DUTY_CYCLE3); console.log("frequency", gpiox.get_pwm_frequency(PIN_OUTPUT), "duty cycle", gpiox.get_pwm_dutycycle(PIN_OUTPUT)); }, 5000); // prevent stop program var id_intv = setInterval(function() {}, 100); process.on('SIGINT', () => { clearTimeout(id_tout); clearInterval(id_intv); console.log(" -> program stopped"); process.exit(); });