reflecting pull upa nd down for buttons
All checks were successful
Python CI / test (3.12) (push) Successful in 7m12s
Python CI / test (3.13) (push) Successful in 22m11s

This commit is contained in:
Duncan Tourolle 2025-11-23 14:40:47 +01:00
parent 5f38e3865e
commit 73102392dd
2 changed files with 26 additions and 9 deletions

View File

@ -43,6 +43,7 @@ class ButtonConfig:
gpio: int gpio: int
gesture: GestureType gesture: GestureType
description: str = "" description: str = ""
pull_up: bool = True # True = pull-up (button pulls LOW), False = pull-down (button pulls HIGH)
class GPIOButtonHandler: class GPIOButtonHandler:
@ -99,7 +100,8 @@ class GPIOButtonHandler:
logger.info(f"GPIO button handler created with {len(buttons)} buttons") logger.info(f"GPIO button handler created with {len(buttons)} buttons")
for btn in buttons: for btn in buttons:
logger.info(f" Button '{btn.name}' on GPIO {btn.gpio} -> {btn.gesture.value}") active_type = "active low" if btn.pull_up else "active high"
logger.info(f" Button '{btn.name}' on GPIO {btn.gpio} -> {btn.gesture.value} ({active_type})")
async def initialize(self): async def initialize(self):
"""Initialize GPIO pins and set up button callbacks.""" """Initialize GPIO pins and set up button callbacks."""
@ -125,15 +127,17 @@ class GPIOButtonHandler:
except Exception: except Exception:
pass # Ignore if no event detection was set pass # Ignore if no event detection was set
# Configure pin # Configure pin based on button's pull_up setting
if self.pull_up: if button.pull_up:
# Pull-up resistor: button pulls pin LOW when pressed
GPIO.setup(button.gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(button.gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Button pressed = LOW (0)
edge = GPIO.FALLING edge = GPIO.FALLING
logger.debug(f"Button '{button.name}' configured with pull-up (active low)")
else: else:
# Pull-down resistor: button pulls pin HIGH when pressed
GPIO.setup(button.gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(button.gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Button pressed = HIGH (1)
edge = GPIO.RISING edge = GPIO.RISING
logger.debug(f"Button '{button.name}' configured with pull-down (active high)")
# Add event detection with debounce # Add event detection with debounce
GPIO.add_event_detect( GPIO.add_event_detect(
@ -237,14 +241,22 @@ def load_button_config_from_dict(config: dict, screen_width: int = 1872, screen_
{ {
"gpio_buttons": { "gpio_buttons": {
"enabled": true, "enabled": true,
"pull_up": true, "pull_up": true, # Default pull_up for all buttons
"bounce_time_ms": 200, "bounce_time_ms": 200,
"buttons": [ "buttons": [
{ {
"name": "next_page", "name": "next_page",
"gpio": 23, "gpio": 23,
"gesture": "swipe_left", "gesture": "swipe_left",
"description": "Next page" "description": "Next page",
"pull_up": true # Optional: override per button (true = active low, false = active high)
},
{
"name": "power_off",
"gpio": 21,
"gesture": "long_press",
"description": "Power off",
"pull_up": false # Active high button
} }
] ]
} }
@ -261,6 +273,9 @@ def load_button_config_from_dict(config: dict, screen_width: int = 1872, screen_
return None return None
# Parse button configurations # Parse button configurations
# Get default pull_up from global config for backward compatibility
default_pull_up = gpio_config.get("pull_up", True)
buttons = [] buttons = []
for btn_cfg in gpio_config.get("buttons", []): for btn_cfg in gpio_config.get("buttons", []):
try: try:
@ -272,7 +287,8 @@ def load_button_config_from_dict(config: dict, screen_width: int = 1872, screen_
name=btn_cfg["name"], name=btn_cfg["name"],
gpio=btn_cfg["gpio"], gpio=btn_cfg["gpio"],
gesture=gesture, gesture=gesture,
description=btn_cfg.get("description", "") description=btn_cfg.get("description", ""),
pull_up=btn_cfg.get("pull_up", default_pull_up) # Per-button or global default
) )
buttons.append(button) buttons.append(button)

View File

@ -32,7 +32,8 @@
"gpio": 21, "gpio": 21,
"gesture": "long_press", "gesture": "long_press",
"description": "Power off button (long press to shutdown)", "description": "Power off button (long press to shutdown)",
"comment": "You may want to implement shutdown logic in the application" "pull_up": false,
"comment": "Active high button - pulls HIGH when pressed (unlike prev/next which pull LOW)"
} }
] ]
}, },