Update button_controller.py

Summary

Optimize GPIO button polling interval

Problem

Aggressive polling at 100 Hz (10ms interval) unnecessarily high for human button presses, causing elevated CPU usage with no benefit to responsiveness.

Changes

Changed default poll_interval_seconds from 0.01 to 0.05 (100 Hz → 20 Hz)
Updated ButtonRuntimeConfig dataclass default
Updated fallback default in constructor
This commit is contained in:
imonlinux
2026-01-10 13:26:19 -08:00
committed by GitHub
parent 67abca1111
commit 4b6b6dd380
+7 -4
View File
@@ -40,7 +40,9 @@ class ButtonRuntimeConfig:
enabled: bool
pin: int
long_press_seconds: float
poll_interval_seconds: float = 0.01 # 100 Hz polling
# CRITICAL FIX: Change default from 0.01s to 0.05s (100 Hz -> 20 Hz)
# 80% reduction in CPU usage for button monitoring
poll_interval_seconds: float = 0.05 # 20 Hz polling
class ButtonController:
@@ -73,6 +75,9 @@ class ButtonController:
self.event_bus = event_bus
self.state = state
# CRITICAL FIX: Default poll_interval_seconds to 0.05 instead of 0.01
default_poll_interval = 0.05
self._cfg = ButtonRuntimeConfig(
enabled=getattr(config, "enabled", False),
pin=getattr(config, "pin", 17),
@@ -80,9 +85,7 @@ class ButtonController:
getattr(config, "long_press_seconds", 1.0)
),
poll_interval_seconds=float(
getattr(config, "poll_interval_seconds", 0.01)
if hasattr(config, "poll_interval_seconds")
else 0.01
getattr(config, "poll_interval_seconds", default_poll_interval)
),
)