All Projects → liu2guang → MultiButton

liu2guang / MultiButton

Licence: other
A compact and easy to use event-driven button driver module. | 一个小巧易用的事件驱动按钮驱动模块.

Programming Languages

c
50402 projects - #5 most used programming language
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to MultiButton

dwin
基于RT-Thread物联网操作系统的dwin串口屏幕快速开发库!
Stars: ✭ 49 (-37.18%)
Mutual labels:  env, rtthread
libcsv
libcsv is a small, simple and fast CSV library written in pure ANSI C89 that can read and write CSV data. | libcsv是用纯ANSI C89编写的小型、简单、快速的CSV库,支持读写CSV数据.
Stars: ✭ 23 (-70.51%)
Mutual labels:  env, rtthread
wqbc
An R package for water quality thresholds and index calculation for British Columbia
Stars: ✭ 16 (-79.49%)
Mutual labels:  env
exenv
Exenv makes loading environment variables from external sources easy.
Stars: ✭ 35 (-55.13%)
Mutual labels:  env
envyable
The simplest yaml to ENV config loader.
Stars: ✭ 78 (+0%)
Mutual labels:  env
EnvReportBC
List of repositories administered by Environmental Reporting BC (www.gov.bc.ca/environmental-reporting-bc)
Stars: ✭ 17 (-78.21%)
Mutual labels:  env
checkdotenv
Verify environment variables presence for Node JS.
Stars: ✭ 12 (-84.62%)
Mutual labels:  env
tfenv
Transform environment variables for use with Terraform (e.g. `HOSTNAME` ⇨ `TF_VAR_hostname`)
Stars: ✭ 120 (+53.85%)
Mutual labels:  env
vite-plugin-environment
Easily expose environment variables in Vite.js
Stars: ✭ 57 (-26.92%)
Mutual labels:  env
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (-23.08%)
Mutual labels:  env
env-dot-prop
♻️ Get, set, or delete nested properties of process.env using a dot path
Stars: ✭ 31 (-60.26%)
Mutual labels:  env
env-diff
Env-diff is a lightweight library which sync your .env files with .env.dist by composer scripts, hooks or manual running
Stars: ✭ 24 (-69.23%)
Mutual labels:  env
microservice-template
📖 Nest.js based microservice repository template
Stars: ✭ 131 (+67.95%)
Mutual labels:  env
envman
Manage your .env configuration easily
Stars: ✭ 20 (-74.36%)
Mutual labels:  env
envclasses
envclasses is a library to map fields on dataclass object to environment variables.
Stars: ✭ 26 (-66.67%)
Mutual labels:  env
k8s-env-gen
Kubernetes environment generator makes docker env files compatible with kubernetes
Stars: ✭ 19 (-75.64%)
Mutual labels:  env
ptolemy
Elixir Application Environment Variable Management
Stars: ✭ 13 (-83.33%)
Mutual labels:  env
webpack-dotenv-plugin
Use dotenv with webpack.
Stars: ✭ 53 (-32.05%)
Mutual labels:  env
workbench
A hierarchical environment manager for bash, written in bash.
Stars: ✭ 17 (-78.21%)
Mutual labels:  env
dotenv validator
This gem check if required env variables are present and its format using the .env and .env.sample files from Dotenv.
Stars: ✭ 33 (-57.69%)
Mutual labels:  env

MultiButton

简介

MultiButton 是一个小巧简单易用的事件驱动型按键驱动模块,可无限量扩展按键,按键事件的回调异步处理方式可以简化你的程序结构,去除冗余的按键处理硬编码,让你的按键业务逻辑更清晰。

MultiButton的作者是0x1abin, github地址: https://github.com/0x1abin/MultiButton.

使用方法

1.先申请一个按键结构

struct Button button1;

2.初始化按键对象,绑定按键的GPIO电平读取接口read_button_pin() ,后一个参数设置有效触发电平

button_init(&button1, read_button_pin, 0);

3.注册按键事件

button_attach(&button1, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
button_attach(&button1, DOUBLE_CLICK, Callback_DOUBLE_Click_Handler);
...

4.启动按键

button_start(&button1);

5.设置一个5ms间隔的定时器循环调用后台处理函数

while(1) {
    ...
    if(timer_ticks == 5) {
        timer_ticks = 0;
        
        button_ticks();
    }
}

特性

MultiButton 使用C语言实现,基于面向对象方式设计思路,每个按键对象单独用一份数据结构管理:

struct Button {
	uint16_t ticks;
	uint8_t  repeat: 4;
	uint8_t  event : 4;
	uint8_t  state : 3;
	uint8_t  debounce_cnt : 3; 
	uint8_t  active_level : 1;
	uint8_t  button_level : 1;
	uint8_t  (*hal_button_Level)(void);
	BtnCallback  cb[number_of_event];
	struct Button* next;
};

这样每个按键使用单向链表相连,依次进入 button_handler(struct Button* handle) 状态机处理,所以每个按键的状态彼此独立。

按键事件

事件 说明
PRESS_DOWN 按键按下,每次按下都触发
PRESS_UP 按键弹起,每次松开都触发
PRESS_REPEAT 重复按下触发,变量repeat计数连击次数
SINGLE_CLICK 单击按键事件
DOUBLE_CLICK 双击按键事件
LONG_PRESS_START 达到长按时间阈值时触发一次
LONG_PRESS_HOLD 长按期间一直触发

Examples

#include "button.h"

struct Button btn1;

uint8_t read_button1_GPIO() 
{
	return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
}

void BTN1_PRESS_DOWN_Handler(void* btn)
{
	//do something...
}
void BTN1_PRESS_UP_Handler(void* btn)
{
	//do something...
}

int main()
{
	button_init(&btn1, read_button1_GPIO, 0);
	button_attach(&btn1, PRESS_DOWN,       BTN1_PRESS_DOWN_Handler);
	button_attach(&btn1, PRESS_UP,         BTN1_PRESS_UP_Handler);
	button_attach(&btn1, PRESS_REPEAT,     BTN1_PRESS_REPEAT_Handler);
	button_attach(&btn1, SINGLE_CLICK,     BTN1_SINGLE_Click_Handler);
	button_attach(&btn1, DOUBLE_CLICK,     BTN1_DOUBLE_Click_Handler);
	button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
	button_attach(&btn2, LONG_PRESS_HOLD,  BTN1_LONG_PRESS_HOLD_Handler);
	button_start(&btn1);
	
	//make the timer invoking the button_ticks() interval 5ms.
	//This function is implemented by yourself.
	__timer_start(button_ticks, 0, 5); 
	
	while(1) 
	{}
}

...

状态图

states.png

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].