Comments (4)
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.
I'm going to integrate it
from raspberryio.
from raspberryio.
Hi . I am using such a code, I have Pin noise problem and only button is fired, any solution?
from raspberryio.
Related Issues (20)
- Unable to load shared library 'libwiringPi.so.2.52 HOT 4
- [FAQ] wiringpi vs pigpio HOT 1
- Could not load file or assembly 'Unosquare.Swan.Lite' HOT 1
- Issues with an executable (C#) and sudo HOT 2
- wiringPiISR: Can't find gpio program HOT 7
- Waveshare PN532. Cant read UID HOT 1
- SPI Channel0 & Channel1 clarification HOT 1
- Documentation: "Pin27" HOT 1
- "StartSoftPwm" fails on second call HOT 6
- How to Use ADS1115 I2C Analog-to-Digital Converter devices HOT 5
- Support the I2C HD44780 (1604 LC-Display) HOT 2
- The type or namespace name 'Gpio' does not exist in the namespace 'Unosquare.RaspberryIO' (are you missing an assembly reference?) HOT 2
- Help required with Pi.Camera.OpenVideoStream. HOT 2
- Make I2C Bus port selectable HOT 1
- Raw Values of ADS1015 via I2C wrong scaling HOT 1
- Is there a way to set SPI mode? HOT 1
- A question about softpwm HOT 1
- Publishing to linux-arm
- "The DLL" libc ": The specified module was not found. HOT 6
- Switch to libcamera HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from raspberryio.