You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
996 B
35 lines
996 B
# button.py mit lgpio für den Raspberry Pi 5
|
|
#! /usr/bin/python3
|
|
import lgpio
|
|
from time import sleep
|
|
import logging
|
|
|
|
|
|
__PIN__ = 23 # GPIO Pin, für den Taster
|
|
__WAIT__ = 0.5 # Warten für 0,5 Sekunden
|
|
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
|
|
|
if __name__ =='__main__':
|
|
handle = lgpio.gpiochip_open(0)
|
|
lgpio.gpio_claim_input(handle, __PIN__,lgpio.SET_PULL_UP) # Pin ist Input mit PullUp
|
|
|
|
pressed = False
|
|
|
|
logging.debug('Start der Schleife.')
|
|
try:
|
|
while True:
|
|
value = lgpio.gpio_read(handle, __PIN__)
|
|
|
|
pressed = value != 1
|
|
if pressed:
|
|
logging.info(f'Button wurde gedrückt {value}->{pressed}.')
|
|
else:
|
|
logging.debug(f'Button nicht gedrückt {value}->{pressed}.')
|
|
|
|
sleep(__WAIT__)
|
|
except KeyboardInterrupt:
|
|
logging.debug('Abbruch durch Benutzer.')
|
|
finally:
|
|
logging.debug('handle wird geschlossen.')
|
|
lgpio.gpiochip_close(handle) # Handle schließen
|
|
|
|
|