Git Product home page Git Product logo

Comments (4)

marinasundstrom avatar marinasundstrom commented on May 19, 2024 1

My improved Button class:

using System;
using System.Linq;
using System.Threading.Tasks;
using Unosquare.RaspberryIO.Gpio;
using Unosquare.RaspberryIO.Native;

namespace RelaysTest
{
    public class Button
    {
        private GpioPin gpioPin;
        private ulong pressed_last_interrupt;
        private ulong released_last_interrupt;
        private const ulong INTERRUPT_TIME = 500;

        public Button(GpioPin gpioPin)
        {
            this.gpioPin = gpioPin;

            Initialize();
        }

        private void Initialize()
        {
            gpioPin.InputPullMode = GpioPinResistorPullMode.PullDown;
            gpioPin.PinMode = GpioPinDriveMode.Input;
            gpioPin.RegisterInterruptCallback(EdgeDetection.RisingAndFallingEdges, handleInterrupt);
        }

        private void handleInterrupt()
        {
            if (gpioPin.Read())
            {
                handleButtonPressed();
            }
            else
            {
                handleButtonReleased();
            }
        }

        private void handleButtonPressed()
        {
            ulong interrupt_time = WiringPi.millis();

            if (interrupt_time - pressed_last_interrupt > INTERRUPT_TIME)
            {
                pressed_last_interrupt = interrupt_time;
                Pressed?.Invoke(this, new EventArgs());
            }
        }

        private void handleButtonReleased()
        {
            ulong interrupt_time = WiringPi.millis();

            if (interrupt_time - released_last_interrupt > INTERRUPT_TIME)
            {
                released_last_interrupt = interrupt_time;
                Released?.Invoke(this, new EventArgs());
            }
        }

        public event EventHandler<EventArgs> Pressed;

        public event EventHandler<EventArgs> Released;
    }
}

Test:

    public static class Extensions
    {
        public static GpioPin GetGpioPinByBcmPinNumber(this GpioController controller, int bcmPinNumber) =>
            controller.First(pin => pin.BcmPinNumber == bcmPinNumber);
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            var relayChannel1 = GpioController.Instance.GetGpioPinByBcmPinNumber(17);
            relayChannel1.PinMode = GpioPinDriveMode.Output;

            var buttonPin = GpioController.Instance.GetGpioPinByBcmPinNumber(26);
            buttonPin.PinMode = GpioPinDriveMode.Input;

            var button = new Button(buttonPin);
            button.Pressed += (s, e) =>
            {
                Console.WriteLine("Pressed");
            };

            button.Released += (s, e) =>
            {
                Console.WriteLine("Released");
                var value = relayChannel1.Read();
                relayChannel1.Write(!value);
            };

            Console.ReadKey();
        }
    }

from raspberryio.

geoperez avatar geoperez commented on May 19, 2024

I'm going to integrate it

from raspberryio.

geoperez avatar geoperez commented on May 19, 2024

f2f5464

from raspberryio.

buda2570 avatar buda2570 commented on May 19, 2024

Hi . I am using such a code, I have Pin noise problem and only button is fired, any solution?

from raspberryio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.