Move timeout to a promise
This commit is contained in:
parent
32893467bb
commit
7afc662ba7
@ -1,20 +1,49 @@
|
|||||||
const { exec } = require('child_process');
|
const { exec } = require('child_process');
|
||||||
|
|
||||||
const pins = [
|
const pins = [
|
||||||
{ key: 'igniter', board: 13, bcm: 27, mode: 'OUT' },
|
{
|
||||||
{ key: 'exhaust', board: 15, bcm: 22, mode: 'OUT' },
|
key: 'igniter',
|
||||||
{ key: 'auger', board: 7, bcm: 4, mode: 'OUT' },
|
board: 13,
|
||||||
{ key: 'pof', board: 16, bcm: 23, mode: 'IN' },
|
bcm: 27,
|
||||||
{ key: 'vacuum', board: 22, bcm: 25, mode: 'IN' }
|
mode: 'OUT',
|
||||||
|
defaultState: 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'exhaust',
|
||||||
|
board: 15,
|
||||||
|
bcm: 22,
|
||||||
|
mode: 'OUT',
|
||||||
|
defaultState: 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'auger',
|
||||||
|
board: 7,
|
||||||
|
bcm: 4,
|
||||||
|
mode: 'OUT',
|
||||||
|
defaultState: 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'pof',
|
||||||
|
board: 16,
|
||||||
|
bcm: 23,
|
||||||
|
mode: 'IN'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'vacuum',
|
||||||
|
board: 22,
|
||||||
|
bcm: 25,
|
||||||
|
mode: 'IN' }
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
|
||||||
pins.forEach(pin => {
|
pins.forEach(pin => {
|
||||||
console.log(`Key: ${pin.key}, Board: ${pin.board}, BCM: ${pin.bcm}, Mode: ${pin.mode}`);
|
console.log(`Key: ${pin.key}, Board: ${pin.board}, BCM: ${pin.bcm}, Mode: ${pin.mode}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
// Function to toggle a pin on-then-off
|
// Calls the GPIO Interface script to toggle a pin's state opposite of its current state
|
||||||
togglePin(pin, callback) {
|
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) {
|
||||||
@ -23,26 +52,39 @@ module.exports = {
|
|||||||
if (stderr) {
|
if (stderr) {
|
||||||
return callback(new Error(stderr));
|
return callback(new Error(stderr));
|
||||||
}
|
}
|
||||||
console.log(`Successfully toggled pin ${pin}`);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
// Function to sense a pin state
|
// Calls the GPIO Interface script to read a pin's state
|
||||||
sensePin(pin, callback) {
|
readPin(pin, callback) {
|
||||||
exec(`python3 src/python/gpio_interface.py sense ${pin}`, (error, stdout, stderr) => {
|
exec(`python3 src/python/gpio_interface.py read ${pin}`, (error, stdout, stderr) => {
|
||||||
|
if (error) return callback(error);
|
||||||
|
if (stderr) return callback(new Error(stderr));
|
||||||
|
callback(null, stdout.trim());
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// Calls the GPIO Interface script to set a pin's state regardless of its current state
|
||||||
|
setPin(pin, state, callback) {
|
||||||
|
exec(`python 3 src/python/gpio_interface.py set ${pin} ${state}`, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(`Error sensing pin ${pin}: ${error.message}`);
|
|
||||||
return callback(error);
|
return callback(error);
|
||||||
}
|
}
|
||||||
if (stderr) {
|
if (stderr) {
|
||||||
console.error(`Stderr while sensing pin ${pin}: ${stderr}`);
|
|
||||||
return callback(new Error(stderr));
|
return callback(new Error(stderr));
|
||||||
}
|
}
|
||||||
console.log(`Pin ${pin} state: ${stdout.trim()}`);
|
callback(null);
|
||||||
callback(stdout.trim(), null);
|
})
|
||||||
});
|
|
||||||
},
|
},
|
||||||
// Toggle pins sequentially and then sense pin states
|
// Boot up sanity check during debug mode
|
||||||
debugInit() {
|
debugInit() {
|
||||||
|
console.log('Resetting all output pins.');
|
||||||
|
pins.forEach(async (pin) => {
|
||||||
|
if (pin.mode === 'OUT') {
|
||||||
|
this.setPin(pin.board, pin.defaultState, err => {
|
||||||
|
if (err) throw err;
|
||||||
|
console.log(`Set ${pin.key} pin to ${pin.defaultState}.`);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
});
|
||||||
pins.forEach(async (pin) => {
|
pins.forEach(async (pin) => {
|
||||||
switch (pin.mode) {
|
switch (pin.mode) {
|
||||||
case 'OUT':
|
case 'OUT':
|
||||||
@ -50,14 +92,13 @@ module.exports = {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
});
|
});
|
||||||
// Wait 1000ms before toggling again.
|
// Wait 1000ms before toggling again.
|
||||||
await setTimeout(() => {
|
await sleep(1000);
|
||||||
this.togglePin(pin.board, err => {
|
this.togglePin(pin.board, err => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
});
|
});
|
||||||
}, 1000);
|
|
||||||
break;
|
break;
|
||||||
case 'IN':
|
case 'IN':
|
||||||
this.sensePin(pin.board, (state, err) => {
|
this.readPin(pin.board, (err, state) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
console.log(`${pin.key} state: ${state}`);
|
console.log(`${pin.key} state: ${state}`);
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
import sys
|
import sys
|
||||||
import time
|
|
||||||
|
|
||||||
# Initialize GPIO using Board mode for pin numbering
|
# Initialize GPIO using Board mode for pin numbering
|
||||||
GPIO.setmode(GPIO.BOARD)
|
GPIO.setmode(GPIO.BOARD)
|
||||||
@ -27,22 +26,35 @@ def toggle_pin(pin):
|
|||||||
# Exit with 1 for failure
|
# Exit with 1 for failure
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
def sense_pin(pin):
|
def read_pin(pin):
|
||||||
setup_pin(pin, 'IN')
|
setup_pin(pin, 'IN')
|
||||||
try:
|
try:
|
||||||
# Sense pin state
|
# Read pin state
|
||||||
state = GPIO.input(pin)
|
state = GPIO.input(pin)
|
||||||
# Return 1 if pin is HIGH, 0 if LOW
|
# Return 1 if pin is HIGH, 0 if LOW
|
||||||
return 1 if state == GPIO.HIGH else 0
|
return 1 if state == GPIO.HIGH else 0
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Handle errors
|
# Handle errors
|
||||||
print(f"Error sensing pin {pin}: {e}")
|
print(f"Error reading pin {pin}: {e}")
|
||||||
# Return -1 on error
|
# Return -1 on error
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
def set_pin_state(pin, state):
|
||||||
|
setup_pin(pin, 'OUT')
|
||||||
|
try:
|
||||||
|
# Set the pin state to either HIGH (1) or LOW (0)
|
||||||
|
GPIO.output(pin, GPIO.HIGH if state == 1 else GPIO.LOW)
|
||||||
|
# Exit with 0 for success
|
||||||
|
return 0
|
||||||
|
except Exception as e:
|
||||||
|
# Handle errors
|
||||||
|
print(f"Error setting pin {pin} to state {state}: {e}")
|
||||||
|
# Exit with 1 for failure
|
||||||
|
return 1
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 3:
|
||||||
print("Usage: python3 gpio_interface.py <command> <pin>")
|
print("Usage: python3 gpio_interface.py <command> <pin> [<state>]")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
command = sys.argv[1].lower()
|
command = sys.argv[1].lower()
|
||||||
@ -51,13 +63,23 @@ def main():
|
|||||||
if command == "toggle":
|
if command == "toggle":
|
||||||
result = toggle_pin(pin)
|
result = toggle_pin(pin)
|
||||||
sys.exit(result)
|
sys.exit(result)
|
||||||
elif command == "sense":
|
elif command == "read":
|
||||||
result = sense_pin(pin)
|
result = read_pin(pin)
|
||||||
print(result)
|
print(result)
|
||||||
sys.exit(0 if result >= 0 else 1)
|
sys.exit(0 if result >= 0 else 1)
|
||||||
|
elif command == "set":
|
||||||
|
if len(sys.argv) < 4:
|
||||||
|
print("Usage: python3 gpio_interface.py set <pin> <state>")
|
||||||
|
sys.exit(1)
|
||||||
|
state = int(sys.argv[3])
|
||||||
|
if state not in [0, 1]:
|
||||||
|
print("Invalid state. Use 0 for LOW or 1 for HIGH.")
|
||||||
|
sys.exit(1)
|
||||||
|
result = set_pin_state(pin, state)
|
||||||
|
sys.exit(result)
|
||||||
else:
|
else:
|
||||||
print("Invalid command. Use 'toggle' or 'sense'.")
|
print("Invalid command. Use 'toggle', 'read', or 'set'.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user