Skip to content

ESP32 - Gpio repeated behaviour  #630

Closed
nanoframework/nf-interpreter
#1660

Description

@valoni

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)

Activity

  1. nfbot commented on Jun 19, 2020

    @nfbot
    Member

    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..

  2. Dweaver309 commented on Jun 19, 2020

    @Dweaver309

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

  3. AdrianSoundy commented on Jun 20, 2020

    @AdrianSoundy
    Member

    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.

  4. valoni commented on Jun 20, 2020

    @valoni
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions