From 1ce82270f5983c697980e7d9138d3c29196b9cbd Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Fri, 16 Aug 2024 20:52:22 -0400 Subject: [PATCH] Testing better --- src/main.js | 99 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 81 insertions(+), 18 deletions(-) diff --git a/src/main.js b/src/main.js index 57d536e..081e606 100644 --- a/src/main.js +++ b/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 - } else { - togglePin(pins[index], (err) => { - if (err) { - process.exit(1); // Exit with error - } else { - setTimeout(() => toggleAllPins(pins, index + 1), 1000); // 1-second delay between toggles - } - }); +// 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 toggling pins -toggleAllPins(pins); \ No newline at end of file +// Start the process +controlPins();