digipi/home/pi/gpio_redled.py

47 wiersze
1.1 KiB
Python
Czysty Zwykły widok Historia

2022-05-18 14:44:13 +00:00
#!/usr/bin/python3
import pyinotify
2024-01-05 18:33:27 +00:00
#import RPi.GPIO as GPIO
from gpiozero import LED
2022-05-18 14:44:13 +00:00
import os.path
from time import sleep
2024-01-05 18:33:27 +00:00
#GPIO.setwarnings(False)
2022-05-18 14:44:13 +00:00
def handle_change(cb):
with open('/sys/class/gpio/gpio12/value', 'r') as f:
status = f.read(1)
if status == '0':
2024-01-05 18:33:27 +00:00
#print("OFF\n")
led26.off()
2022-05-18 14:44:13 +00:00
else:
2024-01-05 18:33:27 +00:00
#print("RED!\n")
led26.on()
2022-05-18 14:44:13 +00:00
f.close
def null_function(junk): # default callback prints tons of debugging info
return()
# wait for something to initialize PTT gpio pin
while not os.path.exists("/sys/class/gpio/gpio12/value"):
sleep(5)
print("Detected GPIO12 initialization. Mirroring state to GPIO26.")
2024-01-05 18:33:27 +00:00
led26 = LED(26)
led12 = LED(12)
2022-05-18 14:44:13 +00:00
# Instanciate a new WatchManager (will be used to store watches).
wm = pyinotify.WatchManager()
# Associate this WatchManager with a Notifier
notifier = pyinotify.Notifier(wm, default_proc_fun=null_function)
# Add a new watch
wm.add_watch('/sys/class/gpio/gpio12/value', pyinotify.IN_MODIFY)
# Loop forever and handle events.
notifier.loop(callback=handle_change)