Files
board-mate/esp32-wifi/button_handler.py
2025-12-30 23:54:17 +01:00

29 lines
720 B
Python

import _thread
from machine import Pin
import time
class ButtonHandler:
def __init__(self, pin , action):
self.button = Pin(pin, Pin.IN, Pin.PULL_UP)
self.action = action
self._pressed = False
_thread.start_new_thread(self._listen, ())
def _listen(self):
while True:
if not self.button.value():
if not self._pressed:
self._pressed = True
self.action()
else:
self._pressed = False
time.sleep(0.1)
if __name__ == "__main__":
def on_press():
print("Button action triggered")
btn = ButtonHandler(14, on_press)
while True:
time.sleep(1)