103 lines
3.2 KiB
JavaScript
103 lines
3.2 KiB
JavaScript
const { exec } = require('child_process');
|
|
|
|
// List of pins for toggling
|
|
const togglePins = [7, 13];
|
|
// List of pins for sensing
|
|
const sensePins = [16, 22];
|
|
|
|
// Function to toggle a pin on-then-off
|
|
function togglePin(pin, callback) {
|
|
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.error(`Error toggling pin ${pin}: ${error.message}`);
|
|
return callback(error);
|
|
}
|
|
if (stderr) {
|
|
console.error(`Stderr while toggling pin ${pin}: ${stderr}`);
|
|
return callback(new Error(stderr));
|
|
}
|
|
console.log(`Successfully toggled pin ${pin}`);
|
|
// Wait for 1 second before turning it off
|
|
setTimeout(() => {
|
|
exec(`python3 src/python/gpio_interface.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 src/python/gpio_interface.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);
|
|
});
|
|
}
|
|
|
|
// Toggle pins sequentially and then sense pin states
|
|
function controlPins() {
|
|
let index = 0;
|
|
|
|
// Toggle all pins
|
|
function toggleNextPin() {
|
|
if (index >= togglePins.length) {
|
|
console.log('All toggle pins processed.');
|
|
// Proceed to sensing pins
|
|
senseNextPin();
|
|
} 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 the process
|
|
controlPins();
|