Wait, is this peak ?
This commit is contained in:
@@ -11,7 +11,8 @@ grovepi.pinMode(button, "INPUT")
|
||||
button_callback = None
|
||||
|
||||
def read_button_state():
|
||||
if not grove_lock.acquire(timeout=0.05):
|
||||
# Increase timeout slightly so the button thread can wait for long I2C sensor reads to finish
|
||||
if not grove_lock.acquire(timeout=0.2):
|
||||
return None
|
||||
try:
|
||||
return grovepi.digitalRead(button)
|
||||
@@ -26,15 +27,18 @@ def monitor_button():
|
||||
last_button_state = button_switch_state
|
||||
|
||||
while True:
|
||||
time.sleep(0.04)
|
||||
|
||||
current_state = read_button_state()
|
||||
|
||||
if current_state is not None:
|
||||
# Rising edge detection (0 -> 1 transition)
|
||||
if current_state == 1 and last_button_state == 0:
|
||||
if button_callback:
|
||||
button_callback()
|
||||
last_button_state = current_state
|
||||
time.sleep(0.02) # Fast 20ms poll when lock is clear
|
||||
else:
|
||||
# Lock was busy; retry quickly without updating last_button_state
|
||||
time.sleep(0.01)
|
||||
|
||||
def start_button_monitoring_thread():
|
||||
threading.Thread(target=monitor_button, daemon=True).start()
|
||||
|
||||
Reference in New Issue
Block a user