In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "pure C language implementation of key-driven Button_drive example usage", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "pure C language implementation of key-driven Button_drive example usage" bar!
Introduction to Button_drive
Button_drive is a compact keystroke driver that supports clicks, double clicks, long presses, continuous triggers, etc. (trigger events can be added to the keystroke control block later). In theory, Button,Button_drive can be extended indefinitely to handle business logic by keystroke trigger event callback, which can be used in RTOS. I have only tested it on RT-Thread so far. The purpose of writing key drivers is to separate the user's keystroke logic from keystroke processing events, so that users do not have to deal with complex and troublesome logical events.
Button_drive use effect
Click and long press
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/1.png?raw=true)]).
Double click
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/2.png?raw=true)]).
Press repeatedly
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/3.png?raw=true)]).
Press and release
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/4.png?raw=true)]).
Usage
Create a key handle
Button_t Button1;Button_t Button2
Create keys and initialize key information, including key name, key level detection function interface, key trigger level.
Button_Create ("Button1", / / key name & Button1, / / key handle Read_Button1_Level, / / key level detection function interface BTN_TRIGGER) / / trigger level.
The key trigger event is linked to the event callback function, and when the key event is triggered, it automatically jumps back to deal with the business logic in the callback function.
Button_Attach (& Button1,BUTTON_DOWM,Btn2_Dowm_CallBack); / / Click Button_Attach (& Button1,BUTTON_DOUBLE,Btn2_Double_CallBack); / / double-click Button_Attach (& Button1,BUTTON_LONG,Btn2_Long_CallBack); / / long press.
Call the callback key handler function periodically. It is recommended to call cycle 20-50ms.
Button_Process (); / / call the keystroke processing function periodically
Two functions that need to be implemented by the user:
Key level detection interface:
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);} / / this is the pseudo code I simply tested on stm32, based on the actual source code.
Key-press logic processing
Void Btn1_Dowm_CallBack (void * btn) {PRINT_INFO ("Button1 click!");} void Btn1_Double_CallBack (void * btn) {PRINT_INFO ("Button1 double click!");} void Btn1_Long_CallBack (void * btn) {PRINT_INFO ("Button1 long press!"); Button_Delete (& Button2); PRINT_INFO ("delete Button1"); Search_Button ();} characteristics
Button_drive open source, keystroke control block adopts data structure, keystroke events use enumerated types to ensure that there is no repetition, and it is easy to add user-needed logic. Macro definition is used to define anti-shake time, continuous press trigger time, double-click time interval, long press time, etc., easy to modify. At the same time, all the created keys are linked by a single linked list, and the user only needs to create them, regardless of the key processing, just call Button_Process (), and automatically traverse all the created keys in the function. Key deletion is supported, and users do not need to delete the corresponding keys in the code created in the mapping link code, nor do they need to delete any callback event handlers on keys, just call the Button_Delete () function, so that any state of the deleted keys will not be handled. Of course, the key memory will not be released at present, if you use os, it is recommended to release the key memory.
Key control block / * each key corresponds to a global structure variable. Its member variables are necessary for anti-shake and multiple key states * / typedef struct button {/ * below is a function pointer to determine whether the keystroke hand is pressed * / uint8_t (* Read_Button_Level) (void); / * to read the key level function, the user is required to implement * / char name [BTN _ NAME_MAX] Uint8_t Button_State: 4; / * the current state of the button (press or pop up) * / uint8_t Button_Last_State: 4; / * the state of the last button, which is used to determine the double-click * / uint8_t Button_Trigger_Level: 2; / * button trigger level * / uint8_t Button_Last_Level: 2 / * key current level * / uint8_t Button_Trigger_Event; / * key trigger event, click, double click, long press etc. * / Button_CallBack CallBack_ function [number _ of_event]; uint8_t Button_Cycle; / * continuous key cycle * / uint8_t Timer_Count; / * timing * / uint8_t Debounce_Time / * Anti-shake time * / uint8_t Long_Time; / * duration pressed by key * / 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, / * event triggering callback * / NONE_TRIGGER} Button_Event Macro definition selects # define BTN_NAME_MAX 32 / / name with a maximum of 32 bytes / * keystroke anti-shake time 40ms. It is recommended that the call cycle is valid only if 20ms continuously detects that the 40ms status remains unchanged, including whether continuous triggering is supported for the two events * / # define CONTINUOS_TRIGGER 0 / / Do not detect whether single-click and long-click / * support click-double-click trigger at the same time. If you choose to open the macro definition, single-click and double-click will be called back, but the click will delay the response, because you must judge whether the double-click is triggered after the click. Otherwise, the delay time is the double-click interval BUTTON_DOUBLE_TIME. If you do not open this macro definition, it is recommended that only one of the clicks / double clicks exist in the project, otherwise, a click will be triggered when you double click the response, because the double click must be pressed once and released. Does the * / # define SINGLE_AND_DOUBLE_TRIGGER 1 / * generated after the double click is pressed and released supports long press release to trigger? if the macro definition is opened, a single long press will be triggered only after a long press release. Otherwise, the long press will be triggered for a long time. The trigger period is determined by BUTTON_LONG_CYCLE * / # define LONG_FREE_TRIGGER 0 # define BUTTON_DEBOUNCE_TIME 2 / / de-shake time (NMQ 1) * call cycle # define BUTTON_CONTINUOS_CYCLE 1 / / continuous press trigger cycle time (NMAE 1) * call cycle # define BUTTON_LONG_CYCLE 1 / / long press trigger cycle time ( NMel 1) * call cycle # define BUTTON_DOUBLE_TIME 15 / / double-click interval (n Mel 1) * call cycle is recommended to be 200-600ms#define BUTTON_LONG_TIME 50 / * last n seconds ((NMel 1) * call cycle ms) Consider the long press event * / # 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); / / Click Button_Attach (& Button1,BUTTON_DOUBLE,Btn1_Double_CallBack); / / double-click Button_Attach (& Button1,BUTTON_CONTINUOS,Btn1_Continuos_CallBack); / / double-click Button_Attach (& Button1,BUTTON_CONTINUOS_FREE,Btn1_ContinuosFree_CallBack) / / double-click to release Button_Attach (& Button1,BUTTON_LONG,Btn1_Long_CallBack); / / long-press Button_Create ("Button2", & Button2, Read_KEY2_Level, KEY_ON); Button_Attach (& Button2,BUTTON_DOWM,Btn2_Dowm_CallBack) / / Click Button_Attach (& Button2,BUTTON_DOUBLE,Btn2_Double_CallBack); / / double-click Button_Attach (& Button2,BUTTON_CONTINUOS,Btn2_Continuos_CallBack); / / double-click Button_Attach (& Button2,BUTTON_CONTINUOS_FREE,Btn2_ContinuosFree_CallBack); / / double-click to release Button_Attach (& Button2,BUTTON_LONG,Btn2_Long_CallBack) / / long press Get_Button_Event (& Button1); Get_Button_Event (& Button2); follow up
According to the request of Liuguang boss, let me play RTT's rtkpgs and intend to practice my hands with Button_drive.
ButtonDrive is used in env
At present, I have made the keystroke driver into a software package (packages). If you use the RT-Thread operating system, you can configure it directly in env!
The steps are as follows:
Select online software package
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/5.png?raw=true)]).
Select the package attribute to be related to the peripheral
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/6.png?raw=true)]).
Select button_drive
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/7.png?raw=true)]).
Enter the option configuration of the driver (with default properties)
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/8.png?raw=true)]).
If you don't know what the button configuration means, press "shift+?" There can be an explanation
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/9.png?raw=true)]).
Compile and generate mdk/iar project
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/10.png?raw=true)]).
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (https://github.com/jiejieTop/ButtonDrive/blob/master/png/11.png?raw=true)]).
About introduction to rtkpgs (English)
Buildpkg is a quick build tool for generating RT-Thread package.
A good package should look like this:
The code is elegant and standardized.
Examples routines that provide easy-to-understand routines.
SConscript file, which is used to compile with the RT-Thread environment.
README.md documentation to provide the user with the necessary functional description.
The docs folder, which places detailed documents other than README.
License license file, copyright description.
In order to facilitate the rapid generation of RT-Thread package canonical templates and reduce the burden of the preparatory work for the migration of open source warehouses to RT-Thread, buildpkg based on this purpose came into being to provide auxiliary development tools for package developers who develop Rt-Thread.
Serial number support function description 1 build package template to create specified name package, automatically add readme/ version number / github ci script / demo/ open source protocol file 2 migrate open source warehouse to build package from designated git warehouse, automatically add readme/ version number / github ci script / demo/ open source protocol file However, the migrated warehouse requires users to modify 3 according to the actual situation, update package and generate package, and then update the previously set version number again. Instructions for the use of open source protocols or scons scripts 1. Build package
Buildpkg.exe make pkgdemo
two。 Migrate open source repositories
Buildpkg.exe make cstring https://github.com/liu2guang/cstring.git
3. Update package
Buildpkg.exe update pkgname
4. Optional configuration of long parameters and short parameters description-version=v1.0.0-v v1.0.0 sets the version of package-license=MIT-l MIT sets the copyright agreement followed by package-submodule-s deletes the git sub-module Windows10 and the demo of the Linux platform
[external link image transfer failed. The origin server may have hotlink protection mechanism. It is recommended to save the image and upload it directly (img-CT6Hxh38-1571148176408) (/ figures/buildpkg.gif)]
Test platform serial number test platform test results 1win10exe test, py test passed 2win7exe to be tested, py test 3macpy script to be tested does not know whether it is compatible, there are no test conditions, after the maintenance of the 4linuxpy script does not know whether it is compatible, there are no test conditions, after maintenance, thank you for your reading, the above is the "pure C language implementation of key-driven Button_drive example usage" content, after the study of this article I believe that you have a deeper understanding of the example usage of button-driven Button_drive implemented in pure C language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.