Skip to content

Latest commit

 

History

History
49 lines (41 loc) · 2.3 KB

README.md

File metadata and controls

49 lines (41 loc) · 2.3 KB

简体中文 (Chinese)

micropython-easybutton

  • Implements various button state recognition using interrupts, executes specified functions when the button is pressed, suitable for micropython.

Features

  • Execute a specified function at regular intervals after the button is pressed
  • Execute a function when the button is released after a short press
  • Execute a function when the button is released after a long press
  • Execute a function when the button is pressed
  • Execute a function when the button is released

Notes

  • ./main.py is the example file
  • ./lib/easybutton.py is the button library file

Example

  • In this example, the button is connected to a pin and the other end is connected to GND
import time
from machine import Pin
from lib.easybutton import EasyButton

# Initialize the button
btn = Pin(2, Pin.IN, Pin.PULL_UP)
# if there are any non-standard naming, please submit a PR, thanks.
# 如果有不规范的命名,请提交 PR,谢谢
b = EasyButton(btn)

# Define functions, they can be defined first or later used as anonymous functions
# 定义函数,可以先定义,也可以后续使用匿名函数
def test():
    print("up")

# Set trigger functions for the button
b.down_func = lambda: print("down")  # Executed when the button is pressed  # 按钮按下时执行
b.hold_func = lambda: print("hold")  # Executed at regular intervals after the button is pressed  # 按钮按下后,每隔一段时间执行一次
b.short_func = lambda: print("short")  # Executed when the button is short pressed and released  # 按钮短按后,松开时执行
b.long_func = (print, "long")  # Executed when the button is long pressed and released  # 按钮长按后,松开时执行
b.up_func = test  # Executed when the button is released  # 按钮松开时执行函数

# Since an interrupt is used, the code can continue to execute, and it will only pause when the button is pressed.
# 由于使用了中断,所以后续可以继续执行代码,只有在按钮被按下时才会暂停继续执行的代码,松开则恢复
# This loop is not necessary, it's just for demonstration purposes. It's not necessary in actual use.
# 这里的循环并不是必须,仅为演示使用,实际使用中并非必要
while True:
    print("---- running ----")
    time.sleep(1)