Testing better

This commit is contained in:
Skylar Grant 2024-08-16 20:52:22 -04:00
parent 9d084ee37a
commit 1ce82270f5

View File

@ -1,9 +1,11 @@
const { exec } = require('child_process'); const { exec } = require('child_process');
// List of pins to toggle // List of pins for toggling
const pins = [7, 13, 15, 16, 18, 22]; const togglePins = [7, 13];
// List of pins for sensing
const sensePins = [16, 22];
// Function to toggle a pin // Function to toggle a pin on-then-off
function togglePin(pin, callback) { function togglePin(pin, callback) {
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => { exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
if (error) { if (error) {
@ -15,25 +17,86 @@ function togglePin(pin, callback) {
return callback(new Error(stderr)); return callback(new Error(stderr));
} }
console.log(`Successfully toggled pin ${pin}`); console.log(`Successfully toggled pin ${pin}`);
// Wait for 1 second before turning it off
setTimeout(() => {
exec(`python3 gpio_control.py toggle ${pin}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error turning off pin ${pin}: ${error.message}`);
return callback(error);
}
if (stderr) {
console.error(`Stderr while turning off pin ${pin}: ${stderr}`);
return callback(new Error(stderr));
}
console.log(`Successfully turned off pin ${pin}`);
callback(null);
});
}, 1000); // 1-second delay to ensure the pin is toggled
});
}
// Function to sense a pin state
function sensePin(pin, callback) {
exec(`python3 gpio_control.py sense ${pin}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error sensing pin ${pin}: ${error.message}`);
return callback(error);
}
if (stderr) {
console.error(`Stderr while sensing pin ${pin}: ${stderr}`);
return callback(new Error(stderr));
}
console.log(`Pin ${pin} state: ${stdout.trim()}`);
callback(null); callback(null);
}); });
} }
// Toggle all pins // Toggle pins sequentially and then sense pin states
function toggleAllPins(pins, index = 0) { function controlPins() {
if (index >= pins.length) { let index = 0;
console.log('All pins toggled.');
process.exit(0); // Exit successfully // Toggle all pins
} else { function toggleNextPin() {
togglePin(pins[index], (err) => { if (index >= togglePins.length) {
if (err) { console.log('All toggle pins processed.');
process.exit(1); // Exit with error // Proceed to sensing pins
} else { senseNextPin();
setTimeout(() => toggleAllPins(pins, index + 1), 1000); // 1-second delay between toggles } else {
} togglePin(togglePins[index], (err) => {
}); if (err) {
process.exit(1); // Exit with error
} else {
index++;
toggleNextPin();
}
});
}
} }
// Sense all pins
function senseNextPin() {
index = 0;
function sensePinNext() {
if (index >= sensePins.length) {
console.log('All sense pins processed.');
process.exit(0); // Exit successfully
} else {
sensePin(sensePins[index], (err) => {
if (err) {
process.exit(1); // Exit with error
} else {
index++;
sensePinNext();
}
});
}
}
sensePinNext();
}
// Start with toggling pins
toggleNextPin();
} }
// Start toggling pins // Start the process
toggleAllPins(pins); controlPins();