From 73102392ddb71637f408f2d2f9d547d40c9392dc Mon Sep 17 00:00:00 2001 From: Duncan Tourolle Date: Sun, 23 Nov 2025 14:40:47 +0100 Subject: [PATCH] reflecting pull upa nd down for buttons --- dreader/gpio_buttons.py | 32 ++++++++++++++++++++++++-------- hardware_config.json | 3 ++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/dreader/gpio_buttons.py b/dreader/gpio_buttons.py index 8ab2d59..cfa8c84 100644 --- a/dreader/gpio_buttons.py +++ b/dreader/gpio_buttons.py @@ -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) diff --git a/hardware_config.json b/hardware_config.json index 7a3ba69..224c20c 100644 --- a/hardware_config.json +++ b/hardware_config.json @@ -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)" } ] },