hestia/src/main.js

103 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-08-17 00:41:12 +00:00
const { exec } = require('child_process');
2024-08-17 00:52:22 +00:00
// List of pins for toggling
const togglePins = [7, 13];
// List of pins for sensing
const sensePins = [16, 22];
2024-08-17 00:41:12 +00:00
2024-08-17 00:52:22 +00:00
// Function to toggle a pin on-then-off
2024-08-17 00:41:12 +00:00
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}`);
2024-08-17 00:52:22 +00:00
// Wait for 1 second before turning it off
setTimeout(() => {
2024-08-17 00:53:12 +00:00
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
2024-08-17 00:52:22 +00:00
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) {
2024-08-17 00:53:12 +00:00
exec(`python3 src/python/gpio_interface.py sense ${pin}`, (error, stdout, stderr) => {
2024-08-17 00:52:22 +00:00
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()}`);
2024-08-17 00:41:12 +00:00
callback(null);
});
}
2024-08-17 00:52:22 +00:00
// 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
2024-08-17 00:41:12 +00:00
} else {
2024-08-17 00:52:22 +00:00
sensePin(sensePins[index], (err) => {
if (err) {
process.exit(1); // Exit with error
} else {
index++;
sensePinNext();
}
});
2024-08-17 00:41:12 +00:00
}
2024-08-17 00:52:22 +00:00
}
sensePinNext();
2024-08-17 00:41:12 +00:00
}
2024-08-17 00:52:22 +00:00
// Start with toggling pins
toggleNextPin();
2024-08-17 00:41:12 +00:00
}
2024-08-17 00:52:22 +00:00
// Start the process
controlPins();