Skip to content

Commit

Permalink
Merge pull request #208 from jangatzke/new-gpio-lib
Browse files Browse the repository at this point in the history
Update gpioControl.js
  • Loading branch information
Garfonso authored Oct 8, 2024
2 parents 90119ec + c83d828 commit 64ae8ec
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/gpioControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ const util = require('util');
const exec = util.promisify(require('node:child_process').exec);

function getRaspberryModelFromCpuInfo(cpuinfo) {
const modelRegEx = /^Raspberry Pi (\d+) Model.*/mi;
const model = modelRegEx.exec(cpuinfo)[1];
return Number(model);
// RPi Zero familiy has a different naming. All Rpi Zero share the same ChipNum as RPi 1 and thus can all be handled as a Rpi 1.
if (cpuinfo.includes("Zero")) {

Check failure on line 11 in lib/gpioControl.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Strings must use singlequote
return 1;
}
else {
const modelRegEx = /^Raspberry Pi (\d+) Model.*/mi;
const model = modelRegEx.exec(cpuinfo)[1];
return Number(model);
}
}

// not used.. classes are too overengineered... :-(
Expand Down Expand Up @@ -48,20 +54,21 @@ class GpioControl {

try {
const { Default, Edge } = require('opengpio');
let chipNum = 0;
let chipNum = null;
try {
this.log.debug(Default);

const {stdout, stderr} = await exec('cat /proc/device-tree/model');
const { stdout, stderr } = await exec('cat /proc/device-tree/model');
this.log.debug('CPU Info: ' + stdout);
this.log.debug('STDERR: ' + stderr);
const model = getRaspberryModelFromCpuInfo(stdout);
this.log.debug(`Got ${model} from ${stdout}.`);
if (model >= 5) {
this.log.debug('Using GPIO chip 4 for Raspberry Pi 5 or newer.');
chipNum = 4;
} else {
chipNum = 0;
}
this.gpioChip = true; //let's keep this condition for now.
this.gpioChip = true; //let's keep this condition for now.

Check failure on line 71 in lib/gpioControl.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
} catch (e) {
this.log.error('Cannot read CPU Info: ' + e);
}
Expand Down

0 comments on commit 64ae8ec

Please sign in to comment.