Testing better
This commit is contained in:
parent
9d084ee37a
commit
1ce82270f5
87
src/main.js
87
src/main.js
@ -1,9 +1,11 @@
|
||||
const { exec } = require('child_process');
|
||||
|
||||
// List of pins to toggle
|
||||
const pins = [7, 13, 15, 16, 18, 22];
|
||||
// List of pins for toggling
|
||||
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) {
|
||||
exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
@ -15,25 +17,86 @@ function togglePin(pin, callback) {
|
||||
return callback(new Error(stderr));
|
||||
}
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
// Toggle all pins
|
||||
function toggleAllPins(pins, index = 0) {
|
||||
if (index >= pins.length) {
|
||||
console.log('All pins toggled.');
|
||||
process.exit(0); // Exit successfully
|
||||
// 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(pins[index], (err) => {
|
||||
togglePin(togglePins[index], (err) => {
|
||||
if (err) {
|
||||
process.exit(1); // Exit with error
|
||||
} else {
|
||||
setTimeout(() => toggleAllPins(pins, index + 1), 1000); // 1-second delay between toggles
|
||||
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
|
||||
toggleAllPins(pins);
|
||||
// Start the process
|
||||
controlPins();
|
||||
|
Loading…
Reference in New Issue
Block a user