Not really tested

This commit is contained in:
Skylar Grant 2024-08-19 20:56:39 -04:00
parent 178bc1e115
commit f0687e4c2b
2 changed files with 25 additions and 27 deletions

View File

@ -22,15 +22,17 @@ module.exports = {
}); });
}, },
// Calls the GPIO Interface script to set a pin's state regardless of its current state // Calls the GPIO Interface script to set a pin's state regardless of its current state
setPin(pin, state, callback) { setPin(pin, state) {
return new Promise((resolve, reject) => {
exec(`python3 src/python/gpio_interface.py set ${pin} ${state}`, (error, stdout, stderr) => { exec(`python3 src/python/gpio_interface.py set ${pin} ${state}`, (error, stdout, stderr) => {
if (error) { if (error) {
callback(error); reject(error);
} }
if (stderr) { if (stderr) {
callback(new Error(stderr)); reject(new Error(stderr));
} }
callback(null); resolve();
}) })
});
} }
} }

View File

@ -21,32 +21,28 @@ module.exports = {
setDefaults() { setDefaults() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let stateChanges = []; let stateChanges = [];
for (const pin of pins) {
const promises = pins.map(pin => {
if (pin.mode === 'OUT') { if (pin.mode === 'OUT') {
gpio.setPin(pin.board, pin.defaultState, (err) => { return gpio.setPin(pin.board, pin.defaultState).then(() => {
if (err) reject(err);
stateChanges.push(`Set ${pin.key} pin to ${pin.defaultState}.`); 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 // Boot up sanity check during debug mode
debugInit() { async debugInit() {
module.exports.log('Resetting all output pins.'); module.exports.log('Resetting all output pins.');
module.exports.gpio.setDefaults().then(changes => { module.exports.gpio.setDefaults().then((changes) => {
module.exports.log(changes); module.exports.log(changes);
}).catch(e => console.error(e)); }).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) { for (const pin of pins) {
switch (pin.mode) { switch (pin.mode) {
case 'OUT': case 'OUT':