Skip to content

Commit

Permalink
fix stupidity
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfonso committed May 24, 2024
1 parent 8c163b3 commit a1668d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
49 changes: 25 additions & 24 deletions lib/gpioControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
const buttonEvents = [ 'pressed', 'clicked', 'clicked_pressed', 'double_clicked', 'released' ];

class GpioControl {
constructor(adapter) {
constructor(adapter, log) {
this.adapter = adapter;
this.gpio = null;
this.gpioButtons = null;
this.config = adapter.config;
this.debounceTimers = [];
this.log = log;
}

/**
Expand All @@ -20,15 +21,15 @@ class GpioControl {
setupGpio(gpioPorts, buttonPorts) {
if (gpioPorts.length === 0 && buttonPorts.length === 0) return;

this.adapter.log.debug('Inputs are pull ' + (this.config.inputPullUp ? 'up' : 'down') + '.');
this.adapter.log.debug('Buttons are pull ' + (this.config.buttonPullUp ? 'up' : 'down') + '.');
this.log.debug('Inputs are pull ' + (this.config.inputPullUp ? 'up' : 'down') + '.');
this.log.debug('Buttons are pull ' + (this.config.buttonPullUp ? 'up' : 'down') + '.');

try {
this.gpio = require('rpi-gpio');
this.gpio.setMode(this.gpio.MODE_BCM);
} catch (e) {
this.gpio = null;
this.adapter.log.error('Cannot initialize/setMode GPIO: ' + e);
this.log.error('Cannot initialize/setMode GPIO: ' + e);
console.error(e);
if (e.message.includes('NODE_MODULE_VERSION')) {
return this.adapter.terminate('A dependency requires a rebuild.', 13);
Expand All @@ -42,34 +43,34 @@ class GpioControl {
// Setup all the regular GPIO input and outputs.
for (const port of gpioPorts) {
const direction = this.config.gpios[port].input;
this.adapter.log.debug(`Port ${port} direction: ${direction}`);
this.log.debug(`Port ${port} direction: ${direction}`);
if (direction === 'in') {
// Input port
haveGpioInputs = true;
this.gpio.setup(port, this.gpio.DIR_IN, this.gpio.EDGE_BOTH, (err) => {
if (err) {
this.adapter.log.error('Cannot setup port ' + port + ' as input: ' + err);
this.log.error('Cannot setup port ' + port + ' as input: ' + err);
} else {
this.readValue(port);
}
});
} else {
// All the different flavors of output
const directionCode = direction === 'outlow' ? this.gpio.DIR_LOW : direction === 'outhigh' ? this.gpio.DIR_HIGH : this.gpio.DIR_OUT;
this.adapter.log.debug(`Port ${port} directionCode: ${directionCode}`);
this.log.debug(`Port ${port} directionCode: ${directionCode}`);
this.gpio.setup(port, directionCode, (err) => {
err && this.adapter.log.error('Cannot setup port ' + port + ' as output: ' + err);
err && this.log.error('Cannot setup port ' + port + ' as output: ' + err);
});
}
}

// Setup input change handler - only has to be done once no matter how many inputs we have.
if (haveGpioInputs) {
this.adapter.log.debug('Register onchange handler');
this.log.debug('Register onchange handler');
this.gpio.on('change', (port, value) => {
// Ignore buttons as they are handled below
if (this.adapter.config.gpios[port].input === 'in') {
this.adapter.log.debug('GPIO change on port ' + port + ': ' + value);
this.log.debug('GPIO change on port ' + port + ': ' + value);
if (this.debounceTimers[port] !== null) {
// Timer is running, but the state changed (must be back) so just cancel timer.
clearTimeout(this.debounceTimers[port]);
Expand All @@ -78,7 +79,7 @@ class GpioControl {
// Start a timer and report to state if it doesn't revert within a given period.
this.debounceTimers[port] = setTimeout(async (t_port, t_value) => {
this.debounceTimers[t_port] = null;
this.adapter.log.debug(`GPIO debounced on port ${t_port}: ${t_value}`);
this.log.debug(`GPIO debounced on port ${t_port}: ${t_value}`);
await this.adapter.setStateAsync('gpio.' + t_port + '.state', this.inputPullUp(t_value), true);
}, this.config.inputDebounceMs, port, value);
}
Expand All @@ -88,7 +89,7 @@ class GpioControl {

// Setup any buttons using the same rpi-gpio object as other I/O.
if (buttonPorts.length > 0) {
this.adapter.log.debug(`Setting up button ports: ${buttonPorts}`);
this.log.debug(`Setting up button ports: ${buttonPorts}`);
try {
const rpi_gpio_buttons = require('rpi-gpio-buttons');
this.gpioButtons = new rpi_gpio_buttons({
Expand All @@ -103,7 +104,7 @@ class GpioControl {
});
} catch (e) {
this.gpioButtons = null;
this.adapter.log.error('Cannot initialize GPIO Buttons: ' + e);
this.log.error('Cannot initialize GPIO Buttons: ' + e);
console.error(e);
if (e.message.includes('NODE_MODULE_VERSION')) {
return this.adapter.terminate('A dependency requires a rebuild.', 13);
Expand All @@ -113,16 +114,16 @@ class GpioControl {
// Setup events for buttons - only has to be done once no matter how many buttons we have.
if (this.gpioButtons) {
for (const eventName of buttonEvents) {
this.adapter.log.debug(`Register button handler for ${eventName}`);
this.log.debug(`Register button handler for ${eventName}`);
this.gpioButtons.on(eventName, async (port) => {
this.adapter.log.debug(`${eventName} triggered for port ${port}`);
this.log.debug(`${eventName} triggered for port ${port}`);
const stateName = `gpio.${port}.${eventName}`;
await this.adapter.setStateAsync(stateName, true, true);
});
}
// And start button processing
this.gpioButtons.init().catch(err => {
this.adapter.log.error(`An error occurred during buttons init(). ${err.message}`);
this.log.error(`An error occurred during buttons init(). ${err.message}`);
});
}
}
Expand All @@ -136,12 +137,12 @@ class GpioControl {
*/
readValue(port) {
if (!this.gpio) {
return this.adapter.log.error('GPIO is not initialized!');
return this.log.error('GPIO is not initialized!');
}

this.gpio.read(port, async (err, value) => {
if (err) {
this.adapter.log.error('Cannot read port ' + port + ': ' + err);
this.log.error('Cannot read port ' + port + ': ' + err);
} else {
await this.adapter.setStateAsync('gpio.' + port + '.state', this.inputPullUp(value), true);
}
Expand All @@ -156,10 +157,10 @@ class GpioControl {
writeGpio(port, value) {
port = parseInt(port, 10);
if (!this.config.gpios[port] || !this.config.gpios[port].enabled) {
this.adapter.log.warn('Port ' + port + ' is not writable, because disabled.');
this.log.warn('Port ' + port + ' is not writable, because disabled.');
return;
} else if (this.config.gpios[port].input === 'in' || this.config.gpios[port].input === 'true' || this.config.gpios[port].input === true) {
return this.adapter.log.warn('Port ' + port + ' is configured as input and not writable');
return this.log.warn('Port ' + port + ' is configured as input and not writable');
}

if (value === 'true') value = true;
Expand All @@ -171,17 +172,17 @@ class GpioControl {
if (this.gpio) {
this.gpio.write(port, value, async err => {
if (err) {
this.adapter.log.error(err);
this.log.error(err);
} else {
this.adapter.log.debug('Written ' + value + ' into port ' + port);
this.log.debug('Written ' + value + ' into port ' + port);
await this.adapter.setStateAsync('gpio.' + port + '.state', value, true);
}
});
} else {
this.adapter.log.error('GPIO is not initialized!');
this.log.error('GPIO is not initialized!');
}
} catch (error) {
this.adapter.log.error('Cannot write port ' + port + ': ' + error);
this.log.error('Cannot write port ' + port + ': ' + error);
}
}

Expand Down
8 changes: 4 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Rpi2 extends utils.Adapter {
await this.subscribeStatesAsync('*');
await main(this);
await this.initPorts();
this.gpioControl = new GpioControl(this);
this.gpioControl = new GpioControl(this, this.log);
}

async initPorts() {
Expand Down Expand Up @@ -109,15 +109,15 @@ class Rpi2 extends utils.Adapter {
dhtPorts.push(port);
break;
default:
this.adapter.log.error('Cannot setup port ' + port + ': invalid direction type.');
this.log.error('Cannot setup port ' + port + ': invalid direction type.');
}
}
}

this.gpioControl.setupGpio(gpioPorts, buttonPorts);
setupDht(this, dhtPorts);
} else {
this.adapter.log.info('GPIO ports are not configured');
this.log.info('GPIO ports are not configured');
}
}

Expand Down Expand Up @@ -254,7 +254,7 @@ class Rpi2 extends utils.Adapter {
async syncPortDirection(port, data) {
const stateName = 'gpio.' + port + '.isInput';
if (data.enabled) {
this.adapter.log.debug(`Creating ${stateName}`);
this.log.debug(`Creating ${stateName}`);
const obj = {
common: {
name: 'GPIO ' + port + ' direction',
Expand Down

0 comments on commit a1668d3

Please sign in to comment.