This commit is contained in:
Skylar Grant 2024-08-18 13:14:28 -04:00
parent 1413decbe0
commit f596ea1bda

View File

@ -13,55 +13,55 @@ pins.forEach(pin => {
}); });
// Function to toggle a pin on-then-off module.exports = {
export function togglePin(pin, callback) { // Function to toggle a pin on-then-off
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => { togglePin(pin, callback) {
if (error) { exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
return callback(error); if (error) {
} return callback(error);
if (stderr) { }
return callback(new Error(stderr)); if (stderr) {
} return callback(new Error(stderr));
console.log(`Successfully toggled pin ${pin}`); }
}); console.log(`Successfully toggled pin ${pin}`);
} });
}.
// Function to sense a pin state // Function to sense a pin state
export function sensePin(pin, callback) { sensePin(pin, callback) {
exec(`python3 src/python/gpio_interface.py sense ${pin}`, (error, stdout, stderr) => { exec(`python3 src/python/gpio_interface.py sense ${pin}`, (error, stdout, stderr) => {
if (error) { if (error) {
console.error(`Error sensing pin ${pin}: ${error.message}`); console.error(`Error sensing pin ${pin}: ${error.message}`);
return callback(error); return callback(error);
} }
if (stderr) { if (stderr) {
console.error(`Stderr while sensing pin ${pin}: ${stderr}`); console.error(`Stderr while sensing pin ${pin}: ${stderr}`);
return callback(new Error(stderr)); return callback(new Error(stderr));
} }
console.log(`Pin ${pin} state: ${stdout.trim()}`); console.log(`Pin ${pin} state: ${stdout.trim()}`);
callback(stdout.trim(), null); callback(stdout.trim(), null);
}); });
} },
// Toggle pins sequentially and then sense pin states
// Toggle pins sequentially and then sense pin states debugInit() {
export function debugInit() { pins.forEach(async (pin) => {
pins.forEach(async (pin) => { switch (pin.mode) {
switch (pin.mode) { case 'OUT':
case 'OUT': togglePin(pin.board, err => {
togglePin(pin.board, err => { if (err) throw err;
if (err) throw err; });
}); // Wait 1000ms before toggling again.
// Wait 1000ms before toggling again. setTimeout(togglePin(pin.board, err => {
setTimeout(togglePin(pin.board, err => { if (err) throw err;
if (err) throw err; }), 1000);
}), 1000); break;
break; case 'IN':
case 'IN': sensePin(pin.board, (state, err) => {
sensePin(pin.board, (state, err) => { if (err) throw err;
if (err) throw err; console.log(`${pin.key} state: ${state}`);
console.log(`${pin.key} state: ${state}`); });
}); default:
default: break;
break; }
} });
}); }
} }