gpio trigger script
This commit is contained in:
38
gpio.py
Normal file
38
gpio.py
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
import subprocess
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
# pins whose states will form the binary number
|
||||
PINS = [17, 27, 9, 22] # MSB → LSB
|
||||
|
||||
for p in PINS:
|
||||
try:
|
||||
GPIO.setup(p, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||
except RuntimeError as e:
|
||||
sys.stderr.write(f"Pin {p} init error: {e}\n")
|
||||
GPIO.cleanup()
|
||||
sys.exit(1)
|
||||
|
||||
# read pins, build integer (bit 3 = pin 17, bit 2 = pin 27, bit 1 = pin 22, bit 0 = pin 10)
|
||||
old_value=0
|
||||
while(True):
|
||||
value = 0
|
||||
for i, p in enumerate(PINS):
|
||||
level = GPIO.input(p)
|
||||
value = (value << 1) | (1 if level else 0)
|
||||
|
||||
if(value != old_value):
|
||||
#print(f"GPIO states [{', '.join(str(p) for p in PINS)}] -> 0b{value:04b} ({value})")
|
||||
print(str(value))
|
||||
command = "nc -N localhost 12345 < {}.dat".format(value)
|
||||
subprocess.run(command, shell=True, capture_output=False, text=True)
|
||||
|
||||
old_value = value
|
||||
time.sleep(0.1)
|
||||
|
||||
|
||||
GPIO.cleanup()
|
||||
Reference in New Issue
Block a user