diff --git a/src/custom_modules/VoidGPIO.js b/src/custom_modules/VoidGPIO.js index 69ef74a..5c50031 100644 --- a/src/custom_modules/VoidGPIO.js +++ b/src/custom_modules/VoidGPIO.js @@ -22,15 +22,17 @@ module.exports = { }); }, // Calls the GPIO Interface script to set a pin's state regardless of its current state - setPin(pin, state, callback) { - exec(`python3 src/python/gpio_interface.py set ${pin} ${state}`, (error, stdout, stderr) => { - if (error) { - callback(error); - } - if (stderr) { - callback(new Error(stderr)); - } - callback(null); - }) + setPin(pin, state) { + return new Promise((resolve, reject) => { + exec(`python3 src/python/gpio_interface.py set ${pin} ${state}`, (error, stdout, stderr) => { + if (error) { + reject(error); + } + if (stderr) { + reject(new Error(stderr)); + } + resolve(); + }) + }); } } \ No newline at end of file diff --git a/src/custom_modules/functions.js b/src/custom_modules/functions.js index 0c50238..09f9a8b 100644 --- a/src/custom_modules/functions.js +++ b/src/custom_modules/functions.js @@ -21,32 +21,28 @@ module.exports = { setDefaults() { return new Promise((resolve, reject) => { let stateChanges = []; - for (const pin of pins) { + + const promises = pins.map(pin => { if (pin.mode === 'OUT') { - gpio.setPin(pin.board, pin.defaultState, (err) => { - if (err) reject(err); - stateChanges.push( `Set ${pin.key} pin to ${pin.defaultState}.`); - }) + return gpio.setPin(pin.board, pin.defaultState).then(() => { + stateChanges.push(`Set ${pin.key} pin to ${pin.defaultState}.`); + }).catch(e => console.error(e)); } - } - resolve(stateChanges.join('\n')); + }); + + Promise.all(promises).then(() => { + const changes = stateChanges.join('\n'); + resolve(changes); + }).catch(reject); }); }, // Boot up sanity check during debug mode - debugInit() { + async debugInit() { module.exports.log('Resetting all output pins.'); - module.exports.gpio.setDefaults().then(changes => { + module.exports.gpio.setDefaults().then((changes) => { module.exports.log(changes); }).catch(e => console.error(e)); - pins.forEach(pin => { - if (pin.mode === 'OUT') { - gpio.setPin(pin.board, pin.defaultState, err => { - if (err) throw err; - module.exports.log(); - }); - }; - }); for (const pin of pins) { switch (pin.mode) { case 'OUT':