From 4e4bf9f7ca2d021dd075a924043d4576f42b92b0 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Wed, 21 Aug 2024 22:16:32 -0400 Subject: [PATCH] Change togglePin to Promise+fix calling of toggle --- src/custom_modules/VoidGPIO.js | 16 +++++++--------- src/custom_modules/functions.js | 10 ++++------ src/main.js | 4 ++-- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/custom_modules/VoidGPIO.js b/src/custom_modules/VoidGPIO.js index baa4cdc..b52909c 100644 --- a/src/custom_modules/VoidGPIO.js +++ b/src/custom_modules/VoidGPIO.js @@ -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 diff --git a/src/custom_modules/functions.js b/src/custom_modules/functions.js index a272bca..af65c92 100644 --- a/src/custom_modules/functions.js +++ b/src/custom_modules/functions.js @@ -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 => { diff --git a/src/main.js b/src/main.js index adb8749..ef12670 100644 --- a/src/main.js +++ b/src/main.js @@ -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)); }); \ No newline at end of file