Giter Club home page Giter Club logo

wiringpi-dotnet's Introduction

THIS REPORT HAS BEEN ARCHIVED

Build status

wiringpi-dotnet wiringpi-dotnet

โญ Please star this project if you find it useful!

Provides complete managed access to the popular wiringpi C library

The default low-level provider is the wonderful WiringPi library available here. You do not need to install this library yourself. The Unosquare.WiringPi assembly will automatically extract the compiled binary of the library in the same path as the entry assembly.

Installation

Install basic Raspberry.IO package: NuGet version

PM> Install-Package Unosquare.Raspberry.IO

Install WiringPi implementation: NuGet version

PM> Install-Package Unosquare.WiringPi

Obtaining Board and System Information

RaspberryIO contains useful utilities to obtain information about the board it is running on. You can simply call the Pi.Info.ToString() method to obtain a dump of all system properties as a single string, or you can use the individual properties such as Installed RAM, Processor Count, Raspberry Pi Version, Serial Number, etc. There's not a lot more to this.

Please note Pi.Info depends on Wiring Pi, and the /proc/cpuinfo and /proc/meminfo files.

Using the GPIO Pins

Pin reference for the B plus (B+) - Header P1

BCM Name Mode V L R V Mode Name BCM
3.3v 01 02 5v
2 SDA.1 ALT0 1 03 04 5V
3 SCL.1 ALT0 1 05 06 GND
4 GPIO. 7 IN 1 07 08 1 ALT0 TxD 14
GND 09 10 1 ALT0 RxD 15
17 GPIO. 0 IN 0 11 12 0 IN GPIO. 1 18
27 GPIO. 2 IN 0 13 14 GND
22 GPIO. 3 IN 0 15 16 0 IN GPIO. 4 23
3.3v 17 18 0 IN GPIO. 5 24
10 MOSI IN 0 19 20 GND
9 MISO IN 0 21 22 0 IN GPIO. 6 25
11 SCLK IN 0 23 24 1 IN CE0 8
GND 25 26 1 IN CE1 7
0 SDA.0 IN 1 27 28 1 IN SCL.0 1
5 GPIO.21 IN 1 29 30 GND
6 GPIO.22 IN 1 31 32 0 IN GPIO.26 12
13 GPIO.23 IN 0 33 34 GND
19 GPIO.24 IN 0 35 36 0 IN GPIO.27 16
26 GPIO.25 IN 0 37 38 0 IN GPIO.28 20
GND 39 40 0 IN GPIO.29 21

The above diagram shows the pins of GPIO Header P1. There is an additional GPIO header on the Pi called P5. More info available here

In order to access the pins, use Pi.Gpio. The pins can have multiple behaviors and fortunately Pi.Gpio can be iterated, addressed by index, addressed by BCM pin number and provides the pins as publicly accessible properties.

Here is an example of addressing the pins in all the various ways:

public static void TestLedBlinking()
{
    // Get a reference to the pin you need to use.
    // All 3 methods below are exactly equivalent
    var blinkingPin = Pi.Gpio[17];
    blinkingPin = Pi.Gpio[BcmPin.Gpio17];
    blinkingPin = Pi.Gpio.Pin17;

    // Configure the pin as an output
    blinkingPin.PinMode = GpioPinDriveMode.Output;

    // perform writes to the pin by toggling the isOn variable
    var isOn = false;
    for (var i = 0; i < 20; i++)
    {
        isOn = !isOn;
        blinkingPin.Write(isOn);
        System.Threading.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

wiringpi-dotnet's People

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

wiringpi-dotnet's Issues

.NET 5 Single file deployment throws exception

Since .NET 5 the Assembly.Location API returns an empty string when the app is deployed as single file executable.
https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file

This prevents the library from using with a single file deployment.

Error
The basePath variable will be empty.

var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

Path.Combine later on will throw an exception.
var targetPath = Path.Combine(basePath, filename);

Exception

System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
   at System.IO.Path.Combine(String path1, String path2)
   at Unosquare.WiringPi.Resources.EmbeddedResources.ExtractAll()
   at Unosquare.WiringPi.BootstrapWiringPi.Bootstrap()
   at Unosquare.RaspberryIO.Pi.Init[T]()
   at HorseFeeder.Raspberry.InitializeService.InitializeRaspberryPi()

Possible solution
I'm not 100% sure but maybe it's possible to use AppContext.BaseDirectory to get the basePath.
https://docs.microsoft.com/en-us/dotnet/api/system.appcontext.basedirectory?view=net-5.0#System_AppContext_BaseDirectory

GPIO BCM14/15 do not have GP capability even when UART disabled

Describe the bug
GPIO BCM 14 and 15 only have the UARTTXD / UARTRXD capabilities, even when the UART is disabled with enable_uart=0 in config.txt. This prevents use as regular GPIO pins.

To Reproduce

  1. Disable the UART on pins BCM14/15 in config.txt with enable_uart=0.
  2. Attempt to set Pin Mode to Input or Output using Pi.GPIO[14].PinMode = GpioPinDriveMode.Input
  3. Receive System.NotSupportedException: Pin 14 'BCM 14 (TXD)' does not support mode 'Input'. Pin capabilities are limited to: UARTTXD
  4. Using the gpio(1) command-line tool on the Pi, run gpio -g mode 14 in to set the pin mode to input
  5. Verify signals can be read using gpio readall

Expected behavior
When the UART is not using pins BCM14/15, it should be possible to use these as general purpose I/O pins.

I'm very happy to submit a PR to just add the GP capability to those pins, but I wasn't sure if you wanted to do some runtime detection of whether the UART is enabled before allowing it (i.e. only report that capability if the UART is not enabled), or whether it's acceptable to always report the capability, because the underlying hardware possesses it.

Environment:

  • OS: Raspbian GNU/Linux 10 (buster)
  • .NET 5.0
  • Unosquare.WiringPi 0.5.1
  • Unosquare.Raspberry.IO 0.27.1
  • Raspberry Pi 3

II2CDevice.Read(int length) doesn't seem to work

Please bear with me.

I'm trying to use the II2CDevice.Read(int length) method to bring back a six-byte array off the bus. The behavior, though, is that it is bringing back six copies of the FIRST byte of the bus. I've tried this many times, many ways, and all I get back is six of the same byte. Note also, this is a purchased I2C device (a Thunderborg) so I am unable to change its behavior. I've seen in a similar bug elsewhere that the solution was to change a connected Arduino's code to work in a specific way. I've also seen that I should be able to access the underlying WiringPi.Read(int fd, int length) type function, but that is beyond my ken at the moment.

To Reproduce
Steps to reproduce the behavior:

for (int kb = 0; kb < splash.Length; kb++)
{
     log.WriteLog("Found: " + splash[kb].ToString("X2");
}

Expected behavior
This SHOULD produce 0x99/0x15/0x00/0x00/0x00/0x00

Screenshots
It produces:

16/03/2021 22:09:06: Found: 99
16/03/2021 22:09:06: Found: 99
16/03/2021 22:09:06: Found: 99
16/03/2021 22:09:06: Found: 99
16/03/2021 22:09:06: Found: 99

Desktop (please complete the following information):

  • Raspberry Pi 3
  • .NET IoT 3.1

Additional context
Any help would be appreciated.

Enable setting GpioPin.PwmRegister prior to changing pin mode

Hi guys!
Been playing with hardware PWM on RPi 3B and encountered this issue:
When switching GpioPin.PinMode to GpioPinDriveMode.PwmOutput, the underlying WiringPi library immediately starts the clock. That's probably the right thing to do, as long as one gets a chance to set the desired duty cycle first. That could be done by calling pwmWrite(pin, val) which doesn't care if a pin is not yet in PWM mode. However, the wrapping setter in GpioPin.PwmRegister throws InvalidOperationException because the pin hasn't been switched to PwmOutput. But if we switch the pin to PwmOutput, the clock is started and the default range and divisor kick in immediately, producing an "arbitrary" burst signal for at least the 100 microsecond delay that is hard-coded in WiringPi, before any code gets a chance to adjust those parameters and set register value to the desired duty cycle. The way it manifests itself with an LED is a short burst of light until the proper duty cycle gets set.
Having the mode check in the PwmRegister property setter commented out and setting a value prior to switching pin mode, I was finally able to start PWM with any desired duty cycle (e.g. a flat-line zero).
I believe this is important if one wants to get a predictable PWM output right off the bat when starting a motor at a low speed or slowly lighting up an LED without getting an uncontrolled initial "burst".
Let me know what you think. - Advance thanks!

Adding Bouncetime to RegisterInterruptCallback()

I am porting some code from python to c#.

I have a simple setup using a RP3 with a SenseHat and an external Button (wired between pin 16 and GND ).

In python I used the following code to achive a bounce free function call after a button press:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(16,GPIO.IN,pull_up_down=GPIO.PUD_UP)

def nextTick(*args)
  # do my stuff here

GPIO.add_event_detect(16,GPIO.FALLING,callback=nextTick,bouncetime=200)

with your libary i tried:

Pi.Init<BootstrapWiringPi>();
var pin = Pi.Gpio[16];
pin.PinMode = GpioPinDriveMode.Input;
pin.InputPullMode = GpioPinResistorPullMode.PullUp;
pin.RegisterInterruptCallback(EdgeDetection.FallingEdge, ButtonCallback);

this works for detecting the button press, but since the button is quiet cheap I get up to 10 function calls each time i press the button.

Would it be possible / or is it allready possible to add a bouncetime to RegisterInterruptCallback() to drop incomming interrupts in a given time window to remove bounce from the button?

The name 'Pi' does not exist in the current context

I tried to use your example:
// All 3 methods below are exactly equivalent
var blinkingPin = Pi.Gpio[17];
blinkingPin = Pi.Gpio[BcmPin.Gpio17];
blinkingPin = Pi.Gpio.Pin17;
There is only Unosquare.WiringPi namespace, which does not have "Pi" class

IGpioPin does not allow access to PWM functions

IGpioPin does not give access to all PWM functions.

In Workbench, the Servo demo should take an IGpioPin in the constructor, not a GpioPin.

Alternatively explain in docs how to get a GpioPin instance from
Pi.Gpio[bcmPinNumber]

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.