Change togglePin to Promise+fix calling of toggle
This commit is contained in:
parent
4f35efcf79
commit
4e4bf9f7ca
@ -2,15 +2,13 @@ const { exec } = require('child_process');
|
||||
|
||||
module.exports = {
|
||||
// Calls the GPIO Interface script to toggle a pin's state opposite of its current state
|
||||
togglePin(pin, callback) {
|
||||
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
callback(error);
|
||||
}
|
||||
if (stderr) {
|
||||
callback(new Error(stderr));
|
||||
}
|
||||
callback(null);
|
||||
togglePin(pin) {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
|
||||
if (error) reject(error);
|
||||
if (stderr) reject(new Error(stderr));
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
// Calls the GPIO Interface script to read a pin's state
|
||||
|
@ -47,16 +47,14 @@ module.exports = {
|
||||
for (const pin of pins) {
|
||||
switch (pin.mode) {
|
||||
case 'OUT':
|
||||
gpio.togglePin(pin.board, err => {
|
||||
if (err) throw err;
|
||||
gpio.togglePin(pin.board).then(() => {
|
||||
module.exports.log(`Toggled ${pin.key}`);
|
||||
});
|
||||
}).catch(e => console.error(e));
|
||||
// Wait 1000ms before toggling again.
|
||||
await module.exports.sleep(1000);
|
||||
gpio.togglePin(pin.board, err => {
|
||||
if (err) throw err;
|
||||
gpio.togglePin(pin.board).then(() => {
|
||||
module.exports.log(`Toggled ${pin.key}`);
|
||||
});
|
||||
}).catch(e => console.error(e));
|
||||
break;
|
||||
case 'IN':
|
||||
gpio.readPin(pin.board).then(state => {
|
||||
|
@ -24,7 +24,7 @@ setInterval(() => {
|
||||
|
||||
comms.on('stateChange', (change) => {
|
||||
console.log(`State change detected: ${change.name}`);
|
||||
gpio.togglePin(config.states[change.name].pin, change.on).then(() => {
|
||||
gpio.togglePin(config.states[change.name].board).then(() => {
|
||||
console.log(`Pin ${config.states[change.name].pin} set to ${change.on}`);
|
||||
});
|
||||
}).catch(e => console.error(e));
|
||||
});
|
Loading…
Reference in New Issue
Block a user