Pure key actuation written in C language, the key event processing logic and separate key ~

button drive

JJ himself wrote a key driver to support single-double-click, double-click, press; using a callback Handling key events (custom debounce time), using the 3-step, create a key, key events and callback handler link map, cycle check button.

Source Address: https://github.com/jiejieTop/ButtonDrive . Author: JJ

Foreword

A few days ago wrote a key driver, with reference to the MulitButton usage data structure, logic implementation is not the same.
Here to thank all of the open source developers, so I learned a lot, but the network is also a good platform, but also hope that all developers can create a virtuous circle of knowledge to the school network, from the back to the network. Thanks MulitButton author 0x1abin , thanks to the two chiefs rtt: Grand Master , streamer .

Button_drive Profile

Button_drive is a small key actuation, support click, double click, press, continuous triggering (subsequent trigger event may be added in the key control block), can be theoretically unlimited extension Button, Button_drive triggering event callback mode using the key business process logic, support the use of RTOS, I currently only in the RT-Thread tested on.
Write key driving purpose is to want to user keys and key processing logic separate incident, users do not need complex event processing logic trouble.

Button_drive use effect

  1. Click the Press

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-2nhPnwqF-1571148176402) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/1 .png? raw = true)]

  1. Double-click

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-wtGvjTjq-1571148176403) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/2 .png? raw = true)]

  1. Double-click

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-QUypOh2s-1571148176403) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/3 .png? raw = true)]

  1. Double-click release

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-LfsbuM1W-1571148176404) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/4 .png? raw = true)]

Instructions

  1. Creating a key handle
Button_t Button1;
Button_t Button2; 
  1. Creating a key, initialization key information, including the name of the key, the key level detection function interface, key trigger level.
  Button_Create("Button1",              //按键名字
                &Button1,               //按键句柄
                Read_Button1_Level,     //按键电平检测函数接口
                BTN_TRIGGER);           //触发电平
                
                ......
  1. Button trigger event and the event callback link map, when the key event to be triggered automatically jump callback processing business logic.
  Button_Attach(&Button1,BUTTON_DOWM,Btn2_Dowm_CallBack);       //按键单击
  Button_Attach(&Button1,BUTTON_DOUBLE,Btn2_Double_CallBack);   //双击
  Button_Attach(&Button1,BUTTON_LONG,Btn2_Long_CallBack);       //长按
                
                .......
  1. Cycle calls the callback button handler can recommend call cycles 20-50ms.
Button_Process();     //需要周期调用按键处理函数

Users need to achieve 2 functions:

  • Level detection key interfaces:
uint8_t Read_Button1_Level(void)
{
  return GPIO_ReadInputDataBit(BTN1_GPIO_PORT,BTN1_GPIO_PIN);
}

uint8_t Read_Button2_Level(void)
{
  return GPIO_ReadInputDataBit(BTN2_GPIO_PORT,BTN2_GPIO_PIN);
}

// 这是我在stm32上简单测试的伪代码,以实际源码为准
  • Key logic
void Btn1_Dowm_CallBack(void *btn)
{
  PRINT_INFO("Button1 单击!");
}

void Btn1_Double_CallBack(void *btn)
{
  PRINT_INFO("Button1 双击!");
}

void Btn1_Long_CallBack(void *btn)
{
  PRINT_INFO("Button1 长按!");
  
  Button_Delete(&Button2);
  PRINT_INFO("删除Button1");
  Search_Button();
}

Feature

Button_drive open source control key and the data structure block mode, using the key events enumerated types, to ensure that no duplicates, and to add user needs logic using macros define debounce time, even by the trigger time, double time interval, longer press time, easy to modify.
And all keys are created using a single linked list batter, the user just created, disregard key processing, simply call Button_Process()can automatically traverse all keys are created in the function.
Support delete button, users do not need to delete the corresponding key in the code to create a map link code, without deleting any callbacks on key handler, simply call the Button_Delete()function can be, so the child will not be processed on the Delete button of any state. Of course, the current key memory will not be released, if os, I suggest you release the key memory.

Key control block
/*
    每个按键对应1个全局的结构体变量。
    其成员变量是实现消抖和多种按键状态所必须的
*/
typedef struct button
{
    /* 下面是一个函数指针,指向判断按键手否按下的函数 */
    uint8_t (*Read_Button_Level)(void); /* 读取按键电平函数,需要用户实现 */
  
  char Name[BTN_NAME_MAX];
    
  uint8_t Button_State              :   4;    /* 按键当前状态(按下还是弹起) */
  uint8_t Button_Last_State         :   4;    /* 上一次的按键状态,用于判断双击 */
  uint8_t Button_Trigger_Level      :   2;    /* 按键触发电平 */
  uint8_t Button_Last_Level         :   2;    /* 按键当前电平 */
  
  uint8_t Button_Trigger_Event;     /* 按键触发事件,单击,双击,长按等 */
  
  Button_CallBack CallBack_Function[number_of_event];
  uint8_t Button_Cycle;            /* 连续按键周期 */
  
  uint8_t Timer_Count;          /* 计时 */
  uint8_t Debounce_Time;        /* 消抖时间 */
  
  uint8_t Long_Time;          /* 按键按下持续时间 */
  
  struct button *Next;
  
}Button_t;
trigger event
typedef enum {
  BUTTON_DOWM = 0,
  BUTTON_UP,
  BUTTON_DOUBLE,
  BUTTON_LONG,
  BUTTON_CONTINUOS,
  BUTTON_CONTINUOS_FREE,
  BUTTON_ALL_RIGGER,
  number_of_event, /* 触发回调的事件 */
  NONE_TRIGGER
}Button_Event;
Select the macro definition
#define BTN_NAME_MAX  32     //名字最大为32字节

/* 按键消抖时间40ms, 建议调用周期为20ms
 只有连续检测到40ms状态不变才认为有效,包括弹起和按下两种事件
*/

#define CONTINUOS_TRIGGER             0  //是否支持连续触发,连发的话就不要检测单双击与长按了    

/* 是否支持单击&双击同时存在触发,如果选择开启宏定义的话,单双击都回调,只不过单击会延迟响应,
   因为必须判断单击之后是否触发了双击否则,延迟时间是双击间隔时间 BUTTON_DOUBLE_TIME。
   而如果不开启这个宏定义,建议工程中只存在单击/双击中的一个,否则,在双击响应的时候会触发一次单击,
   因为双击必须是有一次按下并且释放之后才产生的 */
#define SINGLE_AND_DOUBLE_TRIGGER     1 

/* 是否支持长按释放才触发,如果打开这个宏定义,那么长按释放之后才触发单次长按,
   否则在长按指定时间就一直触发长按,触发周期由 BUTTON_LONG_CYCLE 决定 */
#define LONG_FREE_TRIGGER             0 

#define BUTTON_DEBOUNCE_TIME      2   //消抖时间      (n-1)*调用周期
#define BUTTON_CONTINUOS_CYCLE  1     //连按触发周期时间  (n-1)*调用周期  
#define BUTTON_LONG_CYCLE       1     //长按触发周期时间  (n-1)*调用周期 
#define BUTTON_DOUBLE_TIME      15  //双击间隔时间  (n-1)*调用周期  建议在200-600ms
#define BUTTON_LONG_TIME          50        /* 持续n秒((n-1)*调用周期 ms),认为长按事件 */

#define TRIGGER_CB(event)   \
        if(btn->CallBack_Function[event]) \
          btn->CallBack_Function[event]((Button_t*)btn)
example
  Button_Create("Button1",
              &Button1, 
              Read_KEY1_Level, 
              KEY_ON);
  Button_Attach(&Button1,BUTTON_DOWM,Btn1_Dowm_CallBack);                       //单击
  Button_Attach(&Button1,BUTTON_DOUBLE,Btn1_Double_CallBack);                   //双击
  Button_Attach(&Button1,BUTTON_CONTINUOS,Btn1_Continuos_CallBack);             //连按  
  Button_Attach(&Button1,BUTTON_CONTINUOS_FREE,Btn1_ContinuosFree_CallBack);    //连按释放  
  Button_Attach(&Button1,BUTTON_LONG,Btn1_Long_CallBack);                       //长按


  Button_Create("Button2",
              &Button2, 
              Read_KEY2_Level, 
              KEY_ON);
  Button_Attach(&Button2,BUTTON_DOWM,Btn2_Dowm_CallBack);                     //单击
  Button_Attach(&Button2,BUTTON_DOUBLE,Btn2_Double_CallBack);                 //双击
  Button_Attach(&Button2,BUTTON_CONTINUOS,Btn2_Continuos_CallBack);           //连按
  Button_Attach(&Button2,BUTTON_CONTINUOS_FREE,Btn2_ContinuosFree_CallBack);  //连按释放
  Button_Attach(&Button2,BUTTON_LONG,Btn2_Long_CallBack);                     //长按

  Get_Button_Event(&Button1);
  Get_Button_Event(&Button2);

Follow-up

Streamer Gangster request, let me play with the RTT rtkpgs , we intend to use the right hand Button_drive a training practice.

ButtonDrive in env

Currently I have made a key driver package (packages), if using RT-Thread operating system, it can be configured directly use in env!

Proceed as follows:

  1. Select Packages Online

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-gG59d3Yn-1571148176404) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/5 .png? raw = true)]

  1. Select the package property related peripherals

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-amGYFH8P-1571148176404) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/6 .png? raw = true)]

  1. Select button_drive

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-O0a0wqdi-1571148176404) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/7 .png? raw = true)]

  1. Into the drive configuration options (comes with default properties)

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/8 (img-O5sGP0CE-1571148176405) .png? raw = true)]

  1. If you do not know what it means to configure the keys, press "shift +?", There can be interpretation

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-EYxByBVm-1571148176407) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/9 .png? raw = true)]

  1. Compiled mdk / iar Engineering

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-e0pdTFhz-1571148176407) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/10 .png? raw = true)]

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-lXNX8VeJ-1571148176407) (https://github.com/jiejieTop/ButtonDrive/blob/master/png/11 .png? raw = true)]

About rtkpgs

Introduction ( English )

buildpkg for generating rapid RT-Thread package build tool.

A good package should look like this:

  1. Code is elegant and standardized.
  2. examples routine, providing easy to understand the use of routine.
  3. SConscript file for RT-Thread environment and compiled together.
  4. README.md document, provide the necessary functionality to the user instructions.
  5. docs folder, place the other details of the document except the README.
  6. a license file, Copyright.

In order to facilitate the rapid generation of RT-Thread package standardized templates, and reduce the burden on the preparatory work of open source warehouse migration of the RT-Thread, based buildpkg came into being for this purpose, to provide supporting development tools for the development package of Rt-Thread developers.

No. Support description
1 Construction of template package Creating specify the name of package, automatically added readme / version / github ci script / demo / open source license file
2 Open source migration warehouse Construction of the specified git repository package, automatically add the readme / version / github ci script / demo / open source license file, but require the user to migrate warehouse modify the actual situation
3 Update package After generating package can update the version number, open source licenses or scons script again and so set before

Instructions for use

1. Build the package

buildpkg.exe make pkgdemo

2. Migration Open Source warehouse

buildpkg.exe make cstring https://github.com/liu2guang/cstring.git

3. Update package

buildpkg.exe update pkgname

4. Optional

Long parameters Short parameter description
--version=v1.0.0 -v v1.0.0 Set the package version
--license = MY l MIT Set copyright agreement package followed
--submodule -s Delete git submodule

Moving map presentation Windows10 and Linux platforms

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-CT6Hxh28-1571148176408) (/ figures / buildpkg.gif)]

testing platform

No. testing platform Test Results
1 win10 exe test passes, py test by
2 win7 exe to be tested, py to be tested
3 mac py script does not know whether compatible, there is no test conditions, followed by maintenance at
4 linux py script does not know whether compatible, there is no test conditions, followed by maintenance at

Contact

I like to focus on it!

I welcome public attention No.

The relevant code number can be acquired in public background.
For more information please concern "Things IoT development," the public number!

Guess you like

Origin www.cnblogs.com/iot-dev/p/11688807.html