Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 - Gpio repeated behaviour #630

Closed
valoni opened this issue Jun 19, 2020 · 4 comments · Fixed by nanoframework/nf-interpreter#1660
Closed

ESP32 - Gpio repeated behaviour #630

valoni opened this issue Jun 19, 2020 · 4 comments · Fixed by nanoframework/nf-interpreter#1660

Comments

@valoni
Copy link

valoni commented Jun 19, 2020

Details about Problem

Target: ESP32

Firmware image version: Stable 1.4.2.1 (evend latest develop)

A clear and concise description of what the bug is.

Detailed repro steps so we can see the same problem

source code :

using System;
using System.Diagnostics;
using System.Threading;
using Windows.Devices.Gpio;
using nanoFramework.Runtime.Native;

namespace BlinkSample
{
    public class Program
    {
       static GpioPin Userled;
        static GpioPin UserBtn;
		
        public static void Main()
        {
            Debug.WriteLine("--------------------------------------------");

            nanoFramework.Runtime.Native.GC.EnableGCMessages(true);

            Debug.WriteLine($"{ SystemInfo.TargetName } running on { SystemInfo.Platform }.");

            nanoFramework.Runtime.Native.GC.Run(false);

            Debug.WriteLine("--------------------------------------------");


            Debug.WriteLine("--------------------------------------------");
            Debug.WriteLine("Welcome ESP32 Board");
            Debug.WriteLine("--------------------------------------------");

            Userled = GpioController.GetDefault().OpenPin(2);
            Userled.SetDriveMode(GpioPinDriveMode.Output);

            UserBtn = GpioController.GetDefault().OpenPin(0);
            UserBtn.SetDriveMode(GpioPinDriveMode.InputPullUp);
            UserBtn.ValueChanged += UserBtn_ValueChanged;

            int cnt = 0;

            while (true)
            {
                Userled.Write(GpioPinValue.High);
                Thread.Sleep(125);

                Userled.Write(GpioPinValue.Low);
                Thread.Sleep(125);

                Debug.WriteLine("Cnt = " + cnt.ToString());
                cnt++;

                Debug.WriteLine(">> " + cnt.ToString() + " free memory: " + nanoFramework.Runtime.Native.GC.Run(false) + " bytes");

            }
        }

        private static void UserBtn_ValueChanged(object sender, GpioPinValueChangedEventArgs e)
        {
            if (e.Edge == GpioPinEdge.FallingEdge)
                Debug.WriteLine("Button Clicked");
            else
                Debug.WriteLine("Button Unclicked");
        }

       
    }
}

behaviour on GpioPinValueChangedEventArgs event trigger unexpected.
and repeat click/unclick multiple time ?? (if button pressed once)

214 free memory: 104088 bytes
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Unclicked
Button Clicked
Button Clicked
Button Unclicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Clicked
Button Unclicked
Button Clicked
Button Unclicked
Button Clicked
Button Clicked
Button Unclicked
Button Clicked
Button Unclicked
Button Clicked
Button Unclicked
Button Clicked
Button Unclicked
Button Unclicked
Button Unclicked
Button Clicked
Cnt = 214

Expected behaviour

Button Clicked (once)
after release button - Button Unclicked (once)

@nfbot
Copy link
Member

nfbot commented Jun 19, 2020

Hi @valoni,
😞 Looks like you haven't read the instructions with enough care or forgot to add something required or haven't cleanup the instructions. Please make sure to follow the template and fix whathever is wrong or missing and feel free to reopen the issue..

@Dweaver309
Copy link

I can confirm this bug I get the same results using the sample code.

@AdrianSoundy
Copy link
Member

AdrianSoundy commented Jun 20, 2020

If you are using physical buttons you must add a DebounceTimeout as the buttons bounce and create multiple events for every click.

UserBtn.DebounceTimeout = new TimeSpan(0, 0, 0, 0, 50);

The only thing that seems wrong is you get currently get multiple Unclicked or multilpe clicks events in a row which is wrong.

@valoni
Copy link
Author

valoni commented Jun 20, 2020

after your suggestion

i did

            UserBtn = GpioController.GetDefault().OpenPin(0);
            UserBtn.SetDriveMode(GpioPinDriveMode.InputPullUp);

            UserBtn.DebounceTimeout = new TimeSpan(0, 0, 0, 0, 50);

            UserBtn.ValueChanged += UserBtn_ValueChanged;

click work well now , so i belive it just need to be documented this case if used ESP32 ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants