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 = {
|
module.exports = {
|
||||||
// Calls the GPIO Interface script to toggle a pin's state opposite of its current state
|
// Calls the GPIO Interface script to toggle a pin's state opposite of its current state
|
||||||
togglePin(pin, callback) {
|
togglePin(pin) {
|
||||||
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (error) {
|
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
|
||||||
callback(error);
|
if (error) reject(error);
|
||||||
}
|
if (stderr) reject(new Error(stderr));
|
||||||
if (stderr) {
|
resolve();
|
||||||
callback(new Error(stderr));
|
});
|
||||||
}
|
|
||||||
callback(null);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
// Calls the GPIO Interface script to read a pin's state
|
// Calls the GPIO Interface script to read a pin's state
|
||||||
|
@ -47,16 +47,14 @@ module.exports = {
|
|||||||
for (const pin of pins) {
|
for (const pin of pins) {
|
||||||
switch (pin.mode) {
|
switch (pin.mode) {
|
||||||
case 'OUT':
|
case 'OUT':
|
||||||
gpio.togglePin(pin.board, err => {
|
gpio.togglePin(pin.board).then(() => {
|
||||||
if (err) throw err;
|
|
||||||
module.exports.log(`Toggled ${pin.key}`);
|
module.exports.log(`Toggled ${pin.key}`);
|
||||||
});
|
}).catch(e => console.error(e));
|
||||||
// Wait 1000ms before toggling again.
|
// Wait 1000ms before toggling again.
|
||||||
await module.exports.sleep(1000);
|
await module.exports.sleep(1000);
|
||||||
gpio.togglePin(pin.board, err => {
|
gpio.togglePin(pin.board).then(() => {
|
||||||
if (err) throw err;
|
|
||||||
module.exports.log(`Toggled ${pin.key}`);
|
module.exports.log(`Toggled ${pin.key}`);
|
||||||
});
|
}).catch(e => console.error(e));
|
||||||
break;
|
break;
|
||||||
case 'IN':
|
case 'IN':
|
||||||
gpio.readPin(pin.board).then(state => {
|
gpio.readPin(pin.board).then(state => {
|
||||||
|
@ -24,7 +24,7 @@ setInterval(() => {
|
|||||||
|
|
||||||
comms.on('stateChange', (change) => {
|
comms.on('stateChange', (change) => {
|
||||||
console.log(`State change detected: ${change.name}`);
|
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}`);
|
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