Git Product home page Git Product logo

pigpio-dotnet's Introduction

Build status

pgipio-dotnet Raspbery Pi - libpigpio for .net

WE ARE LOOKING FOR A NEW HOME FOR THIS PROJECT. APPLY AT: https://adoptoposs.org/p/d3470190-942b-44ac-84fc-90259cbdee43

⭐ Please star this project if you find it useful!

Provides complete managed access to the popular pigpio C library.

The premise is simple: using the powerful features of C# to control the ARM peripherals of the Raspberry Pi. This library provides a comprehensive way to access the hardware of the Pi. It uses the fantastic C library pigpio. The documentation of the library can be found here.

As a programmer, the choice is yours. You can call the native methods either directly or via the comprehensive API of PiGpio.net.

Example of blinking an LED with direct native calls

Setup.GpioInitialise();
var pin = SystemGpio.Bcm18;
IO.GpioSetMode(pin, PinMode.Output);

while (true)
{
    IO.GpioWrite(pin, true);
    Thread.Sleep(500);
    IO.GpioWrite(pin, false);
    Thread.Sleep(500);
}

Example of blinking an LED with the PiGpio.net Managed API

var pin = Board.Pins[18];

while (true)
{
    pin.Value = !pin.Value;
    Thread.Sleep(500);
}

Related Projects and Nugets

Name Author Description
RaspberryIO Unosquare The Raspberry Pi's IO Functionality in an easy-to-use API for .NET (Mono/.NET Core).
PiGpio.net Unosquare Provides complete managed access to the popular pigpio C library
Raspberry Abstractions Unosquare Allows you to implement your own provider for RaspberryIO.
Raspberry# IO raspberry-sharp Raspberry# IO is a .NET/Mono IO Library for Raspberry Pi. This project is an initiative of the Raspberry# Community.
WiringPi.Net Daniel Riches A simple C# wrapper for Gordon's WiringPi library.
PiSharp Andy Bradford Pi# is a library to expose the GPIO functionality of the Raspberry Pi computer to the C# and Visual Basic.Net languages

pigpio-dotnet's People

Contributors

dependabot-preview[bot] avatar geoperez avatar jaessy77 avatar jlennox avatar mariodivece avatar rainson12 avatar shaggygi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pigpio-dotnet's Issues

Publish new version on Nuget

Hi! Great work with this project! Is it possible to publish the latest version on Nuget? I am not able to use the abstraction project with the pigpio-dotnet version available on Nuget now.

ManagedModel for GpioWaveChain

Could you please implement Unosquare.PiGpio.NativeMethods.Waves.GpioWaveChain in the managed model? It would make code much less clunky.

Cannot marshal 'parameter #2' when using GpioWaveAddGeneric

Describe the bug
A clear and concise description of what the bug is.

To Reproduce

Setup.GpioInitialise();
IO.GpioSetMode(SystemGpio.Bcm04, PinMode.Output);
var wf = new List<GpioPulse>();
wf.Add(new GpioPulse() { GpioOn = BitMask.Bit04, DurationMicroSecs = 9415 });
wf.Add(new GpioPulse() { GpioOff = BitMask.Bit04, DurationMicroSecs = 89565 });
var wid = (uint)Waves.GpioWaveAddGeneric((uint)wf.Count, wf.ToArray()); // crash here

Additional context
The error message:
Cannot marshal 'parameter #2': Unknown error."

What could be the cause? Am I doing something wrong?

Program crashes when compiling with configuration = Release

When I compile with my app with dotnet publish -r linux-arm -o dist -c Release the app is very unstable. I get a lot of messages like this: 2019-10-30 14:11:31 sigHandler: Unhandled signal 34, terminating

If I compile using dotnet publish -r linux-arm -o dist -c Debug the app is much more stable.

Is this something you are familiar with?

How to use it directly with cs file?

For some reasons, my monodevelop/VS Code doesn't work.

Could someone tell me, if there is still a chance to use this library and how?

Thx in advance :)

PiGpio not imlement Abstractions

There are no Interfaces used in PiGpio(IGpioController/IGpioPin/ISpiChannel/etc...)

public sealed class GpioPin
{
...
}

should be

public sealed class GpioPin : IGpioPin
{
...
}

Maybe this is a feature request?

I can't install:"Unosquare.PiGpio 0.0.2".

I can install Unosquare.PiGpio 0.0.1. But I can not install "Unosquare.PiGpio 0.0.2".
Error: Unable to install the “Unosquare.PiGpio 0.0.2” package. You are trying to install this package into a project, the required platform version of which is “.NETFramework, Version = v4.6.1”, but the package does not contain references to assemblies or content files compatible with this platform. Additional information can be obtained from the author of the package.

AlertFunc provokes an error

I've written a simple programm where IO.GpioSetAlertFunc is used. It works for a few seconds but then the error "Unhandled signal 11, terminating" happens.
I've noticed that the error doesn't happen if a pin whose value doesn't change is selected.

My code:

static void Main(string[] args)
        {
            ILogger log = LogManager.GetLogger(typeof(Program));
            log.LogInformation("Start");

            Setup.GpioInitialise();
            IO.GpioSetAlertFunc(UserGpio.Bcm23, MyPiGpioAlertDelegate);
            System.Console.ReadKey();
        }
public static void MyPiGpioAlertDelegate(UserGpio userGpio, LevelChange levelChange, uint timeMicrosecs)
        {
        }

WaitForExit Hangs After Use Gpio

Description
Hi When I use any gpio and then I want to get the output of a process like "cat /proc/cpuinfo | grep Serial | awk ' {print $3}'" the application hangs in WaitForExit of Process method, I've shared a project in a repo -> https://bitbucket.org/juandrn/waitforexithangsafterusegpio/src

To Reproduce

  • 1.- Compile project with dotnet publish -r linux-arm -c debug (In visual studio 2017)
  • 1.1- Send proyect to raspberry
  • 2.- then execute in raspberry with : dotnet WaitForExitAfterSocketTimeout.dll

Expected behavior
The application should return the serial number, the first time always is empty, but It is not the problem, the problem Is the "System.TimeoutException"

Error
https://i.imgur.com/jOtJ55L.png

Develop Dotnet
https://i.imgur.com/TapdHSh.png

Raspberry Runtime Dotnet
https://i.imgur.com/RKGn6WL.png

Best regards
If you need another information please tell me
Thanks! and great project

Unosquare.PiGpio - IO.GpioSetIsrFunc - VB.Net

Should add a quick project background. This program is a GUI for some machines, its written in VB.Net and includes MySQL functions. It uses Mono running on Raspbian 10 (Pi 4B). The app runs great, now I need to implement the I/O portion which requires 1 Interrupt and 1 Output. I can get output to work but need to figure out the Interrupt.
Using Visual Studio 2019.

Not sure if an issue or I'm just clearly missing something... I cant figure out how to setup the interrupts...
I have :

Imports Unosquare.PiGpio
Imports Unosquare.PiGpio.NativeEnums
Imports Unosquare.PiGpio.NativeMethods
Imports Unosquare.PiGpio.NativeTypes

Public Sub InitializeController()
Setup.GpioInitialise()
Dim pin = SystemGpio.Bcm17
IO.GpioSetMode(pin, PinMode.Input)
IO.GpioSetIsrFunc(pin, EdgeDetection.RisingEdge, 0, DisplayMessage)
End Sub

Public Sub DisplayMessage()
MsgBox("Interrupt Received on Pin 17")
End Sub

The callback (DisplayMessage) has an error as Expression does not produce a value...

I tried a function instead of a Sub, but same issue. Not sure how to use the PiGpioIsrDelegate which the documentation mentions...
Using Visual Studio 2019, VB.Net.
Sorry this seems like a really stupid question/issue lol...
I just need a nudge in the right direction in regards to settings up the Interrupt to call a specific Sub/Function.
Thanks in advance.

Setup.GpioInitialise() always returns InitFailed on Raspberry PI 3B+

Hi,

I always get InitFailed when calling Setup.GpioInitialise. Board.LibraryVersion returns 68. The pigpio daemon is running and I can use the command pigs so I am sure pigpio is working correctly.

But the Unosquare library can't initialize the library. What can I check to find the problem?
I am running Raspbian Buster.

Thanks,
Christian

Support pigpoid daemon

Add support for accessing pigpio via its daemon - no need for superuser rights and multiple applications can connect.

PR in progress.

Can this run in windows?

When I try to use this library in windows, I get this error:

Unable to load DLL 'libpigpio.so' or one of its dependencies.

Preparing multiple waves is not possible

I want to use the Unosquare.PiGpio.NativeMethods.Waves.GpioWaveChain method. This requires to prepare multiple waves and pass their wave id.
The issue is, that Unosquare.PiGpio.ManagedModel.WaveBuilder.Prepare is calling Unosquare.PiGpio.NativeMethods.Waves.GpioWaveClear which results in clearing the already prepared waves.
I would propose to remove the call Unosquare.PiGpio.NativeMethods.Waves.GpioWaveClear

sigHandler: Unhandled signal 30, terminating and sigHandler: Unhandled signal 2, terminating

I wrote an application on WinForm. What the program does. When pressing the touch button that is connected to BCM23, the LED lights on and off (BCM18).
I run this application with the command: sudo mono WindowsFormsApp6.exe
The program works for some time (about 3-5 minutes) and then freezes and writes an error:

321312

How can this problem be solved?
Help me please.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Unosquare.PiGpio.NativeMethods;
using Unosquare.PiGpio.NativeEnums;
using System.Threading;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Setup.GpioInitialise();
            var pin23 = SystemGpio.Bcm23;
            IO.GpioSetPullUpDown(pin23, GpioPullMode.Up);
            IO.GpioSetAlertFunc(UserGpio.Bcm23, triggerLED);
        }
        
        private void triggerLED(UserGpio userGpio, LevelChange levelChange, uint timeMicrosecs)
        {
            var LED_gpio = SystemGpio.Bcm18;
            if (levelChange == 0)
            {
                IO.GpioWrite(LED_gpio, true);
            }
            else
            {
                IO.GpioWrite(LED_gpio, false);
            }
        }
    }
}

Previously, I deduced in the textBox1:

private void triggerLED(UserGpio userGpio, LevelChange levelChange, uint timeMicrosecs)
        {
            var LED_gpio = SystemGpio.Bcm18;
            if (levelChange == 0)
            {
                IO.GpioWrite(LED_gpio, true);
                textBox1.Text += " gpio=" + userGpio + " level = " + levelChange + " tick =" + timeMicrosecs + '\r' + '\n';
            }
            else
            {
                IO.GpioWrite(LED_gpio, false);
                textBox1.Text += " gpio=" + userGpio + " level = " + levelChange + " tick =" + timeMicrosecs + '\r' + '\n';
            }
        }

but after a couple of button presses, the application hangs with an error:
sigHandler: Unhandled signal 30, terminating

EmptyWaveform error using managed

I cannot start a wave using managed code:


            Board.Waves.CreateWave.ClearPulses()
            For i = 0 To 2000
                Board.Waves.CreateWave.AddPulse(False, 100, NativeEnums.UserGpio.Bcm19)
                Board.Waves.CreateWave.AddPulse(True, 100, NativeEnums.UserGpio.Bcm19)
            Next

            Board.Waves.CreateWave.Send(NativeEnums.WaveMode.OneShot)

Produces the following error:
image

Using native calls it works:

            Dim pulseOn = New NativeTypes.GpioPulse With {
                .DurationMicroSecs = 100,
                .GpioOff = 0,
                .GpioOn = NativeEnums.BitMask.Bit19}

            Dim pulseOff = New NativeTypes.GpioPulse With {
                .DurationMicroSecs = 100,
                .GpioOff = NativeEnums.BitMask.Bit19,
                .GpioOn = 0}

            NativeMethods.Waves.GpioWaveClear()

            Dim pulses(1999) As NativeTypes.GpioPulse

            For i = 0 To 1999
                pulses(i) = pulseOff
                i += 1
                pulses(i) = pulseOn
            Next

            NativeMethods.Waves.GpioWaveAddGeneric(2000, pulses)

            Dim wid As Integer = NativeMethods.Waves.GpioWaveCreate()

            NativeMethods.Waves.GpioWaveTxSend(CUInt(wid), NativeEnums.WaveMode.OneShot)

Publish package to Nuget

Would you mind publishing the package to nuget? Otherwise projects which will make use of pigpio-dotnet will always contain hard copies of the whole project which in future will make it hard to keep them up to date

Passing null to gpioSetAlertFunction crashes the program

Stopping alerts makes the program crash (pigpio v67)

  1. Get the sample code and save it to test.cs (code below)
  2. Compile the program mcs test.cs
  3. Run the program and the following output is produced (output below)
  4. Ctrl+C does not work. Nothing else works. I have to start a new shell and kill the process manually.

test.cs

namespace Unosquare.PiGpio.HangExample
{
    using System;
    using System.Runtime.InteropServices;

    class PiGpioTest
    {
        public delegate void PiGpioAlertDelegate(uint userGpio, uint levelChange, uint ticks);

        public static void Main(string[] args)
        {
            // Init the library
            GpioInitialise();

            // Register an alert function
            GpioSetAlertFunc(4, (userGpio, levelChange, ticks) =>
            {
                Console.WriteLine($"GPIO: {userGpio} Level: {levelChange} Ticks: {ticks}");
            });

            // Set an alert watchdog of 1 second
            GpioSetWatchdog(4, 1000);

            // Wait for user input to stop alerts
            Console.ReadKey(intercept: true);
            GpioSetAlertFunc(4, null);
            Console.WriteLine("Correctly stopped alerts.");

            // Wait for the user to end the program gracefully
            Console.ReadKey(intercept: true);
            Console.WriteLine("Program stopped gracefully.");
        }

        [DllImport("libpigpio.so", EntryPoint = "gpioInitialise")]
        public static extern int GpioInitialise();

        [DllImport("libpigpio.so", EntryPoint = "gpioSetAlertFunc")]
        public static extern int GpioSetAlertFunc(uint userGpio, [In, MarshalAs(UnmanagedType.FunctionPtr)] PiGpioAlertDelegate callback);

        [DllImport("libpigpio.so", EntryPoint = "gpioSetWatchdog")]
        public static extern int GpioSetWatchdog(uint userGpio, uint timeoutMilliseconds);
    }
}

output:

pi@raspberrypi:~/pigpio-dotnet $ sudo mono test.exe
GPIO: 4 Level: 2 Ticks: 2063112400
GPIO: 4 Level: 2 Ticks: 2064112652
GPIO: 4 Level: 2 Ticks: 2065112905
GPIO: 4 Level: 2 Ticks: 2066113282
GPIO: 4 Level: 2 Ticks: 2067113535
GPIO: 4 Level: 1 Ticks: 2067530671
GPIO: 4 Level: 2 Ticks: 2068531289
GPIO: 4 Level: 2 Ticks: 2069531916
GPIO: 4 Level: 0 Ticks: 2069980377
GPIO: 4 Level: 1 Ticks: 2070152627
GPIO: 4 Level: 0 Ticks: 2070295198
GPIO: 4 Level: 1 Ticks: 2070470928
GPIO: 4 Level: 0 Ticks: 2070596259
GPIO: 4 Level: 1 Ticks: 2071118840
GPIO: 4 Level: 0 Ticks: 2071373096
GPIO: 4 Level: 1 Ticks: 2071373101
GPIO: 4 Level: 0 Ticks: 2071373131
GPIO: 4 Level: 1 Ticks: 2071811452
GPIO: 4 Level: 0 Ticks: 2072063727
GPIO: 4 Level: 2 Ticks: 2073064425
GPIO: 4 Level: 2 Ticks: 2074064677
GPIO: 4 Level: 2 Ticks: 2075065680
GPIO: 4 Level: 2 Ticks: 2076065682
GPIO: 4 Level: 2 Ticks: 2077066310
Correctly stopped alerts.
Program stopped gracefully.
2018-04-05 08:42:05 sigHandler: Unhandled signal 39, terminating

STATE CUE CARD: (? means a positive number, usually 1 or 2, * means any number)
        0x0     - starting (GOOD, unless the thread is running managed code)
        0x1     - running (BAD, unless it's the gc thread)
        0x2     - detached (GOOD, unless the thread is running managed code)
        0x?03   - async suspended (GOOD)
        0x?04   - self suspended (GOOD)
        0x?05   - async suspend requested (BAD)
        0x?06   - self suspend requested (BAD)
        0x*07   - blocking (GOOD)
        0x?08   - blocking with pending suspend (GOOD)
--thread 0x19eeec0 id 0xb43db470 [(nil)] state 105
--thread 0xb4800480 id 0xb6a57470 [(nil)] state 1
--thread 0x190edd8 id 0xb6f10000 [(nil)] state 1  GC INITIATOR
WAITING for 1 threads, got 0 suspended
suspend_thread suspend took 200 ms, which is more than the allowed 200 ms
2018-04-05 08:42:05 sigHandler: Unhandled signal 6, terminating

Connecting to pigpiod

Could you please implement a way of using pigpiod daemon instead of using the pipe interface?
When using the managed way you offer in your library, I did not found a way to chose between the two possibilities that PiGpio is offering.

Error when compiling with msbuild

I tried to compile the pigpio-dotnet/Unosquare.PiGpio/Unosquare.PiGpio.csproj with:
msbuild Unosquare.PiGpio.csproj

And then use the dll for my cs program:
mcs motor.cs -r:Unosquare.PiGpio.dll

However I got error with I target netcoreapp3.0:

error CS0012: The type `System.Enum' is defined in an assembly that is not referenced. Consider adding a reference to assembly `System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
error CS0012: The type `System.Object' is defined in an assembly that is not referenced. Consider adding a reference to assembly `System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

I tried to target netstandard2.0, the similar error:

error CS0012: The type `System.Enum' is defined in an assembly that is not referenced. Consider adding a reference to assembly `netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
error CS0012: The type `System.Object' is defined in an assembly that is not referenced. Consider adding a reference to assembly `netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

Any ideas will be appreciated :)

sudo needed for running PiGpio

The PiGpio is working very nice and fast, but I can't start NetCore code as normal user ("pi"), it needs "sudo" to work. Is there a solution to this issue? I've searched around, but I coudn't solve. Is there anyone can help?

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.