https://github.com/funnygeeker/circuitpython-easybutton
Multi-button state recognition implemented using loops, suitable for CircuitPython 使用循环实现的多种按钮状态识别,适用于 CircuitPython
https://github.com/funnygeeker/circuitpython-easybutton
button circuitpython esp32 esp32-c3 esp32-s3 esp8266 loop rp2040
Last synced: about 2 months ago
JSON representation
Multi-button state recognition implemented using loops, suitable for CircuitPython 使用循环实现的多种按钮状态识别,适用于 CircuitPython
- Host: GitHub
- URL: https://github.com/funnygeeker/circuitpython-easybutton
- Owner: funnygeeker
- License: mit
- Created: 2024-01-03T13:28:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-03T13:50:29.000Z (over 1 year ago)
- Last Synced: 2025-02-12T21:19:11.571Z (3 months ago)
- Topics: button, circuitpython, esp32, esp32-c3, esp32-s3, esp8266, loop, rp2040
- Language: Python
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.ZH-CN.md
- License: LICENSE
Awesome Lists containing this project
README
[English (英语)](./README.md)
# circuitpython-easybutton
- 使用循环实现的多种按钮状态识别,按钮按下时执行指定函数,适用于 `circuitpython`### 功能
- 按钮按下后,每隔一段时间执行一次函数
- 按钮短按后,松开时执行函数
- 按钮长按后,松开时执行函数
- 按钮按下时执行函数
- 按钮松开时执行函数### 说明
`./main.py` 为使用示例文件
`./lib/easybutton.py` 为按钮库文件### 示例
- 在本次示例中,按钮所在的引脚接按钮,按钮接的是 `GND````python
import board
import digitalio
from lib.easybutton import EasyButton, Button# 配置按钮引脚
pin = digitalio.DigitalInOut(board.GP0)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.DOWN# Define functions, they can be defined first or later used as anonymous functions
# 定义函数,可以先定义,也可以后续使用匿名函数
def test():
print("^ UP ^")b = Button(pin)
# Set trigger functions for the button
b.up_func = test # Executed when the button is released # 按钮松开时执行函数
b.down_func = lambda: print("v DOWN v") # Executed when the button is pressed # 按钮按下时执行
b.long_func = lambda: print("+ LONG +") # Executed when the button is long pressed and released # 按钮长按后,松开时执行
b.hold_func = lambda: print("| HOLD |") # Executed at regular intervals after the button is pressed # 按钮按下后,每隔一段时间执行一次
b.short_func = (print, "- SHORT -") # Executed when the button is short pressed and released # 按钮短按后,松开时执行
eb = EasyButton(interval=100)
eb.add(b)
eb.run()
```