diff --git a/lib/gpioControl.js b/lib/gpioControl.js index e5ee9c4..5d38d3d 100644 --- a/lib/gpioControl.js +++ b/lib/gpioControl.js @@ -19,17 +19,6 @@ function getRaspberryModelFromCpuInfo(cpuinfo) { return Number(model); } -// not used.. classes are too overengineered... :-( -//function parseModelStringToClassName(modelString) { -// const modelRegEx = /.*Raspberry Pi (\d+) Model B(\+?).*/mi; -// const parts = modelRegEx.exec(modelString); -// if (parts) { -// //not sure about those other devices, like Nano whatever... they need to report. :-p -// return `RaspberryPi_${parts[1]}B${parts[2] ? 'Plus' : ''}`; -// } -// return 'Default'; -//} - class GpioControl { constructor(adapter, log) { this.adapter = adapter; @@ -54,10 +43,12 @@ class GpioControl { if (gpioPorts.length === 0 && buttonPorts.length === 0) return; try { - const { Default, Edge } = require('opengpio'); + + const { DefaultDevice, Edge } = require('opengpio'); + let chipNum = 0; try { - this.log.debug(Default); + this.log.debug(DefaultDevice); const {stdout, stderr} = await exec('cat /proc/device-tree/model'); this.log.debug('CPU Info: ' + stdout); @@ -80,14 +71,14 @@ class GpioControl { this.log.debug('Buttons are pull ' + (this.config.buttonPullUp ? 'up' : 'down') + '.'); //this.gpioChip = openGpioChip(chipPath); -> somehow not necessary with this library? - this.gpioChip = Default; - if (Default === undefined) { + this.gpioChip = DefaultDevice; + if (this.gpioChip === undefined) { this.log.warn('Cannot initialize GPIO: No chip found. GPIO functionality disabled!'); this.log.warn('Please make sure that libgpiod-dev (on raspian/debian run sudo apt install libgpiod-dev) is installed in the system and then reinstall the adapter.'); this.log.warn('If the library is installed and npm list | grep opengpio shows the npm library is also installed, please report this issue to the adapter developer with the model of your device and deboug output from an adapter start.'); } - this.log.debug('Got chip: ' + Default); - this.log.debug(`GPIO chip ${JSON.stringify(Default?.info)} initialized`); + this.log.debug('Got chip: ' + this.gpioChip); + this.log.debug(`GPIO chip ${JSON.stringify(this.gpioChip?.info)} initialized`); if (this.gpioChip) { // Setup all the regular GPIO input and outputs. @@ -97,19 +88,19 @@ class GpioControl { //TODO: currently pull-up / pull-down is not supported by the library. if (direction === 'in') { - const watch = Default.watch({chip: chipNum, line: port}, Edge.Both); + const watch = this.gpioChip.watch({chip: chipNum, line: port}, Edge.Both); this.gpioPorts[port] = watch; this.gpioInputPorts.push(port); await this.readValue(port); } else { - const pin = Default.output({chip: chipNum, line: port}); + const pin = this.gpioChip.output({chip: chipNum, line: port}); this.gpioPorts[port] = pin; this.gpioOutputPorts.push(port); } } for (const port of buttonPorts) { //still the same as input ports... - const watch = Default.watch({chip: chipNum, line: port}, Edge.Both); + const watch = this.gpioChip.watch({chip: chipNum, line: port}, Edge.Both); this.gpioPorts[port] = watch; this.gpioInputPorts.push(port); await this.readValue(port);