add button event and screeens
This commit is contained in:
63
test_button_simple.py
Normal file
63
test_button_simple.py
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple polling-based button test for GPIO6
|
||||
No edge detection - just polls the pin state
|
||||
"""
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
import signal
|
||||
import sys
|
||||
|
||||
BUTTON_PIN = 6
|
||||
|
||||
def signal_handler(signum, frame):
|
||||
print("Cleaning up GPIO...")
|
||||
GPIO.cleanup()
|
||||
sys.exit(0)
|
||||
|
||||
def main():
|
||||
print("Simple Button Test - Polling GPIO6")
|
||||
print("Press the button connected to GPIO6 (GND when pressed)")
|
||||
print("Press Ctrl+C to exit")
|
||||
|
||||
# Clean up any existing GPIO setup
|
||||
try:
|
||||
GPIO.cleanup()
|
||||
except:
|
||||
pass
|
||||
|
||||
# Set up GPIO
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
|
||||
# Set up signal handler
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
|
||||
print(f"GPIO{BUTTON_PIN} setup complete. Current state: {GPIO.input(BUTTON_PIN)}")
|
||||
print("Monitoring for button presses (1=released, 0=pressed)...")
|
||||
|
||||
last_state = GPIO.input(BUTTON_PIN)
|
||||
button_press_count = 0
|
||||
|
||||
try:
|
||||
while True:
|
||||
current_state = GPIO.input(BUTTON_PIN)
|
||||
|
||||
# Detect falling edge (button press)
|
||||
if last_state == 1 and current_state == 0:
|
||||
button_press_count += 1
|
||||
print(f"Button press #{button_press_count} detected!")
|
||||
time.sleep(0.5) # Simple debounce
|
||||
|
||||
last_state = current_state
|
||||
time.sleep(0.01) # Poll every 10ms
|
||||
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
GPIO.cleanup()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user