简体   繁体   English

具有Raspberry Pi GPIO的保持/按下按钮

[英]Hold/Push Button with Raspberry Pi GPIO

I would like my button to do 2 of the following things; 我希望我的按钮可以执行以下两项操作;

  • Do something whenever a button is pushed 只要按下按钮就做某事
  • Do something whenever a button is held for a certain amount of time (2seconds) 按住按钮一定时间(2秒)时,请执行某些操作

The current code that I'm working with looks a little like this: 我正在使用的当前代码看起来像这样:

    //Comfirming a letter or sending the word.
    private void btnRightValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
    {
        //if button is pressed it will confirm a letter
        if (e.Edge == GpioPinEdge.FallingEdge)
        {
            //code to confirm letter
            ConfirmLetter();

            //if(buttonHeldForAWhile)
            //{
            //Send message
            //SendWord();
            //}
        }
    }

But how can I check whether the button is held for 2 or 3 seconds? 但是,如何检查按钮是否保持2或3秒钟?

I found a way to fix my problem by using a Stopwatch that measures the time that the button is being pressed. 我找到了一种使用秒表来解决问题的方法,该秒表可以测量按钮被按下的时间。

Above our namespace we need to add this in order for us to use the Stopwatch class. 在我们的命名空间上方,我们需要添加它以便我们使用Stopwatch类。

using System.Diagnostics;

Now we want to declare a Stopwatch by: 现在,我们要通过以下方式声明秒表:

Stopwatch stopWatch;

When the button is pressed and held my code will pick that up with the FallingEdge . 按住按钮时,我的代码将通过FallingEdge拾取。 When it is pressed I also want it to create a new Stopwatch and start it. 当它被按下时,我也希望它创建一个新的秒表并启动它。

When the button is released, which is checked by RisingEdge . 释放按钮时,由RisingEdge进行检查。 I stop the timer and get the time between the 2 events. 我停止计时器并获取两个事件之间的时间。 I then compare the time pressed in an if statement so I can decide what to do with the buttonpress. 然后,我将比较if语句中的按下时间,以便我可以决定如何处理按钮按下。 In this case SendWord, or Append current char and preview it. 在这种情况下,SendWord或追加当前字符并预览。

    //Comfirming the chosen letter.
    private void btnRightValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
    {
        if (e.Edge == GpioPinEdge.FallingEdge)
        {
            stopWatch = new Stopwatch();
            stopWatch.Start();
        }

        if (e.Edge == GpioPinEdge.RisingEdge)
        {
            stopWatch.Stop();
            long duration = stopWatch.ElapsedMilliseconds;

            if (duration > 2000 )
            {
                SendWord();
            }
            else
            {
                //Add the current character to the word
                _currentWordSB.Append(Convert.ToString(_currentChar));

            //    //Reset currentChar aswell
                _currentChar = ' ';

            //    //The user confirmed the morse sequence and wants to start a new one, so we reset it.
                _morseCode.Clear();

            //    //Preview the word
                PreviewLetter();
            }
        }
    }

If there is any better way to tackle my problem, please let me know but so far this solution works for me. 如果有更好的方法来解决我的问题,请告诉我,但到目前为止,该解决方案对我而言仍然有效。

I needed this as I was working on a Raspberry Pi project with Morse code input via buttons. 我在通过Raspberry Pi项目进行莫尔斯电码通过按钮输入时需要这个。 That input then had to be displayed on a LCD screen. 然后必须将该输入显示在LCD屏幕上。 If the user was satisfied with his input he could then send it to the other user(s) by holding the Right button for 2 seconds. 如果用户对其输入感到满意,那么他可以通过按住“向右”按钮2秒钟将其发送给其他用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM