Zigbee MCU Development Board- Realize single-click, double-click, long-press recognition feedback-Super simple method-New World Internet of Things-Internet of Things Competition-Zigbee&STM32 Development Board

Table of contents

I. Overview

2. Overall thinking

3. Code implementation

Four. Summary


I. Overview

        Due to the recent need to develop multiple functions for a single Button, we need to realize the effect feedback of single-click, double-click, and long-press to achieve different operation effects. (code open source)


2. Overall thinking

        For different pressing methods, we must first understand the difference between single-click, double-click, and long-press.

        Next, let's continue to look at the waveform of the long press.

        Through analysis, it is not difficult to find that clicking is the simplest process from high level to low level to high level, and the duration of low level is short. The high level time is short, and the long press is essentially a single click, but the middle low level lasts for a long time.

level change duration
click high-low-high short
Press high-low-high long
double click high-low-high-low-high short

        We determine the programming idea through the above table, and the key signals to be received are divided into two types, one is long-duration (long press), and the other is short-duration (click or double-click), because we need a variable to Store the time between two times so that we can make judgments.


3. Code implementation

        Here I choose the Button triggered by low level to realize the following functions.

        First initialize the relevant LEDs and Switch pins.

    P1SEL &=~ 0X44;
    P1DIR &= ~0X44;
    P1SEL &= ~0x1b;
    P1DIR |= 0X1B;

        Then we define a global variable first. Here I use unsigned long, which is limited by the maximum number in the single-chip microcomputer, and the number of instructions that the single-chip microcomputer can process per second, you can choose by yourself.

        Then we increment the defined flag bit in the main loop. I choose 60000 because on our microcontroller, 60000 is roughly equivalent to half a second.

if(flag<60000)
{
    flag++;
}

        Then we continue to complete the main function and analyze it.

if(sw1==0)
{
 halMcuWaitMs(10);
 if(sw1==0)
 {
     if(flag<60000)//认为是第二次双击
     {
       //执行双击策略
       halLedToggle(2);//执行某个语句
     }
     flag=0;
   while(!sw1)
   {
     if(flag<60000)
       flag++;
     if(flag==60000)
     {
       halLedSet(1);//执行某个语句
       flag++;//只执行一次
     }
   }
   if(flag==60001)
   {
      halLedClear(1);//执行某个语句
   }
   flag=0;
   
 }

        Every time the button is released, the flag will be reset. When the button is pressed again, if the flag is less than 60000, it means that this is a double-click event. Otherwise, it means that the duration of this event and the last event is long, and there is no necessary connection. We can further analyze whether it is a single click or a long press.

        In the project of waiting for the button to be released, reset the flag again and use the flag to calculate the time. If the flag is less than 60000 when the button is released, it means that the duration of the low level is less than 0.5s, and it is a click, otherwise it is a click. One long press.

        After the button is released, by judging the value of the flag, if it is less than 60000, it means that this is the end event after the click, otherwise it is the event after the end of the long press.


Four. Summary

        Through the idea of ​​timing, this event analyzes the difference between single-click, double-click, and long-press, realizes the distinction, and uses one button for multiple functions to play different functions and improve development efficiency.

Guess you like

Origin blog.csdn.net/qq_39724355/article/details/127567359