Codestyling...

This commit is contained in:
BlubbFish 2019-12-06 21:09:52 +01:00
parent 800b81d88f
commit f9015f8022
21 changed files with 2883 additions and 3036 deletions

View File

@ -1,15 +1,17 @@
namespace Unosquare.WiringPi using System;
{
using RaspberryIO.Abstractions;
using Swan.DependencyInjection;
/// <summary> using Swan.DependencyInjection;
/// Represents the Bootstrap class to extract resources.
/// </summary> using Unosquare.RaspberryIO.Abstractions;
/// <seealso cref="Unosquare.RaspberryIO.Abstractions.IBootstrap" />
public class BootstrapWiringPi : IBootstrap namespace Unosquare.WiringPi {
/// <summary>
/// Represents the Bootstrap class to extract resources.
/// </summary>
/// <seealso cref="Unosquare.RaspberryIO.Abstractions.IBootstrap" />
public class BootstrapWiringPi : IBootstrap
{ {
private static readonly object SyncLock = new object(); private static readonly Object SyncLock = new Object();
/// <inheritdoc /> /// <inheritdoc />
public void Bootstrap() public void Bootstrap()

View File

@ -1,289 +1,284 @@
namespace Unosquare.WiringPi using System;
{
using System; namespace Unosquare.WiringPi {
/// <summary>
/// Defines all the available Wiring Pi Pin Numbers.
/// </summary>
public enum WiringPiPin {
/// <summary>
/// Unknown WiringPi pin.
/// </summary>
Unknown = -1,
/// <summary> /// <summary>
/// Defines all the available Wiring Pi Pin Numbers. /// WiringPi pin 0.
/// </summary> /// </summary>
public enum WiringPiPin Pin00 = 0,
{
/// <summary>
/// Unknown WiringPi pin.
/// </summary>
Unknown = -1,
/// <summary>
/// WiringPi pin 0.
/// </summary>
Pin00 = 0,
/// <summary>
/// WiringPi pin 1.
/// </summary>
Pin01 = 1,
/// <summary>
/// WiringPi pin 2.
/// </summary>
Pin02 = 2,
/// <summary>
/// WiringPi pin 3.
/// </summary>
Pin03 = 3,
/// <summary>
/// WiringPi pin 4.
/// </summary>
Pin04 = 4,
/// <summary>
/// WiringPi pin 5.
/// </summary>
Pin05 = 5,
/// <summary>
/// WiringPi pin 6.
/// </summary>
Pin06 = 6,
/// <summary>
/// WiringPi pin 7.
/// </summary>
Pin07 = 7,
/// <summary>
/// WiringPi pin 8.
/// </summary>
Pin08 = 8,
/// <summary>
/// WiringPi pin 9.
/// </summary>
Pin09 = 9,
/// <summary>
/// WiringPi pin 10.
/// </summary>
Pin10 = 10,
/// <summary>
/// WiringPi pin 11.
/// </summary>
Pin11 = 11,
/// <summary>
/// WiringPi pin 12.
/// </summary>
Pin12 = 12,
/// <summary>
/// WiringPi pin 13.
/// </summary>
Pin13 = 13,
/// <summary>
/// WiringPi pin 14.
/// </summary>
Pin14 = 14,
/// <summary>
/// WiringPi pin 15.
/// </summary>
Pin15 = 15,
/// <summary>
/// WiringPi pin 16.
/// </summary>
Pin16 = 16,
/// <summary>
/// WiringPi pin 17.
/// </summary>
Pin17 = 17,
/// <summary>
/// WiringPi pin 18.
/// </summary>
Pin18 = 18,
/// <summary>
/// WiringPi pin 19.
/// </summary>
Pin19 = 19,
/// <summary>
/// WiringPi pin 20.
/// </summary>
Pin20 = 20,
/// <summary>
/// WiringPi pin 21.
/// </summary>
Pin21 = 21,
/// <summary>
/// WiringPi pin 22.
/// </summary>
Pin22 = 22,
/// <summary>
/// WiringPi pin 23.
/// </summary>
Pin23 = 23,
/// <summary>
/// WiringPi pin 24.
/// </summary>
Pin24 = 24,
/// <summary>
/// WiringPi pin 25.
/// </summary>
Pin25 = 25,
/// <summary>
/// WiringPi pin 26.
/// </summary>
Pin26 = 26,
/// <summary>
/// WiringPi pin 27.
/// </summary>
Pin27 = 27,
/// <summary>
/// WiringPi pin 28.
/// </summary>
Pin28 = 28,
/// <summary>
/// WiringPi pin 29.
/// </summary>
Pin29 = 29,
/// <summary>
/// WiringPi pin 30.
/// </summary>
Pin30 = 30,
/// <summary>
/// WiringPi pin 31.
/// </summary>
Pin31 = 31,
}
/// <summary> /// <summary>
/// Defines the different pin capabilities. /// WiringPi pin 1.
/// </summary> /// </summary>
[Flags] Pin01 = 1,
public enum PinCapability
{
/// <summary>
/// General Purpose capability: Digital and Analog Read/Write
/// </summary>
GP = 0x01,
/// <summary>
/// General Purpose Clock (not PWM)
/// </summary>
GPCLK = 0x02,
/// <summary>
/// i2c data channel
/// </summary>
I2CSDA = 0x04,
/// <summary>
/// i2c clock channel
/// </summary>
I2CSCL = 0x08,
/// <summary>
/// SPI Master Out, Slave In channel
/// </summary>
SPIMOSI = 0x10,
/// <summary>
/// SPI Master In, Slave Out channel
/// </summary>
SPIMISO = 0x20,
/// <summary>
/// SPI Clock channel
/// </summary>
SPICLK = 0x40,
/// <summary>
/// SPI Chip Select Channel
/// </summary>
SPICS = 0x80,
/// <summary>
/// UART Request to Send Channel
/// </summary>
UARTRTS = 0x100,
/// <summary>
/// UART Transmit Channel
/// </summary>
UARTTXD = 0x200,
/// <summary>
/// UART Receive Channel
/// </summary>
UARTRXD = 0x400,
/// <summary>
/// Hardware Pule Width Modulation
/// </summary>
PWM = 0x800,
}
/// <summary> /// <summary>
/// The PWM mode. /// WiringPi pin 2.
/// </summary> /// </summary>
public enum PwmMode Pin02 = 2,
{
/// <summary>
/// PWM pulses are sent using mark-sign patterns (old school)
/// </summary>
MarkSign = 0,
/// <summary>
/// PWM pulses are sent as a balanced signal (default, newer mode)
/// </summary>
Balanced = 1,
}
/// <summary> /// <summary>
/// Defines GPIO controller initialization modes. /// WiringPi pin 3.
/// </summary> /// </summary>
internal enum ControllerMode Pin03 = 3,
{
/// <summary>
/// The not initialized
/// </summary>
NotInitialized,
/// <summary> /// <summary>
/// The direct with wiring pi pins /// WiringPi pin 4.
/// </summary> /// </summary>
DirectWithWiringPiPins, Pin04 = 4,
/// <summary> /// <summary>
/// The direct with BCM pins /// WiringPi pin 5.
/// </summary> /// </summary>
DirectWithBcmPins, Pin05 = 5,
/// <summary> /// <summary>
/// The direct with header pins /// WiringPi pin 6.
/// </summary> /// </summary>
DirectWithHeaderPins, Pin06 = 6,
/// <summary> /// <summary>
/// The file stream with hardware pins /// WiringPi pin 7.
/// </summary> /// </summary>
FileStreamWithHardwarePins, Pin07 = 7,
}
/// <summary>
/// WiringPi pin 8.
/// </summary>
Pin08 = 8,
/// <summary>
/// WiringPi pin 9.
/// </summary>
Pin09 = 9,
/// <summary>
/// WiringPi pin 10.
/// </summary>
Pin10 = 10,
/// <summary>
/// WiringPi pin 11.
/// </summary>
Pin11 = 11,
/// <summary>
/// WiringPi pin 12.
/// </summary>
Pin12 = 12,
/// <summary>
/// WiringPi pin 13.
/// </summary>
Pin13 = 13,
/// <summary>
/// WiringPi pin 14.
/// </summary>
Pin14 = 14,
/// <summary>
/// WiringPi pin 15.
/// </summary>
Pin15 = 15,
/// <summary>
/// WiringPi pin 16.
/// </summary>
Pin16 = 16,
/// <summary>
/// WiringPi pin 17.
/// </summary>
Pin17 = 17,
/// <summary>
/// WiringPi pin 18.
/// </summary>
Pin18 = 18,
/// <summary>
/// WiringPi pin 19.
/// </summary>
Pin19 = 19,
/// <summary>
/// WiringPi pin 20.
/// </summary>
Pin20 = 20,
/// <summary>
/// WiringPi pin 21.
/// </summary>
Pin21 = 21,
/// <summary>
/// WiringPi pin 22.
/// </summary>
Pin22 = 22,
/// <summary>
/// WiringPi pin 23.
/// </summary>
Pin23 = 23,
/// <summary>
/// WiringPi pin 24.
/// </summary>
Pin24 = 24,
/// <summary>
/// WiringPi pin 25.
/// </summary>
Pin25 = 25,
/// <summary>
/// WiringPi pin 26.
/// </summary>
Pin26 = 26,
/// <summary>
/// WiringPi pin 27.
/// </summary>
Pin27 = 27,
/// <summary>
/// WiringPi pin 28.
/// </summary>
Pin28 = 28,
/// <summary>
/// WiringPi pin 29.
/// </summary>
Pin29 = 29,
/// <summary>
/// WiringPi pin 30.
/// </summary>
Pin30 = 30,
/// <summary>
/// WiringPi pin 31.
/// </summary>
Pin31 = 31,
}
/// <summary>
/// Defines the different pin capabilities.
/// </summary>
[Flags]
public enum PinCapability {
/// <summary>
/// General Purpose capability: Digital and Analog Read/Write
/// </summary>
GP = 0x01,
/// <summary>
/// General Purpose Clock (not PWM)
/// </summary>
GPCLK = 0x02,
/// <summary>
/// i2c data channel
/// </summary>
I2CSDA = 0x04,
/// <summary>
/// i2c clock channel
/// </summary>
I2CSCL = 0x08,
/// <summary>
/// SPI Master Out, Slave In channel
/// </summary>
SPIMOSI = 0x10,
/// <summary>
/// SPI Master In, Slave Out channel
/// </summary>
SPIMISO = 0x20,
/// <summary>
/// SPI Clock channel
/// </summary>
SPICLK = 0x40,
/// <summary>
/// SPI Chip Select Channel
/// </summary>
SPICS = 0x80,
/// <summary>
/// UART Request to Send Channel
/// </summary>
UARTRTS = 0x100,
/// <summary>
/// UART Transmit Channel
/// </summary>
UARTTXD = 0x200,
/// <summary>
/// UART Receive Channel
/// </summary>
UARTRXD = 0x400,
/// <summary>
/// Hardware Pule Width Modulation
/// </summary>
PWM = 0x800,
}
/// <summary>
/// The PWM mode.
/// </summary>
public enum PwmMode {
/// <summary>
/// PWM pulses are sent using mark-sign patterns (old school)
/// </summary>
MarkSign = 0,
/// <summary>
/// PWM pulses are sent as a balanced signal (default, newer mode)
/// </summary>
Balanced = 1,
}
/// <summary>
/// Defines GPIO controller initialization modes.
/// </summary>
internal enum ControllerMode {
/// <summary>
/// The not initialized
/// </summary>
NotInitialized,
/// <summary>
/// The direct with wiring pi pins
/// </summary>
DirectWithWiringPiPins,
/// <summary>
/// The direct with BCM pins
/// </summary>
DirectWithBcmPins,
/// <summary>
/// The direct with header pins
/// </summary>
DirectWithHeaderPins,
/// <summary>
/// The file stream with hardware pins
/// </summary>
FileStreamWithHardwarePins,
}
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,200 +1,167 @@
namespace Unosquare.WiringPi using System;
{
using RaspberryIO.Abstractions;
using System;
public partial class GpioPin using Unosquare.RaspberryIO.Abstractions;
{
internal static readonly Lazy<GpioPin> Pin00 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio00)
{
Capabilities = PinCapability.GP | PinCapability.I2CSDA,
Name = $"BCM 0 {(SystemInfo.GetBoardRevision() == BoardRevision.Rev1 ? "(SDA)" : "(ID_SD)")}",
});
internal static readonly Lazy<GpioPin> Pin01 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio01) namespace Unosquare.WiringPi {
{ public partial class GpioPin {
Capabilities = PinCapability.GP | PinCapability.I2CSCL, internal static readonly Lazy<GpioPin> Pin00 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio00) {
Name = $"BCM 1 {(SystemInfo.GetBoardRevision() == BoardRevision.Rev1 ? "(SCL)" : "(ID_SC)")}", Capabilities = PinCapability.GP | PinCapability.I2CSDA,
}); Name = $"BCM 0 {(SystemInfo.GetBoardRevision() == BoardRevision.Rev1 ? "(SDA)" : "(ID_SD)")}",
});
internal static readonly Lazy<GpioPin> Pin02 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio02) internal static readonly Lazy<GpioPin> Pin01 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio01) {
{ Capabilities = PinCapability.GP | PinCapability.I2CSCL,
Capabilities = PinCapability.GP | PinCapability.I2CSDA, Name = $"BCM 1 {(SystemInfo.GetBoardRevision() == BoardRevision.Rev1 ? "(SCL)" : "(ID_SC)")}",
Name = "BCM 2 (SDA)", });
});
internal static readonly Lazy<GpioPin> Pin03 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio03) internal static readonly Lazy<GpioPin> Pin02 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio02) {
{ Capabilities = PinCapability.GP | PinCapability.I2CSDA,
Capabilities = PinCapability.GP | PinCapability.I2CSCL, Name = "BCM 2 (SDA)",
Name = "BCM 3 (SCL)", });
});
internal static readonly Lazy<GpioPin> Pin04 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio04) internal static readonly Lazy<GpioPin> Pin03 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio03) {
{ Capabilities = PinCapability.GP | PinCapability.I2CSCL,
Capabilities = PinCapability.GP | PinCapability.GPCLK, Name = "BCM 3 (SCL)",
Name = "BCM 4 (GPCLK0)", });
});
internal static readonly Lazy<GpioPin> Pin05 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio05) internal static readonly Lazy<GpioPin> Pin04 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio04) {
{ Capabilities = PinCapability.GP | PinCapability.GPCLK,
Capabilities = PinCapability.GP, Name = "BCM 4 (GPCLK0)",
Name = "BCM 5", });
});
internal static readonly Lazy<GpioPin> Pin06 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio06) internal static readonly Lazy<GpioPin> Pin05 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio05) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP, Name = "BCM 5",
Name = "BCM 6", });
});
internal static readonly Lazy<GpioPin> Pin07 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio07) internal static readonly Lazy<GpioPin> Pin06 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio06) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP | PinCapability.SPICS, Name = "BCM 6",
Name = "BCM 7 (CE1)", });
});
internal static readonly Lazy<GpioPin> Pin08 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio08) internal static readonly Lazy<GpioPin> Pin07 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio07) {
{ Capabilities = PinCapability.GP | PinCapability.SPICS,
Capabilities = PinCapability.GP | PinCapability.SPICS, Name = "BCM 7 (CE1)",
Name = "BCM 8 (CE0)", });
});
internal static readonly Lazy<GpioPin> Pin09 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio09) internal static readonly Lazy<GpioPin> Pin08 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio08) {
{ Capabilities = PinCapability.GP | PinCapability.SPICS,
Capabilities = PinCapability.GP | PinCapability.SPIMISO, Name = "BCM 8 (CE0)",
Name = "BCM 9 (MISO)", });
});
internal static readonly Lazy<GpioPin> Pin10 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio10) internal static readonly Lazy<GpioPin> Pin09 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio09) {
{ Capabilities = PinCapability.GP | PinCapability.SPIMISO,
Capabilities = PinCapability.GP | PinCapability.SPIMOSI, Name = "BCM 9 (MISO)",
Name = "BCM 10 (MOSI)", });
});
internal static readonly Lazy<GpioPin> Pin11 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio11) internal static readonly Lazy<GpioPin> Pin10 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio10) {
{ Capabilities = PinCapability.GP | PinCapability.SPIMOSI,
Capabilities = PinCapability.GP | PinCapability.SPICLK, Name = "BCM 10 (MOSI)",
Name = "BCM 11 (SCLCK)", });
});
internal static readonly Lazy<GpioPin> Pin12 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio12) internal static readonly Lazy<GpioPin> Pin11 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio11) {
{ Capabilities = PinCapability.GP | PinCapability.SPICLK,
Capabilities = PinCapability.GP | PinCapability.PWM, Name = "BCM 11 (SCLCK)",
Name = "BCM 12 (PWM0)", });
});
internal static readonly Lazy<GpioPin> Pin13 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio13) internal static readonly Lazy<GpioPin> Pin12 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio12) {
{ Capabilities = PinCapability.GP | PinCapability.PWM,
Capabilities = PinCapability.GP | PinCapability.PWM, Name = "BCM 12 (PWM0)",
Name = "BCM 13 (PWM1)", });
});
internal static readonly Lazy<GpioPin> Pin14 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio14) internal static readonly Lazy<GpioPin> Pin13 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio13) {
{ Capabilities = PinCapability.GP | PinCapability.PWM,
Capabilities = PinCapability.UARTTXD, Name = "BCM 13 (PWM1)",
Name = "BCM 14 (TXD)", });
});
internal static readonly Lazy<GpioPin> Pin15 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio15) internal static readonly Lazy<GpioPin> Pin14 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio14) {
{ Capabilities = PinCapability.UARTTXD,
Capabilities = PinCapability.UARTRXD, Name = "BCM 14 (TXD)",
Name = "BCM 15 (RXD)", });
});
internal static readonly Lazy<GpioPin> Pin16 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio16) internal static readonly Lazy<GpioPin> Pin15 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio15) {
{ Capabilities = PinCapability.UARTRXD,
Capabilities = PinCapability.GP, Name = "BCM 15 (RXD)",
Name = "BCM 16", });
});
internal static readonly Lazy<GpioPin> Pin17 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio17) internal static readonly Lazy<GpioPin> Pin16 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio16) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP | PinCapability.UARTRTS, Name = "BCM 16",
Name = "BCM 17", });
});
internal static readonly Lazy<GpioPin> Pin18 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio18) internal static readonly Lazy<GpioPin> Pin17 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio17) {
{ Capabilities = PinCapability.GP | PinCapability.UARTRTS,
Capabilities = PinCapability.GP | PinCapability.PWM, Name = "BCM 17",
Name = "BCM 18 (PWM0)", });
});
internal static readonly Lazy<GpioPin> Pin19 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio19) internal static readonly Lazy<GpioPin> Pin18 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio18) {
{ Capabilities = PinCapability.GP | PinCapability.PWM,
Capabilities = PinCapability.GP | PinCapability.PWM | PinCapability.SPIMISO, Name = "BCM 18 (PWM0)",
Name = "BCM 19 (MISO)", });
});
internal static readonly Lazy<GpioPin> Pin20 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio20) internal static readonly Lazy<GpioPin> Pin19 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio19) {
{ Capabilities = PinCapability.GP | PinCapability.PWM | PinCapability.SPIMISO,
Capabilities = PinCapability.GP | PinCapability.SPIMOSI, Name = "BCM 19 (MISO)",
Name = "BCM 20 (MOSI)", });
});
internal static readonly Lazy<GpioPin> Pin21 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio21) internal static readonly Lazy<GpioPin> Pin20 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio20) {
{ Capabilities = PinCapability.GP | PinCapability.SPIMOSI,
Capabilities = PinCapability.GP | PinCapability.SPICLK, Name = "BCM 20 (MOSI)",
Name = $"BCM 21{(SystemInfo.GetBoardRevision() == BoardRevision.Rev1 ? string.Empty : " (SCLK)")}", });
});
internal static readonly Lazy<GpioPin> Pin22 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio22) internal static readonly Lazy<GpioPin> Pin21 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio21) {
{ Capabilities = PinCapability.GP | PinCapability.SPICLK,
Capabilities = PinCapability.GP, Name = $"BCM 21{(SystemInfo.GetBoardRevision() == BoardRevision.Rev1 ? String.Empty : " (SCLK)")}",
Name = "BCM 22", });
});
internal static readonly Lazy<GpioPin> Pin23 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio23) internal static readonly Lazy<GpioPin> Pin22 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio22) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP, Name = "BCM 22",
Name = "BCM 23", });
});
internal static readonly Lazy<GpioPin> Pin24 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio24) internal static readonly Lazy<GpioPin> Pin23 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio23) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP, Name = "BCM 23",
Name = "BCM 24", });
});
internal static readonly Lazy<GpioPin> Pin25 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio25) internal static readonly Lazy<GpioPin> Pin24 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio24) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP, Name = "BCM 24",
Name = "BCM 25", });
});
internal static readonly Lazy<GpioPin> Pin26 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio26) internal static readonly Lazy<GpioPin> Pin25 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio25) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP, Name = "BCM 25",
Name = "BCM 26", });
});
internal static readonly Lazy<GpioPin> Pin27 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio27) internal static readonly Lazy<GpioPin> Pin26 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio26) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP, Name = "BCM 26",
Name = "BCM 27", });
});
internal static readonly Lazy<GpioPin> Pin28 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio28) internal static readonly Lazy<GpioPin> Pin27 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio27) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP | PinCapability.I2CSDA, Name = "BCM 27",
Name = "BCM 28 (SDA)", });
});
internal static readonly Lazy<GpioPin> Pin29 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio29) internal static readonly Lazy<GpioPin> Pin28 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio28) {
{ Capabilities = PinCapability.GP | PinCapability.I2CSDA,
Capabilities = PinCapability.GP | PinCapability.I2CSCL, Name = "BCM 28 (SDA)",
Name = "BCM 29 (SCL)", });
});
internal static readonly Lazy<GpioPin> Pin30 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio30) internal static readonly Lazy<GpioPin> Pin29 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio29) {
{ Capabilities = PinCapability.GP | PinCapability.I2CSCL,
Capabilities = PinCapability.GP, Name = "BCM 29 (SCL)",
Name = "BCM 30", });
});
internal static readonly Lazy<GpioPin> Pin31 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio31) internal static readonly Lazy<GpioPin> Pin30 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio30) {
{ Capabilities = PinCapability.GP,
Capabilities = PinCapability.GP, Name = "BCM 30",
Name = "BCM 31", });
});
} internal static readonly Lazy<GpioPin> Pin31 = new Lazy<GpioPin>(() => new GpioPin(BcmPin.Gpio31) {
Capabilities = PinCapability.GP,
Name = "BCM 31",
});
}
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,72 +1,70 @@
namespace Unosquare.WiringPi using System;
{ using System.Collections.Generic;
using Native; using System.Collections.ObjectModel;
using RaspberryIO.Abstractions; using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel; using Unosquare.RaspberryIO.Abstractions;
using System.Linq;
namespace Unosquare.WiringPi {
/// <inheritdoc />
/// <summary>
/// A simple wrapper for the I2c bus on the Raspberry Pi.
/// </summary>
public class I2CBus : II2CBus {
// TODO: It would be nice to integrate i2c device detection.
private static readonly Object SyncRoot = new Object();
private readonly Dictionary<Int32, II2CDevice> _devices = new Dictionary<Int32, II2CDevice>();
/// <inheritdoc /> /// <inheritdoc />
/// <summary> public ReadOnlyCollection<II2CDevice> Devices {
/// A simple wrapper for the I2c bus on the Raspberry Pi. get {
/// </summary> lock(SyncRoot) {
public class I2CBus : II2CBus return new ReadOnlyCollection<II2CDevice>(this._devices.Values.ToArray());
{
// TODO: It would be nice to integrate i2c device detection.
private static readonly object SyncRoot = new object();
private readonly Dictionary<int, II2CDevice> _devices = new Dictionary<int, II2CDevice>();
/// <inheritdoc />
public ReadOnlyCollection<II2CDevice> Devices
{
get
{
lock (SyncRoot)
return new ReadOnlyCollection<II2CDevice>(_devices.Values.ToArray());
}
}
/// <inheritdoc />
public II2CDevice this[int deviceId] => GetDeviceById(deviceId);
/// <inheritdoc />
public II2CDevice GetDeviceById(int deviceId)
{
lock (SyncRoot)
return _devices[deviceId];
}
/// <inheritdoc />
/// <exception cref="KeyNotFoundException">When the device file descriptor is not found.</exception>
public II2CDevice AddDevice(int deviceId)
{
lock (SyncRoot)
{
if (_devices.ContainsKey(deviceId))
return _devices[deviceId];
var fileDescriptor = SetupFileDescriptor(deviceId);
if (fileDescriptor < 0)
throw new KeyNotFoundException($"Device with id {deviceId} could not be registered with the I2C bus. Error Code: {fileDescriptor}.");
var device = new I2CDevice(deviceId, fileDescriptor);
_devices[deviceId] = device;
return device;
}
}
/// <summary>
/// This initializes the I2C system with your given device identifier.
/// The ID is the I2C number of the device and you can use the i2cdetect program to find this out.
/// wiringPiI2CSetup() will work out which revision Raspberry Pi you have and open the appropriate device in /dev.
/// The return value is the standard Linux filehandle, or -1 if any error in which case, you can consult errno as usual.
/// </summary>
/// <param name="deviceId">The device identifier.</param>
/// <returns>The Linux file handle.</returns>
private static int SetupFileDescriptor(int deviceId)
{
lock (SyncRoot)
return WiringPi.WiringPiI2CSetup(deviceId);
} }
}
} }
/// <inheritdoc />
public II2CDevice this[Int32 deviceId] => this.GetDeviceById(deviceId);
/// <inheritdoc />
public II2CDevice GetDeviceById(Int32 deviceId) {
lock(SyncRoot) {
return this._devices[deviceId];
}
}
/// <inheritdoc />
/// <exception cref="KeyNotFoundException">When the device file descriptor is not found.</exception>
public II2CDevice AddDevice(Int32 deviceId) {
lock(SyncRoot) {
if(this._devices.ContainsKey(deviceId)) {
return this._devices[deviceId];
}
Int32 fileDescriptor = SetupFileDescriptor(deviceId);
if(fileDescriptor < 0) {
throw new KeyNotFoundException($"Device with id {deviceId} could not be registered with the I2C bus. Error Code: {fileDescriptor}.");
}
I2CDevice device = new I2CDevice(deviceId, fileDescriptor);
this._devices[deviceId] = device;
return device;
}
}
/// <summary>
/// This initializes the I2C system with your given device identifier.
/// The ID is the I2C number of the device and you can use the i2cdetect program to find this out.
/// wiringPiI2CSetup() will work out which revision Raspberry Pi you have and open the appropriate device in /dev.
/// The return value is the standard Linux filehandle, or -1 if any error in which case, you can consult errno as usual.
/// </summary>
/// <param name="deviceId">The device identifier.</param>
/// <returns>The Linux file handle.</returns>
private static Int32 SetupFileDescriptor(Int32 deviceId) {
lock(SyncRoot) {
return Native.WiringPi.WiringPiI2CSetup(deviceId);
}
}
}
} }

View File

@ -1,181 +1,182 @@
namespace Unosquare.WiringPi using System;
{ using System.Threading.Tasks;
using Native;
using RaspberryIO.Abstractions; using Unosquare.RaspberryIO.Abstractions;
using RaspberryIO.Abstractions.Native; using Unosquare.RaspberryIO.Abstractions.Native;
using System;
using System.Threading.Tasks; namespace Unosquare.WiringPi {
/// <summary>
/// Represents a device on the I2C Bus.
/// </summary>
public class I2CDevice : II2CDevice {
private readonly Object _syncLock = new Object();
/// <summary> /// <summary>
/// Represents a device on the I2C Bus. /// Initializes a new instance of the <see cref="I2CDevice"/> class.
/// </summary> /// </summary>
public class I2CDevice : II2CDevice /// <param name="deviceId">The device identifier.</param>
{ /// <param name="fileDescriptor">The file descriptor.</param>
private readonly object _syncLock = new object(); internal I2CDevice(Int32 deviceId, Int32 fileDescriptor) {
this.DeviceId = deviceId;
/// <summary> this.FileDescriptor = fileDescriptor;
/// Initializes a new instance of the <see cref="I2CDevice"/> class.
/// </summary>
/// <param name="deviceId">The device identifier.</param>
/// <param name="fileDescriptor">The file descriptor.</param>
internal I2CDevice(int deviceId, int fileDescriptor)
{
DeviceId = deviceId;
FileDescriptor = fileDescriptor;
}
/// <inheritdoc />
public int DeviceId { get; }
/// <inheritdoc />
public int FileDescriptor { get; }
/// <inheritdoc />
public byte Read()
{
lock (_syncLock)
{
var result = WiringPi.WiringPiI2CRead(FileDescriptor);
if (result < 0) HardwareException.Throw(nameof(I2CDevice), nameof(Read));
return (byte)result;
}
}
/// <summary>
/// Reads a byte from the specified file descriptor.
/// </summary>
/// <returns>The byte from device.</returns>
public Task<byte> ReadAsync() => Task.Run(Read);
/// <summary>
/// Reads a buffer of the specified length, one byte at a time.
/// </summary>
/// <param name="length">The length.</param>
/// <returns>The byte array from device.</returns>
public byte[] Read(int length)
{
lock (_syncLock)
{
var buffer = new byte[length];
for (var i = 0; i < length; i++)
{
var result = WiringPi.WiringPiI2CRead(FileDescriptor);
if (result < 0) HardwareException.Throw(nameof(I2CDevice), nameof(Read));
buffer[i] = (byte)result;
}
return buffer;
}
}
/// <summary>
/// Reads a buffer of the specified length, one byte at a time.
/// </summary>
/// <param name="length">The length.</param>
/// <returns>The byte array from device.</returns>
public Task<byte[]> ReadAsync(int length) => Task.Run(() => Read(length));
/// <summary>
/// Writes a byte of data the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
public void Write(byte data)
{
lock (_syncLock)
{
var result = WiringPi.WiringPiI2CWrite(FileDescriptor, data);
if (result < 0) HardwareException.Throw(nameof(I2CDevice), nameof(Write));
}
}
/// <summary>
/// Writes a byte of data the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
/// <returns>The awaitable task.</returns>
public Task WriteAsync(byte data) => Task.Run(() => { Write(data); });
/// <summary>
/// Writes a set of bytes to the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
public void Write(byte[] data)
{
lock (_syncLock)
{
foreach (var b in data)
{
var result = WiringPi.WiringPiI2CWrite(FileDescriptor, b);
if (result < 0) HardwareException.Throw(nameof(I2CDevice), nameof(Write));
}
}
}
/// <summary>
/// Writes a set of bytes to the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
/// <returns>The awaitable task.</returns>
public Task WriteAsync(byte[] data) => Task.Run(() => { Write(data); });
/// <summary>
/// These write an 8 or 16-bit data value into the device register indicated.
/// </summary>
/// <param name="address">The register.</param>
/// <param name="data">The data.</param>
public void WriteAddressByte(int address, byte data)
{
lock (_syncLock)
{
var result = WiringPi.WiringPiI2CWriteReg8(FileDescriptor, address, data);
if (result < 0) HardwareException.Throw(nameof(I2CDevice), nameof(WriteAddressByte));
}
}
/// <summary>
/// These write an 8 or 16-bit data value into the device register indicated.
/// </summary>
/// <param name="address">The register.</param>
/// <param name="data">The data.</param>
public void WriteAddressWord(int address, ushort data)
{
lock (_syncLock)
{
var result = WiringPi.WiringPiI2CWriteReg16(FileDescriptor, address, data);
if (result < 0) HardwareException.Throw(nameof(I2CDevice), nameof(WriteAddressWord));
}
}
/// <summary>
/// These read an 8 or 16-bit value from the device register indicated.
/// </summary>
/// <param name="address">The register.</param>
/// <returns>The address byte from device.</returns>
public byte ReadAddressByte(int address)
{
lock (_syncLock)
{
var result = WiringPi.WiringPiI2CReadReg8(FileDescriptor, address);
if (result < 0) HardwareException.Throw(nameof(I2CDevice), nameof(ReadAddressByte));
return (byte)result;
}
}
/// <summary>
/// These read an 8 or 16-bit value from the device register indicated.
/// </summary>
/// <param name="address">The register.</param>
/// <returns>The address word from device.</returns>
public ushort ReadAddressWord(int address)
{
lock (_syncLock)
{
var result = WiringPi.WiringPiI2CReadReg16(FileDescriptor, address);
if (result < 0) HardwareException.Throw(nameof(I2CDevice), nameof(ReadAddressWord));
return Convert.ToUInt16(result);
}
}
} }
/// <inheritdoc />
public Int32 DeviceId {
get;
}
/// <inheritdoc />
public Int32 FileDescriptor {
get;
}
/// <inheritdoc />
public Byte Read() {
lock(this._syncLock) {
Int32 result = Native.WiringPi.WiringPiI2CRead(this.FileDescriptor);
if(result < 0) {
HardwareException.Throw(nameof(I2CDevice), nameof(Read));
}
return (Byte)result;
}
}
/// <summary>
/// Reads a byte from the specified file descriptor.
/// </summary>
/// <returns>The byte from device.</returns>
public Task<Byte> ReadAsync() => Task.Run(this.Read);
/// <summary>
/// Reads a buffer of the specified length, one byte at a time.
/// </summary>
/// <param name="length">The length.</param>
/// <returns>The byte array from device.</returns>
public Byte[] Read(Int32 length) {
lock(this._syncLock) {
Byte[] buffer = new Byte[length];
for(Int32 i = 0; i < length; i++) {
Int32 result = Native.WiringPi.WiringPiI2CRead(this.FileDescriptor);
if(result < 0) {
HardwareException.Throw(nameof(I2CDevice), nameof(Read));
}
buffer[i] = (Byte)result;
}
return buffer;
}
}
/// <summary>
/// Reads a buffer of the specified length, one byte at a time.
/// </summary>
/// <param name="length">The length.</param>
/// <returns>The byte array from device.</returns>
public Task<Byte[]> ReadAsync(Int32 length) => Task.Run(() => this.Read(length));
/// <summary>
/// Writes a byte of data the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
public void Write(Byte data) {
lock(this._syncLock) {
Int32 result = Native.WiringPi.WiringPiI2CWrite(this.FileDescriptor, data);
if(result < 0) {
HardwareException.Throw(nameof(I2CDevice), nameof(Write));
}
}
}
/// <summary>
/// Writes a byte of data the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
/// <returns>The awaitable task.</returns>
public Task WriteAsync(Byte data) => Task.Run(() => this.Write(data));
/// <summary>
/// Writes a set of bytes to the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
public void Write(Byte[] data) {
lock(this._syncLock) {
foreach(Byte b in data) {
Int32 result = Native.WiringPi.WiringPiI2CWrite(this.FileDescriptor, b);
if(result < 0) {
HardwareException.Throw(nameof(I2CDevice), nameof(Write));
}
}
}
}
/// <summary>
/// Writes a set of bytes to the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
/// <returns>The awaitable task.</returns>
public Task WriteAsync(Byte[] data) => Task.Run(() => this.Write(data));
/// <summary>
/// These write an 8 or 16-bit data value into the device register indicated.
/// </summary>
/// <param name="address">The register.</param>
/// <param name="data">The data.</param>
public void WriteAddressByte(Int32 address, Byte data) {
lock(this._syncLock) {
Int32 result = Native.WiringPi.WiringPiI2CWriteReg8(this.FileDescriptor, address, data);
if(result < 0) {
HardwareException.Throw(nameof(I2CDevice), nameof(WriteAddressByte));
}
}
}
/// <summary>
/// These write an 8 or 16-bit data value into the device register indicated.
/// </summary>
/// <param name="address">The register.</param>
/// <param name="data">The data.</param>
public void WriteAddressWord(Int32 address, UInt16 data) {
lock(this._syncLock) {
Int32 result = Native.WiringPi.WiringPiI2CWriteReg16(this.FileDescriptor, address, data);
if(result < 0) {
HardwareException.Throw(nameof(I2CDevice), nameof(WriteAddressWord));
}
}
}
/// <summary>
/// These read an 8 or 16-bit value from the device register indicated.
/// </summary>
/// <param name="address">The register.</param>
/// <returns>The address byte from device.</returns>
public Byte ReadAddressByte(Int32 address) {
lock(this._syncLock) {
Int32 result = Native.WiringPi.WiringPiI2CReadReg8(this.FileDescriptor, address);
if(result < 0) {
HardwareException.Throw(nameof(I2CDevice), nameof(ReadAddressByte));
}
return (Byte)result;
}
}
/// <summary>
/// These read an 8 or 16-bit value from the device register indicated.
/// </summary>
/// <param name="address">The register.</param>
/// <returns>The address word from device.</returns>
public UInt16 ReadAddressWord(Int32 address) {
lock(this._syncLock) {
Int32 result = Native.WiringPi.WiringPiI2CReadReg16(this.FileDescriptor, address);
if(result < 0) {
HardwareException.Throw(nameof(I2CDevice), nameof(ReadAddressWord));
}
return Convert.ToUInt16(result);
}
}
}
} }

View File

@ -1,12 +1,11 @@
namespace Unosquare.WiringPi.Native namespace Unosquare.WiringPi.Native {
{ /// <summary>
/// <summary> /// A delegate defining a callback for an Interrupt Service Routine.
/// A delegate defining a callback for an Interrupt Service Routine. /// </summary>
/// </summary> public delegate void InterruptServiceRoutineCallback();
public delegate void InterruptServiceRoutineCallback();
/// <summary> /// <summary>
/// Defines the body of a thread worker. /// Defines the body of a thread worker.
/// </summary> /// </summary>
public delegate void ThreadWorker(); public delegate void ThreadWorker();
} }

View File

@ -1,19 +1,17 @@
namespace Unosquare.WiringPi.Native using System;
{ using System.Runtime.InteropServices;
using System;
using System.Runtime.InteropServices;
internal static class SysCall namespace Unosquare.WiringPi.Native {
{ internal static class SysCall {
internal const string LibCLibrary = "libc"; internal const String LibCLibrary = "libc";
[DllImport(LibCLibrary, EntryPoint = "chmod", SetLastError = true)] [DllImport(LibCLibrary, EntryPoint = "chmod", SetLastError = true)]
public static extern int Chmod(string filename, uint mode); public static extern Int32 Chmod(String filename, UInt32 mode);
[DllImport(LibCLibrary, EntryPoint = "strtol", SetLastError = true)] [DllImport(LibCLibrary, EntryPoint = "strtol", SetLastError = true)]
public static extern int StringToInteger(string numberString, IntPtr endPointer, int numberBase); public static extern Int32 StringToInteger(String numberString, IntPtr endPointer, Int32 numberBase);
[DllImport(LibCLibrary, EntryPoint = "write", SetLastError = true)] [DllImport(LibCLibrary, EntryPoint = "write", SetLastError = true)]
public static extern int Write(int fd, byte[] buffer, int count); public static extern Int32 Write(Int32 fd, Byte[] buffer, Int32 count);
} }
} }

View File

@ -1,74 +1,73 @@
namespace Unosquare.WiringPi.Native using System;
{ using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
public partial class WiringPi namespace Unosquare.WiringPi.Native {
{ public partial class WiringPi {
/// <summary> /// <summary>
/// Simple device read. Some devices present data when you read them without having to do any register transactions. /// Simple device read. Some devices present data when you read them without having to do any register transactions.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CRead", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CRead", SetLastError = true)]
public static extern int WiringPiI2CRead(int fd); public static extern Int32 WiringPiI2CRead(Int32 fd);
/// <summary> /// <summary>
/// These read an 8-bit value from the device register indicated. /// These read an 8-bit value from the device register indicated.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <param name="reg">The reg.</param> /// <param name="reg">The reg.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CReadReg8", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CReadReg8", SetLastError = true)]
public static extern int WiringPiI2CReadReg8(int fd, int reg); public static extern Int32 WiringPiI2CReadReg8(Int32 fd, Int32 reg);
/// <summary> /// <summary>
/// These read a 16-bit value from the device register indicated. /// These read a 16-bit value from the device register indicated.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <param name="reg">The reg.</param> /// <param name="reg">The reg.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CReadReg16", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CReadReg16", SetLastError = true)]
public static extern int WiringPiI2CReadReg16(int fd, int reg); public static extern Int32 WiringPiI2CReadReg16(Int32 fd, Int32 reg);
/// <summary> /// <summary>
/// Simple device write. Some devices accept data this way without needing to access any internal registers. /// Simple device write. Some devices accept data this way without needing to access any internal registers.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <param name="data">The data.</param> /// <param name="data">The data.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CWrite", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CWrite", SetLastError = true)]
public static extern int WiringPiI2CWrite(int fd, int data); public static extern Int32 WiringPiI2CWrite(Int32 fd, Int32 data);
/// <summary> /// <summary>
/// These write an 8-bit data value into the device register indicated. /// These write an 8-bit data value into the device register indicated.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <param name="reg">The reg.</param> /// <param name="reg">The reg.</param>
/// <param name="data">The data.</param> /// <param name="data">The data.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CWriteReg8", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CWriteReg8", SetLastError = true)]
public static extern int WiringPiI2CWriteReg8(int fd, int reg, int data); public static extern Int32 WiringPiI2CWriteReg8(Int32 fd, Int32 reg, Int32 data);
/// <summary> /// <summary>
/// These write a 16-bit data value into the device register indicated. /// These write a 16-bit data value into the device register indicated.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <param name="reg">The reg.</param> /// <param name="reg">The reg.</param>
/// <param name="data">The data.</param> /// <param name="data">The data.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CWriteReg16", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CWriteReg16", SetLastError = true)]
public static extern int WiringPiI2CWriteReg16(int fd, int reg, int data); public static extern Int32 WiringPiI2CWriteReg16(Int32 fd, Int32 reg, Int32 data);
/// <summary> /// <summary>
/// This initializes the I2C system with your given device identifier. /// This initializes the I2C system with your given device identifier.
/// The ID is the I2C number of the device and you can use the i2cdetect program to find this out. wiringPiI2CSetup() /// The ID is the I2C number of the device and you can use the i2cdetect program to find this out. wiringPiI2CSetup()
/// will work out which revision Raspberry Pi you have and open the appropriate device in /dev. /// will work out which revision Raspberry Pi you have and open the appropriate device in /dev.
/// The return value is the standard Linux filehandle, or -1 if any error in which case, you can consult errno as usual. /// The return value is the standard Linux filehandle, or -1 if any error in which case, you can consult errno as usual.
/// E.g. the popular MCP23017 GPIO expander is usually device Id 0x20, so this is the number you would pass into wiringPiI2CSetup(). /// E.g. the popular MCP23017 GPIO expander is usually device Id 0x20, so this is the number you would pass into wiringPiI2CSetup().
/// </summary> /// </summary>
/// <param name="devId">The dev identifier.</param> /// <param name="devId">The dev identifier.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CSetup", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiI2CSetup", SetLastError = true)]
public static extern int WiringPiI2CSetup(int devId); public static extern Int32 WiringPiI2CSetup(Int32 devId);
} }
} }

View File

@ -1,69 +1,68 @@
namespace Unosquare.WiringPi.Native using System;
{ using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
public partial class WiringPi namespace Unosquare.WiringPi.Native {
{ public partial class WiringPi {
/// <summary> /// <summary>
/// This opens and initialises the serial device and sets the baud rate. It sets the port into “raw” mode (character at a time and no translations), /// This opens and initialises the serial device and sets the baud rate. It sets the port into “raw” mode (character at a time and no translations),
/// and sets the read timeout to 10 seconds. The return value is the file descriptor or -1 for any error, in which case errno will be set as appropriate. /// and sets the read timeout to 10 seconds. The return value is the file descriptor or -1 for any error, in which case errno will be set as appropriate.
/// The wiringSerial library is intended to provide simplified control suitable for most applications, however if you need advanced control /// The wiringSerial library is intended to provide simplified control suitable for most applications, however if you need advanced control
/// e.g. parity control, modem control lines (via a USB adapter, there are none on the Pis on-board UART!) and so on, /// e.g. parity control, modem control lines (via a USB adapter, there are none on the Pis on-board UART!) and so on,
/// then you need to do some of this the old fashioned way. /// then you need to do some of this the old fashioned way.
/// </summary> /// </summary>
/// <param name="device">The device.</param> /// <param name="device">The device.</param>
/// <param name="baud">The baud.</param> /// <param name="baud">The baud.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "serialOpen", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "serialOpen", SetLastError = true)]
public static extern int SerialOpen(string device, int baud); public static extern Int32 SerialOpen(String device, Int32 baud);
/// <summary> /// <summary>
/// Closes the device identified by the file descriptor given. /// Closes the device identified by the file descriptor given.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "serialClose", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "serialClose", SetLastError = true)]
public static extern int SerialClose(int fd); public static extern Int32 SerialClose(Int32 fd);
/// <summary> /// <summary>
/// Sends the single byte to the serial device identified by the given file descriptor. /// Sends the single byte to the serial device identified by the given file descriptor.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <param name="c">The c.</param> /// <param name="c">The c.</param>
[DllImport(WiringPiLibrary, EntryPoint = "serialPutchar", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "serialPutchar", SetLastError = true)]
public static extern void SerialPutchar(int fd, byte c); public static extern void SerialPutchar(Int32 fd, Byte c);
/// <summary> /// <summary>
/// Sends the nul-terminated string to the serial device identified by the given file descriptor. /// Sends the nul-terminated string to the serial device identified by the given file descriptor.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <param name="s">The s.</param> /// <param name="s">The s.</param>
[DllImport(WiringPiLibrary, EntryPoint = "serialPuts", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "serialPuts", SetLastError = true)]
public static extern void SerialPuts(int fd, string s); public static extern void SerialPuts(Int32 fd, String s);
/// <summary> /// <summary>
/// Returns the number of characters available for reading, or -1 for any error condition, /// Returns the number of characters available for reading, or -1 for any error condition,
/// in which case errno will be set appropriately. /// in which case errno will be set appropriately.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "serialDataAvail", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "serialDataAvail", SetLastError = true)]
public static extern int SerialDataAvail(int fd); public static extern Int32 SerialDataAvail(Int32 fd);
/// <summary> /// <summary>
/// Returns the next character available on the serial device. /// Returns the next character available on the serial device.
/// This call will block for up to 10 seconds if no data is available (when it will return -1). /// This call will block for up to 10 seconds if no data is available (when it will return -1).
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "serialGetchar", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "serialGetchar", SetLastError = true)]
public static extern int SerialGetchar(int fd); public static extern Int32 SerialGetchar(Int32 fd);
/// <summary> /// <summary>
/// This discards all data received, or waiting to be send down the given device. /// This discards all data received, or waiting to be send down the given device.
/// </summary> /// </summary>
/// <param name="fd">The fd.</param> /// <param name="fd">The fd.</param>
[DllImport(WiringPiLibrary, EntryPoint = "serialFlush", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "serialFlush", SetLastError = true)]
public static extern void SerialFlush(int fd); public static extern void SerialFlush(Int32 fd);
} }
} }

View File

@ -1,36 +1,35 @@
namespace Unosquare.WiringPi.Native using System;
{ using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
public partial class WiringPi namespace Unosquare.WiringPi.Native {
{ public partial class WiringPi {
#region WiringPi - Shift Library #region WiringPi - Shift Library
/// <summary> /// <summary>
/// This shifts an 8-bit data value in with the data appearing on the dPin and the clock being sent out on the cPin. /// This shifts an 8-bit data value in with the data appearing on the dPin and the clock being sent out on the cPin.
/// Order is either LSBFIRST or MSBFIRST. The data is sampled after the cPin goes high. /// Order is either LSBFIRST or MSBFIRST. The data is sampled after the cPin goes high.
/// (So cPin high, sample data, cPin low, repeat for 8 bits) The 8-bit value is returned by the function. /// (So cPin high, sample data, cPin low, repeat for 8 bits) The 8-bit value is returned by the function.
/// </summary> /// </summary>
/// <param name="dPin">The d pin.</param> /// <param name="dPin">The d pin.</param>
/// <param name="cPin">The c pin.</param> /// <param name="cPin">The c pin.</param>
/// <param name="order">The order.</param> /// <param name="order">The order.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "shiftIn", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "shiftIn", SetLastError = true)]
public static extern byte ShiftIn(byte dPin, byte cPin, byte order); public static extern Byte ShiftIn(Byte dPin, Byte cPin, Byte order);
/// <summary> /// <summary>
/// The shifts an 8-bit data value val out with the data being sent out on dPin and the clock being sent out on the cPin. /// The shifts an 8-bit data value val out with the data being sent out on dPin and the clock being sent out on the cPin.
/// order is as above. Data is clocked out on the rising or falling edge ie. dPin is set, then cPin is taken high then low /// order is as above. Data is clocked out on the rising or falling edge ie. dPin is set, then cPin is taken high then low
/// repeated for the 8 bits. /// repeated for the 8 bits.
/// </summary> /// </summary>
/// <param name="dPin">The d pin.</param> /// <param name="dPin">The d pin.</param>
/// <param name="cPin">The c pin.</param> /// <param name="cPin">The c pin.</param>
/// <param name="order">The order.</param> /// <param name="order">The order.</param>
/// <param name="val">The value.</param> /// <param name="val">The value.</param>
[DllImport(WiringPiLibrary, EntryPoint = "shiftOut", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "shiftOut", SetLastError = true)]
public static extern void ShiftOut(byte dPin, byte cPin, byte order, byte val); public static extern void ShiftOut(Byte dPin, Byte cPin, Byte order, Byte val);
#endregion #endregion
} }
} }

View File

@ -1,64 +1,63 @@
namespace Unosquare.WiringPi.Native using System;
{ using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
public partial class WiringPi namespace Unosquare.WiringPi.Native {
{ public partial class WiringPi {
#region WiringPi - Soft PWM (https://github.com/WiringPi/WiringPi/blob/master/wiringPi/softPwm.h) #region WiringPi - Soft PWM (https://github.com/WiringPi/WiringPi/blob/master/wiringPi/softPwm.h)
/// <summary> /// <summary>
/// This creates a software controlled PWM pin. You can use any GPIO pin and the pin numbering will be that of the wiringPiSetup() /// This creates a software controlled PWM pin. You can use any GPIO pin and the pin numbering will be that of the wiringPiSetup()
/// function you used. Use 100 for the pwmRange, then the value can be anything from 0 (off) to 100 (fully on) for the given pin. /// function you used. Use 100 for the pwmRange, then the value can be anything from 0 (off) to 100 (fully on) for the given pin.
/// The return value is 0 for success. Anything else and you should check the global errno variable to see what went wrong. /// The return value is 0 for success. Anything else and you should check the global errno variable to see what went wrong.
/// </summary> /// </summary>
/// <param name="pin">The pin.</param> /// <param name="pin">The pin.</param>
/// <param name="initialValue">The initial value.</param> /// <param name="initialValue">The initial value.</param>
/// <param name="pwmRange">The PWM range.</param> /// <param name="pwmRange">The PWM range.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "softPwmCreate", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "softPwmCreate", SetLastError = true)]
public static extern int SoftPwmCreate(int pin, int initialValue, int pwmRange); public static extern Int32 SoftPwmCreate(Int32 pin, Int32 initialValue, Int32 pwmRange);
/// <summary> /// <summary>
/// This updates the PWM value on the given pin. The value is checked to be in-range and pins that havent previously /// This updates the PWM value on the given pin. The value is checked to be in-range and pins that havent previously
/// been initialized via softPwmCreate will be silently ignored. /// been initialized via softPwmCreate will be silently ignored.
/// </summary> /// </summary>
/// <param name="pin">The pin.</param> /// <param name="pin">The pin.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
[DllImport(WiringPiLibrary, EntryPoint = "softPwmWrite", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "softPwmWrite", SetLastError = true)]
public static extern void SoftPwmWrite(int pin, int value); public static extern void SoftPwmWrite(Int32 pin, Int32 value);
/// <summary> /// <summary>
/// This function is undocumented. /// This function is undocumented.
/// </summary> /// </summary>
/// <param name="pin">The pin.</param> /// <param name="pin">The pin.</param>
[DllImport(WiringPiLibrary, EntryPoint = "softPwmStop", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "softPwmStop", SetLastError = true)]
public static extern void SoftPwmStop(int pin); public static extern void SoftPwmStop(Int32 pin);
/// <summary> /// <summary>
/// This creates a software controlled tone pin. You can use any GPIO pin and the pin numbering will be that of the wiringPiSetup() function you used. /// This creates a software controlled tone pin. You can use any GPIO pin and the pin numbering will be that of the wiringPiSetup() function you used.
/// The return value is 0 for success. Anything else and you should check the global errno variable to see what went wrong. /// The return value is 0 for success. Anything else and you should check the global errno variable to see what went wrong.
/// </summary> /// </summary>
/// <param name="pin">The pin.</param> /// <param name="pin">The pin.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "softToneCreate", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "softToneCreate", SetLastError = true)]
public static extern int SoftToneCreate(int pin); public static extern Int32 SoftToneCreate(Int32 pin);
/// <summary> /// <summary>
/// This function is undocumented. /// This function is undocumented.
/// </summary> /// </summary>
/// <param name="pin">The pin.</param> /// <param name="pin">The pin.</param>
[DllImport(WiringPiLibrary, EntryPoint = "softToneStop", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "softToneStop", SetLastError = true)]
public static extern void SoftToneStop(int pin); public static extern void SoftToneStop(Int32 pin);
/// <summary> /// <summary>
/// This updates the tone frequency value on the given pin. The tone will be played until you set the frequency to 0. /// This updates the tone frequency value on the given pin. The tone will be played until you set the frequency to 0.
/// </summary> /// </summary>
/// <param name="pin">The pin.</param> /// <param name="pin">The pin.</param>
/// <param name="freq">The freq.</param> /// <param name="freq">The freq.</param>
[DllImport(WiringPiLibrary, EntryPoint = "softToneWrite", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "softToneWrite", SetLastError = true)]
public static extern void SoftToneWrite(int pin, int freq); public static extern void SoftToneWrite(Int32 pin, Int32 freq);
#endregion #endregion
} }
} }

View File

@ -1,53 +1,52 @@
namespace Unosquare.WiringPi.Native using System;
{ using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
public partial class WiringPi namespace Unosquare.WiringPi.Native {
{ public partial class WiringPi {
#region WiringPi - SPI Library Calls #region WiringPi - SPI Library Calls
/// <summary> /// <summary>
/// This function is undocumented. /// This function is undocumented.
/// </summary> /// </summary>
/// <param name="channel">The channel.</param> /// <param name="channel">The channel.</param>
/// <returns>Unknown.</returns> /// <returns>Unknown.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSPIGetFd", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiSPIGetFd", SetLastError = true)]
public static extern int WiringPiSPIGetFd(int channel); public static extern Int32 WiringPiSPIGetFd(Int32 channel);
/// <summary> /// <summary>
/// This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus. /// This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus.
/// Thats all there is in the helper library. It is possible to do simple read and writes over the SPI bus using the standard read() and write() system calls though /// Thats all there is in the helper library. It is possible to do simple read and writes over the SPI bus using the standard read() and write() system calls though
/// write() may be better to use for sending data to chains of shift registers, or those LED strings where you send RGB triplets of data. /// write() may be better to use for sending data to chains of shift registers, or those LED strings where you send RGB triplets of data.
/// Devices such as A/D and D/A converters usually need to perform a concurrent write/read transaction to work. /// Devices such as A/D and D/A converters usually need to perform a concurrent write/read transaction to work.
/// </summary> /// </summary>
/// <param name="channel">The channel.</param> /// <param name="channel">The channel.</param>
/// <param name="data">The data.</param> /// <param name="data">The data.</param>
/// <param name="len">The length.</param> /// <param name="len">The length.</param>
/// <returns>The result.</returns> /// <returns>The result.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSPIDataRW", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiSPIDataRW", SetLastError = true)]
public static extern int WiringPiSPIDataRW(int channel, byte[] data, int len); public static extern Int32 WiringPiSPIDataRW(Int32 channel, Byte[] data, Int32 len);
/// <summary> /// <summary>
/// This function is undocumented. /// This function is undocumented.
/// </summary> /// </summary>
/// <param name="channel">The channel.</param> /// <param name="channel">The channel.</param>
/// <param name="speed">The speed.</param> /// <param name="speed">The speed.</param>
/// <param name="mode">The mode.</param> /// <param name="mode">The mode.</param>
/// <returns>Unkown.</returns> /// <returns>Unkown.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSPISetupMode", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiSPISetupMode", SetLastError = true)]
public static extern int WiringPiSPISetupMode(int channel, int speed, int mode); public static extern Int32 WiringPiSPISetupMode(Int32 channel, Int32 speed, Int32 mode);
/// <summary> /// <summary>
/// This is the way to initialize a channel (The Pi has 2 channels; 0 and 1). The speed parameter is an integer /// This is the way to initialize a channel (The Pi has 2 channels; 0 and 1). The speed parameter is an integer
/// in the range 500,000 through 32,000,000 and represents the SPI clock speed in Hz. /// in the range 500,000 through 32,000,000 and represents the SPI clock speed in Hz.
/// The returned value is the Linux file-descriptor for the device, or -1 on error. If an error has happened, you may use the standard errno global variable to see why. /// The returned value is the Linux file-descriptor for the device, or -1 on error. If an error has happened, you may use the standard errno global variable to see why.
/// </summary> /// </summary>
/// <param name="channel">The channel.</param> /// <param name="channel">The channel.</param>
/// <param name="speed">The speed.</param> /// <param name="speed">The speed.</param>
/// <returns>The Linux file descriptor for the device or -1 for error.</returns> /// <returns>The Linux file descriptor for the device or -1 for error.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSPISetup", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiSPISetup", SetLastError = true)]
public static extern int WiringPiSPISetup(int channel, int speed); public static extern Int32 WiringPiSPISetup(Int32 channel, Int32 speed);
#endregion #endregion
} }
} }

View File

@ -1,376 +1,375 @@
namespace Unosquare.WiringPi.Native using System.Runtime.InteropServices;
{ using System;
using System.Runtime.InteropServices;
namespace Unosquare.WiringPi.Native {
/// <summary>
/// Provides native C WiringPi Library function call wrappers
/// All credit for the native library goes to the author of http://wiringpi.com/
/// The wrappers were written based on https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPi.h.
/// </summary>
public partial class WiringPi {
internal const String WiringPiLibrary = "libwiringPi.so.2.50";
#region WiringPi - Core Functions (https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPi.h)
/// <summary> /// <summary>
/// Provides native C WiringPi Library function call wrappers /// This initialises wiringPi and assumes that the calling program is going to be using the wiringPi pin numbering scheme.
/// All credit for the native library goes to the author of http://wiringpi.com/ /// This is a simplified numbering scheme which provides a mapping from virtual pin numbers 0 through 16 to the real underlying Broadcom GPIO pin numbers.
/// The wrappers were written based on https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPi.h. /// See the pins page for a table which maps the wiringPi pin number to the Broadcom GPIO pin number to the physical location on the edge connector.
/// This function needs to be called with root privileges.
/// </summary> /// </summary>
public partial class WiringPi /// <returns>The result code.</returns>
{ [DllImport(WiringPiLibrary, EntryPoint = "wiringPiSetup", SetLastError = true)]
internal const string WiringPiLibrary = "libwiringPi.so.2.50"; public static extern Int32 WiringPiSetup();
#region WiringPi - Core Functions (https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPi.h) /// <summary>
/// This initialises wiringPi but uses the /sys/class/gpio interface rather than accessing the hardware directly.
/// This can be called as a non-root user provided the GPIO pins have been exported before-hand using the gpio program.
/// Pin numbering in this mode is the native Broadcom GPIO numbers the same as wiringPiSetupGpio() above,
/// so be aware of the differences between Rev 1 and Rev 2 boards.
///
/// Note: In this mode you can only use the pins which have been exported via the /sys/class/gpio interface before you run your program.
/// You can do this in a separate shell-script, or by using the system() function from inside your program to call the gpio program.
/// Also note that some functions have no effect when using this mode as theyre not currently possible to action unless called with root privileges.
/// (although you can use system() to call gpio to set/change modes if needed).
/// </summary>
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSetupSys", SetLastError = true)]
public static extern Int32 WiringPiSetupSys();
/// <summary> /// <summary>
/// This initialises wiringPi and assumes that the calling program is going to be using the wiringPi pin numbering scheme. /// This is identical to wiringPiSetup, however it allows the calling programs to use the Broadcom GPIO
/// This is a simplified numbering scheme which provides a mapping from virtual pin numbers 0 through 16 to the real underlying Broadcom GPIO pin numbers. /// pin numbers directly with no re-mapping.
/// See the pins page for a table which maps the wiringPi pin number to the Broadcom GPIO pin number to the physical location on the edge connector. /// As above, this function needs to be called with root privileges, and note that some pins are different
/// This function needs to be called with root privileges. /// from revision 1 to revision 2 boards.
/// </summary> /// </summary>
/// <returns>The result code.</returns> /// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSetup", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wiringPiSetupGpio", SetLastError = true)]
public static extern int WiringPiSetup(); public static extern Int32 WiringPiSetupGpio();
/// <summary> /// <summary>
/// This initialises wiringPi but uses the /sys/class/gpio interface rather than accessing the hardware directly. /// Identical to wiringPiSetup, however it allows the calling programs to use the physical pin numbers on the P1 connector only.
/// This can be called as a non-root user provided the GPIO pins have been exported before-hand using the gpio program. /// This function needs to be called with root privileges.
/// Pin numbering in this mode is the native Broadcom GPIO numbers the same as wiringPiSetupGpio() above, /// </summary>
/// so be aware of the differences between Rev 1 and Rev 2 boards. /// <returns>The result code.</returns>
/// [DllImport(WiringPiLibrary, EntryPoint = "wiringPiSetupPhys", SetLastError = true)]
/// Note: In this mode you can only use the pins which have been exported via the /sys/class/gpio interface before you run your program. public static extern Int32 WiringPiSetupPhys();
/// You can do this in a separate shell-script, or by using the system() function from inside your program to call the gpio program.
/// Also note that some functions have no effect when using this mode as theyre not currently possible to action unless called with root privileges.
/// (although you can use system() to call gpio to set/change modes if needed).
/// </summary>
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSetupSys", SetLastError = true)]
public static extern int WiringPiSetupSys();
/// <summary> /// <summary>
/// This is identical to wiringPiSetup, however it allows the calling programs to use the Broadcom GPIO /// This function is undocumented.
/// pin numbers directly with no re-mapping. /// </summary>
/// As above, this function needs to be called with root privileges, and note that some pins are different /// <param name="pin">The pin.</param>
/// from revision 1 to revision 2 boards. /// <param name="mode">The mode.</param>
/// </summary> [DllImport(WiringPiLibrary, EntryPoint = "pinModeAlt", SetLastError = true)]
/// <returns>The result code.</returns> public static extern void PinModeAlt(Int32 pin, Int32 mode);
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSetupGpio", SetLastError = true)]
public static extern int WiringPiSetupGpio();
/// <summary> /// <summary>
/// Identical to wiringPiSetup, however it allows the calling programs to use the physical pin numbers on the P1 connector only. /// This sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK.
/// This function needs to be called with root privileges. /// Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only wiringPi pin 7 (BCM_GPIO 4)
/// </summary> /// supports CLOCK output modes.
/// <returns>The result code.</returns> ///
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiSetupPhys", SetLastError = true)] /// This function has no effect when in Sys mode. If you need to change the pin mode, then you can
public static extern int WiringPiSetupPhys(); /// do it with the gpio program in a script before you start your program.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="mode">The mode.</param>
[DllImport(WiringPiLibrary, EntryPoint = "pinMode", SetLastError = true)]
public static extern void PinMode(Int32 pin, Int32 mode);
/// <summary> /// <summary>
/// This function is undocumented. /// This sets the pull-up or pull-down resistor mode on the given pin, which should be set as an input.
/// </summary> /// Unlike the Arduino, the BCM2835 has both pull-up and down internal resistors. The parameter pud should be; PUD_OFF,
/// <param name="pin">The pin.</param> /// (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v) The internal pull up/down resistors
/// <param name="mode">The mode.</param> /// have a value of approximately 50KΩ on the Raspberry Pi.
[DllImport(WiringPiLibrary, EntryPoint = "pinModeAlt", SetLastError = true)] ///
public static extern void PinModeAlt(int pin, int mode); /// This function has no effect on the Raspberry Pis GPIO pins when in Sys mode.
/// If you need to activate a pull-up/pull-down, then you can do it with the gpio program in a script before you start your program.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="pud">The pud.</param>
[DllImport(WiringPiLibrary, EntryPoint = "pullUpDnControl", SetLastError = true)]
public static extern void PullUpDnControl(Int32 pin, Int32 pud);
/// <summary> /// <summary>
/// This sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK. /// This function returns the value read at the given pin. It will be HIGH or LOW (1 or 0) depending on the logic level at the pin.
/// Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only wiringPi pin 7 (BCM_GPIO 4) /// </summary>
/// supports CLOCK output modes. /// <param name="pin">The pin.</param>
/// /// <returns>The result code.</returns>
/// This function has no effect when in Sys mode. If you need to change the pin mode, then you can [DllImport(WiringPiLibrary, EntryPoint = "digitalRead", SetLastError = true)]
/// do it with the gpio program in a script before you start your program. public static extern Int32 DigitalRead(Int32 pin);
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="mode">The mode.</param>
[DllImport(WiringPiLibrary, EntryPoint = "pinMode", SetLastError = true)]
public static extern void PinMode(int pin, int mode);
/// <summary> /// <summary>
/// This sets the pull-up or pull-down resistor mode on the given pin, which should be set as an input. /// Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as an output.
/// Unlike the Arduino, the BCM2835 has both pull-up and down internal resistors. The parameter pud should be; PUD_OFF, /// WiringPi treats any non-zero number as HIGH, however 0 is the only representation of LOW.
/// (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v) The internal pull up/down resistors /// </summary>
/// have a value of approximately 50KΩ on the Raspberry Pi. /// <param name="pin">The pin.</param>
/// /// <param name="value">The value.</param>
/// This function has no effect on the Raspberry Pis GPIO pins when in Sys mode. [DllImport(WiringPiLibrary, EntryPoint = "digitalWrite", SetLastError = true)]
/// If you need to activate a pull-up/pull-down, then you can do it with the gpio program in a script before you start your program. public static extern void DigitalWrite(Int32 pin, Int32 value);
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="pud">The pud.</param>
[DllImport(WiringPiLibrary, EntryPoint = "pullUpDnControl", SetLastError = true)]
public static extern void PullUpDnControl(int pin, int pud);
/// <summary> /// <summary>
/// This function returns the value read at the given pin. It will be HIGH or LOW (1 or 0) depending on the logic level at the pin. /// Writes the value to the PWM register for the given pin. The Raspberry Pi has one
/// </summary> /// on-board PWM pin, pin 1 (BMC_GPIO 18, Phys 12) and the range is 0-1024.
/// <param name="pin">The pin.</param> /// Other PWM devices may have other PWM ranges.
/// <returns>The result code.</returns> /// This function is not able to control the Pis on-board PWM when in Sys mode.
[DllImport(WiringPiLibrary, EntryPoint = "digitalRead", SetLastError = true)] /// </summary>
public static extern int DigitalRead(int pin); /// <param name="pin">The pin.</param>
/// <param name="value">The value.</param>
[DllImport(WiringPiLibrary, EntryPoint = "pwmWrite", SetLastError = true)]
public static extern void PwmWrite(Int32 pin, Int32 value);
/// <summary> /// <summary>
/// Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as an output. /// This returns the value read on the supplied analog input pin. You will need to
/// WiringPi treats any non-zero number as HIGH, however 0 is the only representation of LOW. /// register additional analog modules to enable this function for devices such as the Gertboard, quick2Wire analog board, etc.
/// </summary> /// </summary>
/// <param name="pin">The pin.</param> /// <param name="pin">The pin.</param>
/// <param name="value">The value.</param> /// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "digitalWrite", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "analogRead", SetLastError = true)]
public static extern void DigitalWrite(int pin, int value); public static extern Int32 AnalogRead(Int32 pin);
/// <summary> /// <summary>
/// Writes the value to the PWM register for the given pin. The Raspberry Pi has one /// This writes the given value to the supplied analog pin. You will need to register additional
/// on-board PWM pin, pin 1 (BMC_GPIO 18, Phys 12) and the range is 0-1024. /// analog modules to enable this function for devices such as the Gertboard.
/// Other PWM devices may have other PWM ranges. /// </summary>
/// This function is not able to control the Pis on-board PWM when in Sys mode. /// <param name="pin">The pin.</param>
/// </summary> /// <param name="value">The value.</param>
/// <param name="pin">The pin.</param> [DllImport(WiringPiLibrary, EntryPoint = "analogWrite", SetLastError = true)]
/// <param name="value">The value.</param> public static extern void AnalogWrite(Int32 pin, Int32 value);
[DllImport(WiringPiLibrary, EntryPoint = "pwmWrite", SetLastError = true)]
public static extern void PwmWrite(int pin, int value);
/// <summary> /// <summary>
/// This returns the value read on the supplied analog input pin. You will need to /// This returns the board revision of the Raspberry Pi. It will be either 1 or 2. Some of the BCM_GPIO pins changed number and
/// register additional analog modules to enable this function for devices such as the Gertboard, quick2Wire analog board, etc. /// function when moving from board revision 1 to 2, so if you are using BCM_GPIO pin numbers, then you need to be aware of the differences.
/// </summary> /// </summary>
/// <param name="pin">The pin.</param> /// <returns>The result code.</returns>
/// <returns>The result code.</returns> [DllImport(WiringPiLibrary, EntryPoint = "piBoardRev", SetLastError = true)]
[DllImport(WiringPiLibrary, EntryPoint = "analogRead", SetLastError = true)] public static extern Int32 PiBoardRev();
public static extern int AnalogRead(int pin);
/// <summary> /// <summary>
/// This writes the given value to the supplied analog pin. You will need to register additional /// This function is undocumented.
/// analog modules to enable this function for devices such as the Gertboard. /// </summary>
/// </summary> /// <param name="model">The model.</param>
/// <param name="pin">The pin.</param> /// <param name="mem">The memory.</param>
/// <param name="value">The value.</param> /// <param name="maker">The maker.</param>
[DllImport(WiringPiLibrary, EntryPoint = "analogWrite", SetLastError = true)] /// <param name="overVolted">The over volted.</param>
public static extern void AnalogWrite(int pin, int value); /// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "piBoardId", SetLastError = true)]
public static extern Int32 PiBoardId(ref Int32 model, ref Int32 mem, ref Int32 maker, ref Int32 overVolted);
/// <summary> /// <summary>
/// This returns the board revision of the Raspberry Pi. It will be either 1 or 2. Some of the BCM_GPIO pins changed number and /// This returns the BCM_GPIO pin number of the supplied wiringPi pin. It takes the board revision into account.
/// function when moving from board revision 1 to 2, so if you are using BCM_GPIO pin numbers, then you need to be aware of the differences. /// </summary>
/// </summary> /// <param name="wPiPin">The w pi pin.</param>
/// <returns>The result code.</returns> /// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "piBoardRev", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "wpiPinToGpio", SetLastError = true)]
public static extern int PiBoardRev(); public static extern Int32 WpiPinToGpio(Int32 wPiPin);
/// <summary> /// <summary>
/// This function is undocumented. /// This returns the BCM_GPIO pin number of the supplied physical pin on the P1 connector.
/// </summary> /// </summary>
/// <param name="model">The model.</param> /// <param name="physPin">The physical pin.</param>
/// <param name="mem">The memory.</param> /// <returns>The result code.</returns>
/// <param name="maker">The maker.</param> [DllImport(WiringPiLibrary, EntryPoint = "physPinToGpio", SetLastError = true)]
/// <param name="overVolted">The over volted.</param> public static extern Int32 PhysPinToGpio(Int32 physPin);
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "piBoardId", SetLastError = true)]
public static extern int PiBoardId(ref int model, ref int mem, ref int maker, ref int overVolted);
/// <summary> /// <summary>
/// This returns the BCM_GPIO pin number of the supplied wiringPi pin. It takes the board revision into account. /// This sets the “strength” of the pad drivers for a particular group of pins.
/// </summary> /// There are 3 groups of pins and the drive strength is from 0 to 7. Do not use this unless you know what you are doing.
/// <param name="wPiPin">The w pi pin.</param> /// </summary>
/// <returns>The result code.</returns> /// <param name="group">The group.</param>
[DllImport(WiringPiLibrary, EntryPoint = "wpiPinToGpio", SetLastError = true)] /// <param name="value">The value.</param>
public static extern int WpiPinToGpio(int wPiPin); /// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "setPadDrive", SetLastError = true)]
public static extern Int32 SetPadDrive(Int32 group, Int32 value);
/// <summary> /// <summary>
/// This returns the BCM_GPIO pin number of the supplied physical pin on the P1 connector. /// Undocumented function.
/// </summary> /// </summary>
/// <param name="physPin">The physical pin.</param> /// <param name="pin">The pin.</param>
/// <returns>The result code.</returns> /// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "physPinToGpio", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "getAlt", SetLastError = true)]
public static extern int PhysPinToGpio(int physPin); public static extern Int32 GetAlt(Int32 pin);
/// <summary> /// <summary>
/// This sets the “strength” of the pad drivers for a particular group of pins. /// Undocumented function.
/// There are 3 groups of pins and the drive strength is from 0 to 7. Do not use this unless you know what you are doing. /// </summary>
/// </summary> /// <param name="pin">The pin.</param>
/// <param name="group">The group.</param> /// <param name="freq">The freq.</param>
/// <param name="value">The value.</param> /// <returns>The result code.</returns>
/// <returns>The result code.</returns> [DllImport(WiringPiLibrary, EntryPoint = "pwmToneWrite", SetLastError = true)]
[DllImport(WiringPiLibrary, EntryPoint = "setPadDrive", SetLastError = true)] public static extern Int32 PwmToneWrite(Int32 pin, Int32 freq);
public static extern int SetPadDrive(int group, int value);
/// <summary> /// <summary>
/// Undocumented function. /// This writes the 8-bit byte supplied to the first 8 GPIO pins.
/// </summary> /// Its the fastest way to set all 8 bits at once to a particular value, although it still takes two write operations to the Pis GPIO hardware.
/// <param name="pin">The pin.</param> /// </summary>
/// <returns>The result code.</returns> /// <param name="value">The value.</param>
[DllImport(WiringPiLibrary, EntryPoint = "getAlt", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "digitalWriteByte", SetLastError = true)]
public static extern int GetAlt(int pin); public static extern void DigitalWriteByte(Int32 value);
/// <summary> /// <summary>
/// Undocumented function. /// This writes the 8-bit byte supplied to the first 8 GPIO pins.
/// </summary> /// Its the fastest way to set all 8 bits at once to a particular value, although it still takes two write operations to the Pis GPIO hardware.
/// <param name="pin">The pin.</param> /// </summary>
/// <param name="freq">The freq.</param> /// <param name="value">The value.</param>
/// <returns>The result code.</returns> [DllImport(WiringPiLibrary, EntryPoint = "digitalWriteByte2", SetLastError = true)]
[DllImport(WiringPiLibrary, EntryPoint = "pwmToneWrite", SetLastError = true)] public static extern void DigitalWriteByte2(Int32 value);
public static extern int PwmToneWrite(int pin, int freq);
/// <summary> /// <summary>
/// This writes the 8-bit byte supplied to the first 8 GPIO pins. /// Undocumented function
/// Its the fastest way to set all 8 bits at once to a particular value, although it still takes two write operations to the Pis GPIO hardware. /// This reads the 8-bit byte supplied to the first 8 GPIO pins.
/// </summary> /// Its the fastest way to get all 8 bits at once to a particular value.
/// <param name="value">The value.</param> /// </summary>
[DllImport(WiringPiLibrary, EntryPoint = "digitalWriteByte", SetLastError = true)] /// <returns>The result code.</returns>
public static extern void DigitalWriteByte(int value); [DllImport(WiringPiLibrary, EntryPoint = "digitalReadByte", SetLastError = true)]
public static extern UInt32 DigitalReadByte();
/// <summary> /// <summary>
/// This writes the 8-bit byte supplied to the first 8 GPIO pins. /// Undocumented function
/// Its the fastest way to set all 8 bits at once to a particular value, although it still takes two write operations to the Pis GPIO hardware. /// This reads the 8-bit byte supplied to the first 8 GPIO pins.
/// </summary> /// Its the fastest way to get all 8 bits at once to a particular value.
/// <param name="value">The value.</param> /// </summary>
[DllImport(WiringPiLibrary, EntryPoint = "digitalWriteByte2", SetLastError = true)] /// <returns>The result code.</returns>
public static extern void DigitalWriteByte2(int value); [DllImport(WiringPiLibrary, EntryPoint = "digitalReadByte2", SetLastError = true)]
public static extern UInt32 DigitalReadByte2();
/// <summary> /// <summary>
/// Undocumented function /// The PWM generator can run in 2 modes “balanced” and “mark:space”. The mark:space mode is traditional,
/// This reads the 8-bit byte supplied to the first 8 GPIO pins. /// however the default mode in the Pi is “balanced”. You can switch modes by supplying the parameter: PWM_MODE_BAL or PWM_MODE_MS.
/// Its the fastest way to get all 8 bits at once to a particular value. /// </summary>
/// </summary> /// <param name="mode">The mode.</param>
/// <returns>The result code.</returns> [DllImport(WiringPiLibrary, EntryPoint = "pwmSetMode", SetLastError = true)]
[DllImport(WiringPiLibrary, EntryPoint = "digitalReadByte", SetLastError = true)] public static extern void PwmSetMode(Int32 mode);
public static extern uint DigitalReadByte();
/// <summary> /// <summary>
/// Undocumented function /// This sets the range register in the PWM generator. The default is 1024.
/// This reads the 8-bit byte supplied to the first 8 GPIO pins. /// </summary>
/// Its the fastest way to get all 8 bits at once to a particular value. /// <param name="range">The range.</param>
/// </summary> [DllImport(WiringPiLibrary, EntryPoint = "pwmSetRange", SetLastError = true)]
/// <returns>The result code.</returns> public static extern void PwmSetRange(UInt32 range);
[DllImport(WiringPiLibrary, EntryPoint = "digitalReadByte2", SetLastError = true)]
public static extern uint DigitalReadByte2();
/// <summary> /// <summary>
/// The PWM generator can run in 2 modes “balanced” and “mark:space”. The mark:space mode is traditional, /// This sets the divisor for the PWM clock.
/// however the default mode in the Pi is “balanced”. You can switch modes by supplying the parameter: PWM_MODE_BAL or PWM_MODE_MS. /// Note: The PWM control functions can not be used when in Sys mode.
/// </summary> /// To understand more about the PWM system, youll need to read the Broadcom ARM peripherals manual.
/// <param name="mode">The mode.</param> /// </summary>
[DllImport(WiringPiLibrary, EntryPoint = "pwmSetMode", SetLastError = true)] /// <param name="divisor">The divisor.</param>
public static extern void PwmSetMode(int mode); [DllImport(WiringPiLibrary, EntryPoint = "pwmSetClock", SetLastError = true)]
public static extern void PwmSetClock(Int32 divisor);
/// <summary> /// <summary>
/// This sets the range register in the PWM generator. The default is 1024. /// Undocumented function.
/// </summary> /// </summary>
/// <param name="range">The range.</param> /// <param name="pin">The pin.</param>
[DllImport(WiringPiLibrary, EntryPoint = "pwmSetRange", SetLastError = true)] /// <param name="freq">The freq.</param>
public static extern void PwmSetRange(uint range); [DllImport(WiringPiLibrary, EntryPoint = "gpioClockSet", SetLastError = true)]
public static extern void GpioClockSet(Int32 pin, Int32 freq);
/// <summary> /// <summary>
/// This sets the divisor for the PWM clock. /// This function registers a function to received interrupts on the specified pin.
/// Note: The PWM control functions can not be used when in Sys mode. /// The edgeType parameter is either INT_EDGE_FALLING, INT_EDGE_RISING, INT_EDGE_BOTH or INT_EDGE_SETUP.
/// To understand more about the PWM system, youll need to read the Broadcom ARM peripherals manual. /// If it is INT_EDGE_SETUP then no initialisation of the pin will happen its assumed that you have already setup the pin elsewhere
/// </summary> /// (e.g. with the gpio program), but if you specify one of the other types, then the pin will be exported and initialised as specified.
/// <param name="divisor">The divisor.</param> /// This is accomplished via a suitable call to the gpio utility program, so it need to be available.
[DllImport(WiringPiLibrary, EntryPoint = "pwmSetClock", SetLastError = true)] /// The pin number is supplied in the current mode native wiringPi, BCM_GPIO, physical or Sys modes.
public static extern void PwmSetClock(int divisor); /// This function will work in any mode, and does not need root privileges to work.
/// The function will be called when the interrupt triggers. When it is triggered, its cleared in the dispatcher before calling your function,
/// so if a subsequent interrupt fires before you finish your handler, then it wont be missed. (However it can only track one more interrupt,
/// if more than one interrupt fires while one is being handled then they will be ignored)
/// This function is run at a high priority (if the program is run using sudo, or as root) and executes concurrently with the main program.
/// It has full access to all the global variables, open file handles and so on.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="mode">The mode.</param>
/// <param name="method">The method.</param>
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiISR", SetLastError = true)]
public static extern Int32 WiringPiISR(Int32 pin, Int32 mode, InterruptServiceRoutineCallback method);
/// <summary> /// <summary>
/// Undocumented function. /// This function creates a thread which is another function in your program previously declared using the PI_THREAD declaration.
/// </summary> /// This function is then run concurrently with your main program. An example may be to have this function wait for an interrupt while
/// <param name="pin">The pin.</param> /// your program carries on doing other tasks. The thread can indicate an event, or action by using global variables to
/// <param name="freq">The freq.</param> /// communicate back to the main program, or other threads.
[DllImport(WiringPiLibrary, EntryPoint = "gpioClockSet", SetLastError = true)] /// </summary>
public static extern void GpioClockSet(int pin, int freq); /// <param name="method">The method.</param>
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "piThreadCreate", SetLastError = true)]
public static extern Int32 PiThreadCreate(ThreadWorker method);
/// <summary> /// <summary>
/// This function registers a function to received interrupts on the specified pin. /// These allow you to synchronise variable updates from your main program to any threads running in your program. keyNum is a number from 0 to 3 and represents a key.
/// The edgeType parameter is either INT_EDGE_FALLING, INT_EDGE_RISING, INT_EDGE_BOTH or INT_EDGE_SETUP. /// When another process tries to lock the same key, it will be stalled until the first process has unlocked the same key.
/// If it is INT_EDGE_SETUP then no initialisation of the pin will happen its assumed that you have already setup the pin elsewhere /// You may need to use these functions to ensure that you get valid data when exchanging data between your main program and a thread
/// (e.g. with the gpio program), but if you specify one of the other types, then the pin will be exported and initialised as specified. /// otherwise its possible that the thread could wake-up halfway during your data copy and change the data
/// This is accomplished via a suitable call to the gpio utility program, so it need to be available. /// so the data you end up copying is incomplete, or invalid. See the wfi.c program in the examples directory for an example.
/// The pin number is supplied in the current mode native wiringPi, BCM_GPIO, physical or Sys modes. /// </summary>
/// This function will work in any mode, and does not need root privileges to work. /// <param name="key">The key.</param>
/// The function will be called when the interrupt triggers. When it is triggered, its cleared in the dispatcher before calling your function, [DllImport(WiringPiLibrary, EntryPoint = "piLock", SetLastError = true)]
/// so if a subsequent interrupt fires before you finish your handler, then it wont be missed. (However it can only track one more interrupt, public static extern void PiLock(Int32 key);
/// if more than one interrupt fires while one is being handled then they will be ignored)
/// This function is run at a high priority (if the program is run using sudo, or as root) and executes concurrently with the main program.
/// It has full access to all the global variables, open file handles and so on.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="mode">The mode.</param>
/// <param name="method">The method.</param>
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "wiringPiISR", SetLastError = true)]
public static extern int WiringPiISR(int pin, int mode, InterruptServiceRoutineCallback method);
/// <summary> /// <summary>
/// This function creates a thread which is another function in your program previously declared using the PI_THREAD declaration. /// These allow you to synchronise variable updates from your main program to any threads running in your program. keyNum is a number from 0 to 3 and represents a key.
/// This function is then run concurrently with your main program. An example may be to have this function wait for an interrupt while /// When another process tries to lock the same key, it will be stalled until the first process has unlocked the same key.
/// your program carries on doing other tasks. The thread can indicate an event, or action by using global variables to /// You may need to use these functions to ensure that you get valid data when exchanging data between your main program and a thread
/// communicate back to the main program, or other threads. /// otherwise its possible that the thread could wake-up halfway during your data copy and change the data
/// </summary> /// so the data you end up copying is incomplete, or invalid. See the wfi.c program in the examples directory for an example.
/// <param name="method">The method.</param> /// </summary>
/// <returns>The result code.</returns> /// <param name="key">The key.</param>
[DllImport(WiringPiLibrary, EntryPoint = "piThreadCreate", SetLastError = true)] [DllImport(WiringPiLibrary, EntryPoint = "piUnlock", SetLastError = true)]
public static extern int PiThreadCreate(ThreadWorker method); public static extern void PiUnlock(Int32 key);
/// <summary> /// <summary>
/// These allow you to synchronise variable updates from your main program to any threads running in your program. keyNum is a number from 0 to 3 and represents a key. /// This attempts to shift your program (or thread in a multi-threaded program) to a higher priority
/// When another process tries to lock the same key, it will be stalled until the first process has unlocked the same key. /// and enables a real-time scheduling. The priority parameter should be from 0 (the default) to 99 (the maximum).
/// You may need to use these functions to ensure that you get valid data when exchanging data between your main program and a thread /// This wont make your program go any faster, but it will give it a bigger slice of time when other programs are running.
/// otherwise its possible that the thread could wake-up halfway during your data copy and change the data /// The priority parameter works relative to others so you can make one program priority 1 and another priority 2
/// so the data you end up copying is incomplete, or invalid. See the wfi.c program in the examples directory for an example. /// and it will have the same effect as setting one to 10 and the other to 90 (as long as no other
/// </summary> /// programs are running with elevated priorities)
/// <param name="key">The key.</param> /// The return value is 0 for success and -1 for error. If an error is returned, the program should then consult the errno global variable, as per the usual conventions.
[DllImport(WiringPiLibrary, EntryPoint = "piLock", SetLastError = true)] /// Note: Only programs running as root can change their priority. If called from a non-root program then nothing happens.
public static extern void PiLock(int key); /// </summary>
/// <param name="priority">The priority.</param>
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "piHiPri", SetLastError = true)]
public static extern Int32 PiHiPri(Int32 priority);
/// <summary> /// <summary>
/// These allow you to synchronise variable updates from your main program to any threads running in your program. keyNum is a number from 0 to 3 and represents a key. /// This causes program execution to pause for at least howLong milliseconds.
/// When another process tries to lock the same key, it will be stalled until the first process has unlocked the same key. /// Due to the multi-tasking nature of Linux it could be longer.
/// You may need to use these functions to ensure that you get valid data when exchanging data between your main program and a thread /// Note that the maximum delay is an unsigned 32-bit integer or approximately 49 days.
/// otherwise its possible that the thread could wake-up halfway during your data copy and change the data /// </summary>
/// so the data you end up copying is incomplete, or invalid. See the wfi.c program in the examples directory for an example. /// <param name="howLong">The how long.</param>
/// </summary> [DllImport(WiringPiLibrary, EntryPoint = "delay", SetLastError = true)]
/// <param name="key">The key.</param> public static extern void Delay(UInt32 howLong);
[DllImport(WiringPiLibrary, EntryPoint = "piUnlock", SetLastError = true)]
public static extern void PiUnlock(int key);
/// <summary> /// <summary>
/// This attempts to shift your program (or thread in a multi-threaded program) to a higher priority /// This causes program execution to pause for at least howLong microseconds.
/// and enables a real-time scheduling. The priority parameter should be from 0 (the default) to 99 (the maximum). /// Due to the multi-tasking nature of Linux it could be longer.
/// This wont make your program go any faster, but it will give it a bigger slice of time when other programs are running. /// Note that the maximum delay is an unsigned 32-bit integer microseconds or approximately 71 minutes.
/// The priority parameter works relative to others so you can make one program priority 1 and another priority 2 /// Delays under 100 microseconds are timed using a hard-coded loop continually polling the system time,
/// and it will have the same effect as setting one to 10 and the other to 90 (as long as no other /// Delays over 100 microseconds are done using the system nanosleep() function You may need to consider the implications
/// programs are running with elevated priorities) /// of very short delays on the overall performance of the system, especially if using threads.
/// The return value is 0 for success and -1 for error. If an error is returned, the program should then consult the errno global variable, as per the usual conventions. /// </summary>
/// Note: Only programs running as root can change their priority. If called from a non-root program then nothing happens. /// <param name="howLong">The how long.</param>
/// </summary> [DllImport(WiringPiLibrary, EntryPoint = "delayMicroseconds", SetLastError = true)]
/// <param name="priority">The priority.</param> public static extern void DelayMicroseconds(UInt32 howLong);
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "piHiPri", SetLastError = true)]
public static extern int PiHiPri(int priority);
/// <summary> /// <summary>
/// This causes program execution to pause for at least howLong milliseconds. /// This returns a number representing the number of milliseconds since your program called one of the wiringPiSetup functions.
/// Due to the multi-tasking nature of Linux it could be longer. /// It returns an unsigned 32-bit number which wraps after 49 days.
/// Note that the maximum delay is an unsigned 32-bit integer or approximately 49 days. /// </summary>
/// </summary> /// <returns>The result code.</returns>
/// <param name="howLong">The how long.</param> [DllImport(WiringPiLibrary, EntryPoint = "millis", SetLastError = true)]
[DllImport(WiringPiLibrary, EntryPoint = "delay", SetLastError = true)] public static extern UInt32 Millis();
public static extern void Delay(uint howLong);
/// <summary> /// <summary>
/// This causes program execution to pause for at least howLong microseconds. /// This returns a number representing the number of microseconds since your program called one of
/// Due to the multi-tasking nature of Linux it could be longer. /// the wiringPiSetup functions. It returns an unsigned 32-bit number which wraps after approximately 71 minutes.
/// Note that the maximum delay is an unsigned 32-bit integer microseconds or approximately 71 minutes. /// </summary>
/// Delays under 100 microseconds are timed using a hard-coded loop continually polling the system time, /// <returns>The result code.</returns>
/// Delays over 100 microseconds are done using the system nanosleep() function You may need to consider the implications [DllImport(WiringPiLibrary, EntryPoint = "micros", SetLastError = true)]
/// of very short delays on the overall performance of the system, especially if using threads. public static extern UInt32 Micros();
/// </summary>
/// <param name="howLong">The how long.</param>
[DllImport(WiringPiLibrary, EntryPoint = "delayMicroseconds", SetLastError = true)]
public static extern void DelayMicroseconds(uint howLong);
/// <summary> #endregion
/// This returns a number representing the number of milliseconds since your program called one of the wiringPiSetup functions. }
/// It returns an unsigned 32-bit number which wraps after 49 days.
/// </summary>
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "millis", SetLastError = true)]
public static extern uint Millis();
/// <summary>
/// This returns a number representing the number of microseconds since your program called one of
/// the wiringPiSetup functions. It returns an unsigned 32-bit number which wraps after approximately 71 minutes.
/// </summary>
/// <returns>The result code.</returns>
[DllImport(WiringPiLibrary, EntryPoint = "micros", SetLastError = true)]
public static extern uint Micros();
#endregion
}
} }

View File

@ -1,65 +1,56 @@
namespace Unosquare.WiringPi.Resources using System;
{ using System.Collections.ObjectModel;
using Native; using System.IO;
using System; using System.Reflection;
using System.Collections.ObjectModel;
using System.IO; using Unosquare.WiringPi.Native;
using System.Reflection;
namespace Unosquare.WiringPi.Resources {
/// <summary>
/// Provides access to embedded assembly files.
/// </summary>
internal static class EmbeddedResources {
/// <summary>
/// Initializes static members of the <see cref="EmbeddedResources"/> class.
/// </summary>
static EmbeddedResources() => ResourceNames = new ReadOnlyCollection<String>(typeof(EmbeddedResources).Assembly.GetManifestResourceNames());
/// <summary> /// <summary>
/// Provides access to embedded assembly files. /// Gets the resource names.
/// </summary> /// </summary>
internal static class EmbeddedResources /// <value>
{ /// The resource names.
/// <summary> /// </value>
/// Initializes static members of the <see cref="EmbeddedResources"/> class. public static ReadOnlyCollection<String> ResourceNames {
/// </summary> get;
static EmbeddedResources()
{
ResourceNames =
new ReadOnlyCollection<string>(typeof(EmbeddedResources).Assembly.GetManifestResourceNames());
}
/// <summary>
/// Gets the resource names.
/// </summary>
/// <value>
/// The resource names.
/// </value>
public static ReadOnlyCollection<string> ResourceNames { get; }
/// <summary>
/// Extracts all the file resources to the specified base path.
/// </summary>
public static void ExtractAll()
{
var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var executablePermissions = SysCall.StringToInteger("0777", IntPtr.Zero, 8);
foreach (var resourceName in ResourceNames)
{
var filename = resourceName.Substring($"{typeof(EmbeddedResources).Namespace}.".Length);
var targetPath = Path.Combine(basePath, filename);
if (File.Exists(targetPath)) return;
using (var stream = typeof(EmbeddedResources).Assembly
.GetManifestResourceStream(resourceName))
{
using (var outputStream = File.OpenWrite(targetPath))
{
stream?.CopyTo(outputStream);
}
try
{
SysCall.Chmod(targetPath, (uint)executablePermissions);
}
catch
{
/* Ignore */
}
}
}
}
} }
/// <summary>
/// Extracts all the file resources to the specified base path.
/// </summary>
public static void ExtractAll() {
String basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
Int32 executablePermissions = SysCall.StringToInteger("0777", IntPtr.Zero, 8);
foreach(String resourceName in ResourceNames) {
String filename = resourceName.Substring($"{typeof(EmbeddedResources).Namespace}.".Length);
String targetPath = Path.Combine(basePath, filename);
if(File.Exists(targetPath)) {
return;
}
using(Stream stream = typeof(EmbeddedResources).Assembly.GetManifestResourceStream(resourceName)) {
using(FileStream outputStream = File.OpenWrite(targetPath)) {
stream?.CopyTo(outputStream);
}
try {
_ = SysCall.Chmod(targetPath, (UInt32)executablePermissions);
} catch {
/* Ignore */
}
}
}
}
}
} }

View File

@ -1,43 +1,45 @@
namespace Unosquare.WiringPi using System;
{
using RaspberryIO.Abstractions;
/// <summary> using Unosquare.RaspberryIO.Abstractions;
/// The SPI Bus containing the 2 SPI channels.
/// </summary>
public class SpiBus : ISpiBus
{
/// <inheritdoc />
public int Channel0Frequency { get; set; }
/// <inheritdoc /> namespace Unosquare.WiringPi {
public int Channel1Frequency { get; set; } /// <summary>
/// The SPI Bus containing the 2 SPI channels.
/// <inheritdoc /> /// </summary>
public int DefaultFrequency => 8000000; public class SpiBus : ISpiBus {
/// <inheritdoc />
/// <inheritdoc /> public Int32 Channel0Frequency {
public ISpiChannel Channel0 get; set;
{
get
{
if (Channel0Frequency == 0)
Channel0Frequency = DefaultFrequency;
return SpiChannel.Retrieve(SpiChannelNumber.Channel0, Channel0Frequency);
}
}
/// <inheritdoc />
public ISpiChannel Channel1
{
get
{
if (Channel1Frequency == 0)
Channel1Frequency = DefaultFrequency;
return SpiChannel.Retrieve(SpiChannelNumber.Channel1, Channel1Frequency);
}
}
} }
/// <inheritdoc />
public Int32 Channel1Frequency {
get; set;
}
/// <inheritdoc />
public Int32 DefaultFrequency => 8000000;
/// <inheritdoc />
public ISpiChannel Channel0 {
get {
if(this.Channel0Frequency == 0) {
this.Channel0Frequency = this.DefaultFrequency;
}
return SpiChannel.Retrieve(SpiChannelNumber.Channel0, this.Channel0Frequency);
}
}
/// <inheritdoc />
public ISpiChannel Channel1 {
get {
if(this.Channel1Frequency == 0) {
this.Channel1Frequency = this.DefaultFrequency;
}
return SpiChannel.Retrieve(SpiChannelNumber.Channel1, this.Channel1Frequency);
}
}
}
} }

View File

@ -1,130 +1,131 @@
namespace Unosquare.WiringPi using System;
{ using System.Collections.Generic;
using Native; using System.Threading.Tasks;
using RaspberryIO.Abstractions;
using RaspberryIO.Abstractions.Native; using Swan;
using Swan;
using System; using Unosquare.RaspberryIO.Abstractions;
using System.Collections.Generic; using Unosquare.RaspberryIO.Abstractions.Native;
using System.Threading.Tasks;
namespace Unosquare.WiringPi {
/// <summary>
/// Provides access to using the SPI buses on the GPIO.
/// SPI is a bus that works like a ring shift register
/// The number of bytes pushed is equal to the number of bytes received.
/// </summary>
public sealed class SpiChannel : ISpiChannel {
/// <summary>
/// The minimum frequency of a SPI Channel.
/// </summary>
public const Int32 MinFrequency = 500000;
/// <summary> /// <summary>
/// Provides access to using the SPI buses on the GPIO. /// The maximum frequency of a SPI channel.
/// SPI is a bus that works like a ring shift register
/// The number of bytes pushed is equal to the number of bytes received.
/// </summary> /// </summary>
public sealed class SpiChannel : ISpiChannel public const Int32 MaxFrequency = 32000000;
{
/// <summary>
/// The minimum frequency of a SPI Channel.
/// </summary>
public const int MinFrequency = 500000;
/// <summary> private static readonly Object SyncRoot = new Object();
/// The maximum frequency of a SPI channel. private static readonly Dictionary<SpiChannelNumber, SpiChannel> Buses = new Dictionary<SpiChannelNumber, SpiChannel>();
/// </summary> private readonly Object _syncLock = new Object();
public const int MaxFrequency = 32000000;
private static readonly object SyncRoot = new object(); /// <summary>
private static readonly Dictionary<SpiChannelNumber, SpiChannel> Buses = new Dictionary<SpiChannelNumber, SpiChannel>(); /// Initializes a new instance of the <see cref="SpiChannel"/> class.
private readonly object _syncLock = new object(); /// </summary>
/// <param name="channel">The channel.</param>
/// <param name="frequency">The frequency.</param>
private SpiChannel(SpiChannelNumber channel, Int32 frequency) {
lock(SyncRoot) {
this.Frequency = frequency.Clamp(MinFrequency, MaxFrequency);
this.Channel = (Int32)channel;
this.FileDescriptor = Native.WiringPi.WiringPiSPISetup((Int32)channel, this.Frequency);
/// <summary> if(this.FileDescriptor < 0) {
/// Initializes a new instance of the <see cref="SpiChannel"/> class. HardwareException.Throw(nameof(SpiChannel), channel.ToString());
/// </summary>
/// <param name="channel">The channel.</param>
/// <param name="frequency">The frequency.</param>
private SpiChannel(SpiChannelNumber channel, int frequency)
{
lock (SyncRoot)
{
Frequency = frequency.Clamp(MinFrequency, MaxFrequency);
Channel = (int)channel;
FileDescriptor = WiringPi.WiringPiSPISetup((int)channel, Frequency);
if (FileDescriptor < 0)
{
HardwareException.Throw(nameof(SpiChannel), channel.ToString());
}
}
}
/// <inheritdoc />
public int FileDescriptor { get; }
/// <inheritdoc />
public int Channel { get; }
/// <inheritdoc />
public int Frequency { get; }
/// <inheritdoc />
public byte[] SendReceive(byte[] buffer)
{
if (buffer == null || buffer.Length == 0)
return null;
lock (_syncLock)
{
var spiBuffer = new byte[buffer.Length];
Array.Copy(buffer, spiBuffer, buffer.Length);
var result = WiringPi.WiringPiSPIDataRW(Channel, spiBuffer, spiBuffer.Length);
if (result < 0) HardwareException.Throw(nameof(SpiChannel), nameof(SendReceive));
return spiBuffer;
}
}
/// <summary>
/// Sends data and simultaneously receives the data in the return buffer.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns>
/// The read bytes from the ring-style bus.
/// </returns>
public Task<byte[]> SendReceiveAsync(byte[] buffer) => Task.Run(() => SendReceive(buffer));
/// <inheritdoc />
public void Write(byte[] buffer)
{
lock (_syncLock)
{
var result = SysCall.Write(FileDescriptor, buffer, buffer.Length);
if (result < 0)
HardwareException.Throw(nameof(SpiChannel), nameof(Write));
}
}
/// <summary>
/// Writes the specified buffer the the underlying FileDescriptor.
/// Do not use this method if you expect data back.
/// This method is efficient if used in a fire-and-forget scenario
/// like sending data over to those long RGB LED strips.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns>The awaitable task.</returns>
public Task WriteAsync(byte[] buffer) => Task.Run(() => { Write(buffer); });
/// <summary>
/// Retrieves the spi bus. If the bus channel is not registered it sets it up automatically.
/// If it had been previously registered, then the bus is simply returned.
/// </summary>
/// <param name="channel">The channel.</param>
/// <param name="frequency">The frequency.</param>
/// <returns>The usable SPI channel.</returns>
internal static ISpiChannel Retrieve(SpiChannelNumber channel, int frequency)
{
lock (SyncRoot)
{
if (Buses.ContainsKey(channel))
return Buses[channel];
var newBus = new SpiChannel(channel, frequency);
Buses[channel] = newBus;
return newBus;
}
} }
}
} }
/// <inheritdoc />
public Int32 FileDescriptor {
get;
}
/// <inheritdoc />
public Int32 Channel {
get;
}
/// <inheritdoc />
public Int32 Frequency {
get;
}
/// <inheritdoc />
public Byte[] SendReceive(Byte[] buffer) {
if(buffer == null || buffer.Length == 0) {
return null;
}
lock(this._syncLock) {
Byte[] spiBuffer = new Byte[buffer.Length];
Array.Copy(buffer, spiBuffer, buffer.Length);
Int32 result = Native.WiringPi.WiringPiSPIDataRW(this.Channel, spiBuffer, spiBuffer.Length);
if(result < 0) {
HardwareException.Throw(nameof(SpiChannel), nameof(SendReceive));
}
return spiBuffer;
}
}
/// <summary>
/// Sends data and simultaneously receives the data in the return buffer.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns>
/// The read bytes from the ring-style bus.
/// </returns>
public Task<Byte[]> SendReceiveAsync(Byte[] buffer) => Task.Run(() => this.SendReceive(buffer));
/// <inheritdoc />
public void Write(Byte[] buffer) {
lock(this._syncLock) {
Int32 result = Native.SysCall.Write(this.FileDescriptor, buffer, buffer.Length);
if(result < 0) {
HardwareException.Throw(nameof(SpiChannel), nameof(Write));
}
}
}
/// <summary>
/// Writes the specified buffer the the underlying FileDescriptor.
/// Do not use this method if you expect data back.
/// This method is efficient if used in a fire-and-forget scenario
/// like sending data over to those long RGB LED strips.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns>The awaitable task.</returns>
public Task WriteAsync(Byte[] buffer) => Task.Run(() => { this.Write(buffer); });
/// <summary>
/// Retrieves the spi bus. If the bus channel is not registered it sets it up automatically.
/// If it had been previously registered, then the bus is simply returned.
/// </summary>
/// <param name="channel">The channel.</param>
/// <param name="frequency">The frequency.</param>
/// <returns>The usable SPI channel.</returns>
internal static ISpiChannel Retrieve(SpiChannelNumber channel, Int32 frequency) {
lock(SyncRoot) {
if(Buses.ContainsKey(channel)) {
return Buses[channel];
}
SpiChannel newBus = new SpiChannel(channel, frequency);
Buses[channel] = newBus;
return newBus;
}
}
}
} }

View File

@ -1,45 +1,42 @@
namespace Unosquare.WiringPi using System;
{
using Native;
using RaspberryIO.Abstractions;
using System;
/// <summary> using Unosquare.RaspberryIO.Abstractions;
/// Represents the WiringPi system info.
/// </summary>
/// <seealso cref="ISystemInfo" />
public class SystemInfo : ISystemInfo
{
private static readonly object Lock = new object();
private static bool _revGetted;
private static BoardRevision _boardRevision = BoardRevision.Rev2;
/// <inheritdoc /> namespace Unosquare.WiringPi {
public BoardRevision BoardRevision => GetBoardRevision(); /// <summary>
/// Represents the WiringPi system info.
/// </summary>
/// <seealso cref="ISystemInfo" />
public class SystemInfo : ISystemInfo {
private static readonly Object Lock = new Object();
private static Boolean _revGetted;
private static BoardRevision _boardRevision = BoardRevision.Rev2;
/// <inheritdoc /> /// <inheritdoc />
public Version LibraryVersion public BoardRevision BoardRevision => GetBoardRevision();
{
get
{
var libParts = WiringPi.WiringPiLibrary.Split('.');
var major = int.Parse(libParts[libParts.Length - 2]);
var minor = int.Parse(libParts[libParts.Length - 1]);
return new Version(major, minor);
}
}
internal static BoardRevision GetBoardRevision() /// <inheritdoc />
{ public Version LibraryVersion {
lock (Lock) get {
{ String[] libParts = Native.WiringPi.WiringPiLibrary.Split('.');
if (_revGetted) return _boardRevision; Int32 major = Int32.Parse(libParts[libParts.Length - 2]);
var val = WiringPi.PiBoardRev(); Int32 minor = Int32.Parse(libParts[libParts.Length - 1]);
_boardRevision = val == 1 ? BoardRevision.Rev1 : BoardRevision.Rev2; return new Version(major, minor);
_revGetted = true; }
}
return _boardRevision;
}
} }
internal static BoardRevision GetBoardRevision() {
lock(Lock) {
if(_revGetted) {
return _boardRevision;
}
Int32 val = Native.WiringPi.PiBoardRev();
_boardRevision = val == 1 ? BoardRevision.Rev1 : BoardRevision.Rev2;
_revGetted = true;
}
return _boardRevision;
}
}
} }

View File

@ -1,72 +1,71 @@
namespace Unosquare.WiringPi using System;
{
using Native; using Swan;
using RaspberryIO.Abstractions;
using RaspberryIO.Abstractions.Native; using Unosquare.RaspberryIO.Abstractions;
using Swan; using Unosquare.RaspberryIO.Abstractions.Native;
using System;
namespace Unosquare.WiringPi {
/// <summary>
/// Use this class to access threading methods using interop.
/// </summary>
/// <seealso cref="IThreading" />
public class Threading : IThreading {
/// <summary>
/// This attempts to shift your program (or thread in a multi-threaded program) to a higher priority and
/// enables a real-time scheduling. The priority parameter should be from 0 (the default) to 99 (the maximum).
/// This wont make your program go any faster, but it will give it a bigger slice of time when other programs
/// are running. The priority parameter works relative to others so you can make one program priority 1 and
/// another priority 2 and it will have the same effect as setting one to 10 and the other to 90
/// (as long as no other programs are running with elevated priorities).
/// </summary>
/// <param name="priority">The priority.</param>
public void SetThreadPriority(Int32 priority) {
priority = priority.Clamp(0, 99);
Int32 result = Native.WiringPi.PiHiPri(priority);
if(result < 0) {
HardwareException.Throw(nameof(Timing), nameof(SetThreadPriority));
}
}
/// <summary> /// <summary>
/// Use this class to access threading methods using interop. /// These allow you to synchronize variable updates from your main program to any threads running in your program.
/// keyNum is a number from 0 to 3 and represents a “key”. When another process tries to lock the same key,
/// it will be stalled until the first process has unlocked the same key.
/// </summary> /// </summary>
/// <seealso cref="IThreading" /> /// <param name="key">The key.</param>
public class Threading : IThreading public void Lock(ThreadLockKey key) => Native.WiringPi.PiLock((Int32)key);
{
/// <summary>
/// This attempts to shift your program (or thread in a multi-threaded program) to a higher priority and
/// enables a real-time scheduling. The priority parameter should be from 0 (the default) to 99 (the maximum).
/// This wont make your program go any faster, but it will give it a bigger slice of time when other programs
/// are running. The priority parameter works relative to others so you can make one program priority 1 and
/// another priority 2 and it will have the same effect as setting one to 10 and the other to 90
/// (as long as no other programs are running with elevated priorities).
/// </summary>
/// <param name="priority">The priority.</param>
public void SetThreadPriority(int priority)
{
priority = priority.Clamp(0, 99);
var result = WiringPi.PiHiPri(priority);
if (result < 0) HardwareException.Throw(nameof(Timing), nameof(SetThreadPriority));
}
/// <summary> /// <summary>
/// These allow you to synchronize variable updates from your main program to any threads running in your program. /// These allow you to synchronize variable updates from your main program to any threads running in your program.
/// keyNum is a number from 0 to 3 and represents a “key”. When another process tries to lock the same key, /// keyNum is a number from 0 to 3 and represents a “key”. When another process tries to lock the same key,
/// it will be stalled until the first process has unlocked the same key. /// it will be stalled until the first process has unlocked the same key.
/// </summary> /// </summary>
/// <param name="key">The key.</param> /// <param name="key">The key.</param>
public void Lock(ThreadLockKey key) => WiringPi.PiLock((int)key); public void Unlock(ThreadLockKey key) => Native.WiringPi.PiUnlock((Int32)key);
/// <summary> /// <inheritdoc />
/// These allow you to synchronize variable updates from your main program to any threads running in your program. /// <summary>
/// keyNum is a number from 0 to 3 and represents a “key”. When another process tries to lock the same key, /// This is really nothing more than a simplified interface to the Posix threads mechanism that Linux supports.
/// it will be stalled until the first process has unlocked the same key. /// See the manual pages on Posix threads (man pthread) if you need more control over them.
/// </summary> /// </summary>
/// <param name="key">The key.</param> /// <exception cref="ArgumentNullException">worker.</exception>
public void Unlock(ThreadLockKey key) => WiringPi.PiUnlock((int)key); public void StartThread(Action worker) {
if(worker == null) {
throw new ArgumentNullException(nameof(worker));
}
/// <inheritdoc /> Int32 result = Native.WiringPi.PiThreadCreate(new Native.ThreadWorker(worker));
/// <summary>
/// This is really nothing more than a simplified interface to the Posix threads mechanism that Linux supports.
/// See the manual pages on Posix threads (man pthread) if you need more control over them.
/// </summary>
/// <exception cref="ArgumentNullException">worker.</exception>
public void StartThread(Action worker)
{
if (worker == null)
throw new ArgumentNullException(nameof(worker));
var result = WiringPi.PiThreadCreate(new ThreadWorker(worker)); if(result != 0) {
HardwareException.Throw(nameof(Timing), nameof(StartThread));
if (result != 0) }
HardwareException.Throw(nameof(Timing), nameof(StartThread));
}
/// <inheritdoc />
public UIntPtr StartThreadEx(Action<UIntPtr> worker, UIntPtr userData) =>
throw new NotSupportedException("WiringPi does only support a simple thread callback that has no parameters.");
/// <inheritdoc />
public void StopThreadEx(UIntPtr handle) =>
throw new NotSupportedException("WiringPi does not support stopping threads.");
} }
/// <inheritdoc />
public UIntPtr StartThreadEx(Action<UIntPtr> worker, UIntPtr userData) => throw new NotSupportedException("WiringPi does only support a simple thread callback that has no parameters.");
/// <inheritdoc />
public void StopThreadEx(UIntPtr handle) => throw new NotSupportedException("WiringPi does not support stopping threads.");
}
} }

View File

@ -1,36 +1,35 @@
namespace Unosquare.WiringPi using System;
{
using Native;
using RaspberryIO.Abstractions;
using Unosquare.RaspberryIO.Abstractions;
namespace Unosquare.WiringPi {
/// <summary>
/// Provides access to timing and threading properties and methods.
/// </summary>
public class Timing : ITiming {
/// <inheritdoc />
/// <summary> /// <summary>
/// Provides access to timing and threading properties and methods. /// This returns a number representing the number of milliseconds since your program
/// initialized the GPIO controller.
/// It returns an unsigned 32-bit number which wraps after 49 days.
/// </summary> /// </summary>
public class Timing : ITiming public UInt32 Milliseconds => Native.WiringPi.Millis();
{
/// <inheritdoc />
/// <summary>
/// This returns a number representing the number of milliseconds since your program
/// initialized the GPIO controller.
/// It returns an unsigned 32-bit number which wraps after 49 days.
/// </summary>
public uint Milliseconds => WiringPi.Millis();
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
/// This returns a number representing the number of microseconds since your /// This returns a number representing the number of microseconds since your
/// program initialized the GPIO controller /// program initialized the GPIO controller
/// It returns an unsigned 32-bit number which wraps after approximately 71 minutes. /// It returns an unsigned 32-bit number which wraps after approximately 71 minutes.
/// </summary> /// </summary>
public uint Microseconds => WiringPi.Micros(); public UInt32 Microseconds => Native.WiringPi.Micros();
/// <inheritdoc cref="ITiming.SleepMilliseconds(uint)" /> /// <inheritdoc cref="ITiming.SleepMilliseconds(UInt32)" />
public static void Sleep(uint millis) => WiringPi.Delay(millis); public static void Sleep(UInt32 millis) => Native.WiringPi.Delay(millis);
/// <inheritdoc /> /// <inheritdoc />
public void SleepMilliseconds(uint millis) => Sleep(millis); public void SleepMilliseconds(UInt32 millis) => Sleep(millis);
/// <inheritdoc /> /// <inheritdoc />
public void SleepMicroseconds(uint micros) => WiringPi.DelayMicroseconds(micros); public void SleepMicroseconds(UInt32 micros) => Native.WiringPi.DelayMicroseconds(micros);
} }
} }