Skip to content

Commit

Permalink
another fix to gpio stuff
Browse files Browse the repository at this point in the history
hopefully fixes the "undefined" errors.
  • Loading branch information
Garfonso committed Oct 13, 2024
1 parent 0b66dbc commit ef4ff1b
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions lib/gpioControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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);
Expand Down

0 comments on commit ef4ff1b

Please sign in to comment.