From f0687e4c2b5f7c543f6ffee8cc7fd63e5e8f46de Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Mon, 19 Aug 2024 20:56:39 -0400 Subject: [PATCH] Not really tested --- src/custom_modules/VoidGPIO.js | 22 ++++++++++++---------- src/custom_modules/functions.js | 30 +++++++++++++----------------- 2 files changed, 25 insertions(+), 27 deletions(-) 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':