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
gesture: GestureType
description: str = ""
pull_up: bool = True # True = pull-up (button pulls LOW), False = pull-down (button pulls HIGH)
class GPIOButtonHandler:
@ -99,7 +100,8 @@ class GPIOButtonHandler:
logger.info(f"GPIO button handler created with {len(buttons)} 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):
"""Initialize GPIO pins and set up button callbacks."""
@ -125,15 +127,17 @@ class GPIOButtonHandler:
except Exception:
pass # Ignore if no event detection was set
# Configure pin
if self.pull_up:
# Configure pin based on button's pull_up setting
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)
# Button pressed = LOW (0)
edge = GPIO.FALLING
logger.debug(f"Button '{button.name}' configured with pull-up (active low)")
else:
# Pull-down resistor: button pulls pin HIGH when pressed
GPIO.setup(button.gpio, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Button pressed = HIGH (1)
edge = GPIO.RISING
logger.debug(f"Button '{button.name}' configured with pull-down (active high)")
# Add event detection with debounce
GPIO.add_event_detect(
@ -237,14 +241,22 @@ def load_button_config_from_dict(config: dict, screen_width: int = 1872, screen_
{
"gpio_buttons": {
"enabled": true,
"pull_up": true,
"pull_up": true, # Default pull_up for all buttons
"bounce_time_ms": 200,
"buttons": [
{
"name": "next_page",
"gpio": 23,
"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
# Parse button configurations
# Get default pull_up from global config for backward compatibility
default_pull_up = gpio_config.get("pull_up", True)
buttons = []
for btn_cfg in gpio_config.get("buttons", []):
try:
@ -272,7 +287,8 @@ def load_button_config_from_dict(config: dict, screen_width: int = 1872, screen_
name=btn_cfg["name"],
gpio=btn_cfg["gpio"],
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)

View File

@ -32,7 +32,8 @@
"gpio": 21,
"gesture": "long_press",
"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)"
}
]
},