Codingstyles...

This commit is contained in:
BlubbFish 2019-12-06 22:24:34 +01:00
parent f9015f8022
commit a7a7278eda
15 changed files with 1091 additions and 1081 deletions

View File

@ -1,20 +1,13 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Represents Definitions for GPIO information. /// Represents Definitions for GPIO information.
/// </summary> /// </summary>
public static class Definitions public static class Definitions {
{ private static readonly Int32[] GpioToPhysR1 = { 3, 5, -1, -1, 7, -1, -1, 26, 24, 21, 19, 23, -1, -1, 8, 10, -1, 11, 12, -1, -1, 13, 15, 16, 18, 22, -1, -1, -1, -1, -1, -1 };
private static readonly int[] GpioToPhysR1 =
{
3, 5, -1, -1, 7, -1, -1, 26, 24, 21, 19, 23, -1, -1, 8, 10, -1, 11, 12, -1, -1, 13, 15, 16, 18, 22, -1, -1, -1, -1, -1, -1,
};
private static readonly int[] GpioToPhysR2 = private static readonly Int32[] GpioToPhysR2 = { 27, 28, 3, 5, 7, 29, 31, 26, 24, 21, 19, 23, 32, 33, 8, 10, 36, 11, 12, 35, 38, 40, 15, 16, 18, 22, 37, 13, /*P1*/ 3, 4, 5, 6 /*P5*/ };
{
27, 28, 3, 5, 7, 29, 31, 26, 24, 21, 19, 23, 32, 33, 8, 10, 36, 11, 12, 35, 38, 40, 15, 16, 18, 22, 37, 13, // P1
3, 4, 5, 6, // P5
};
/// <summary> /// <summary>
/// BCMs to physical pin number. /// BCMs to physical pin number.
@ -22,7 +15,6 @@
/// <param name="rev">The rev.</param> /// <param name="rev">The rev.</param>
/// <param name="bcmPin">The BCM pin.</param> /// <param name="bcmPin">The BCM pin.</param>
/// <returns>The physical pin number.</returns> /// <returns>The physical pin number.</returns>
public static int BcmToPhysicalPinNumber(BoardRevision rev, BcmPin bcmPin) => public static Int32 BcmToPhysicalPinNumber(BoardRevision rev, BcmPin bcmPin) => rev == BoardRevision.Rev1 ? GpioToPhysR1[(Int32)bcmPin] : GpioToPhysR2[(Int32)bcmPin];
rev == BoardRevision.Rev1 ? GpioToPhysR1[(int)bcmPin] : GpioToPhysR2[(int)bcmPin];
} }
} }

View File

@ -1,10 +1,8 @@
namespace Unosquare.RaspberryIO.Abstractions namespace Unosquare.RaspberryIO.Abstractions {
{
/// <summary> /// <summary>
/// Defines the SPI channel numbers. /// Defines the SPI channel numbers.
/// </summary> /// </summary>
public enum SpiChannelNumber public enum SpiChannelNumber {
{
/// <summary> /// <summary>
/// The channel 0 /// The channel 0
/// </summary> /// </summary>
@ -19,8 +17,7 @@
/// <summary> /// <summary>
/// Defines the GPIO Pin values 0 for low, 1 for High. /// Defines the GPIO Pin values 0 for low, 1 for High.
/// </summary> /// </summary>
public enum GpioPinValue public enum GpioPinValue {
{
/// <summary> /// <summary>
/// Digital high /// Digital high
/// </summary> /// </summary>
@ -36,8 +33,7 @@
/// The GPIO pin resistor mode. This is used on input pins so that their /// The GPIO pin resistor mode. This is used on input pins so that their
/// lines are not floating. /// lines are not floating.
/// </summary> /// </summary>
public enum GpioPinResistorPullMode public enum GpioPinResistorPullMode {
{
/// <summary> /// <summary>
/// Pull resistor not active. Line floating /// Pull resistor not active. Line floating
/// </summary> /// </summary>
@ -57,8 +53,7 @@
/// <summary> /// <summary>
/// Defines the different drive modes of a GPIO pin. /// Defines the different drive modes of a GPIO pin.
/// </summary> /// </summary>
public enum GpioPinDriveMode public enum GpioPinDriveMode {
{
/// <summary> /// <summary>
/// Input drive mode (perform reads) /// Input drive mode (perform reads)
/// </summary> /// </summary>
@ -103,8 +98,7 @@
/// <summary> /// <summary>
/// Defines the different threading locking keys. /// Defines the different threading locking keys.
/// </summary> /// </summary>
public enum ThreadLockKey public enum ThreadLockKey {
{
/// <summary> /// <summary>
/// The lock 0 /// The lock 0
/// </summary> /// </summary>
@ -129,8 +123,7 @@
/// <summary> /// <summary>
/// Defines the different edge detection modes for pin interrupts. /// Defines the different edge detection modes for pin interrupts.
/// </summary> /// </summary>
public enum EdgeDetection public enum EdgeDetection {
{
/// <summary> /// <summary>
/// Falling Edge /// Falling Edge
/// </summary> /// </summary>
@ -150,8 +143,7 @@
/// <summary> /// <summary>
/// The hardware revision of the board. /// The hardware revision of the board.
/// </summary> /// </summary>
public enum BoardRevision public enum BoardRevision {
{
/// <summary> /// <summary>
/// Revision 1 (the early Model A and B's). /// Revision 1 (the early Model A and B's).
/// </summary> /// </summary>
@ -166,8 +158,7 @@
/// <summary> /// <summary>
/// Defines the Header connectors available. /// Defines the Header connectors available.
/// </summary> /// </summary>
public enum GpioHeader public enum GpioHeader {
{
/// <summary> /// <summary>
/// Not defined /// Not defined
/// </summary> /// </summary>
@ -187,8 +178,7 @@
/// <summary> /// <summary>
/// Defines all the BCM Pin numbers available for the user. /// Defines all the BCM Pin numbers available for the user.
/// </summary> /// </summary>
public enum BcmPin public enum BcmPin {
{
/// <summary> /// <summary>
/// GPIO 0 /// GPIO 0
/// </summary> /// </summary>
@ -354,8 +344,7 @@
/// Enumerates the different pins on the P1 Header. /// Enumerates the different pins on the P1 Header.
/// Enumeration values correspond to the physical pin number. /// Enumeration values correspond to the physical pin number.
/// </summary> /// </summary>
public enum P1 public enum P1 {
{
/// <summary> /// <summary>
/// Header P1 Physical Pin 3. GPIO 0 for rev1 or GPIO 2 for rev2. /// Header P1 Physical Pin 3. GPIO 0 for rev1 or GPIO 2 for rev2.
/// </summary> /// </summary>
@ -502,8 +491,7 @@
/// as commonly referenced by Raspberry Pi documentation. /// as commonly referenced by Raspberry Pi documentation.
/// Enumeration values correspond to the physical pin number. /// Enumeration values correspond to the physical pin number.
/// </summary> /// </summary>
public enum P5 public enum P5 {
{
/// <summary> /// <summary>
/// Header P5 Physical Pin 3, GPIO 28. /// Header P5 Physical Pin 3, GPIO 28.
/// </summary> /// </summary>

View File

@ -1,10 +1,8 @@
namespace Unosquare.RaspberryIO.Abstractions namespace Unosquare.RaspberryIO.Abstractions {
{
/// <summary> /// <summary>
/// Interface for bootstrapping an <see cref="Abstractions"/> implementation. /// Interface for bootstrapping an <see cref="Abstractions"/> implementation.
/// </summary> /// </summary>
public interface IBootstrap public interface IBootstrap {
{
/// <summary> /// <summary>
/// Bootstraps an <see cref="Abstractions"/> implementation. /// Bootstraps an <see cref="Abstractions"/> implementation.
/// </summary> /// </summary>

View File

@ -1,13 +1,12 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
using System.Collections.Generic; using System.Collections.Generic;
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interface for Raspberry Pi GPIO controller. /// Interface for Raspberry Pi GPIO controller.
/// </summary> /// </summary>
/// <seealso cref="System.Collections.Generic.IReadOnlyCollection{IGpioPin}" /> /// <seealso cref="System.Collections.Generic.IReadOnlyCollection{IGpioPin}" />
public interface IGpioController : IReadOnlyCollection<IGpioPin> public interface IGpioController : IReadOnlyCollection<IGpioPin> {
{
/// <summary> /// <summary>
/// Gets the <see cref="IGpioPin"/> with the specified BCM pin. /// Gets the <see cref="IGpioPin"/> with the specified BCM pin.
/// </summary> /// </summary>
@ -16,7 +15,7 @@
/// </value> /// </value>
/// <param name="bcmPinNumber">The BCM pin number.</param> /// <param name="bcmPinNumber">The BCM pin number.</param>
/// <returns>A reference to the GPIO pin.</returns> /// <returns>A reference to the GPIO pin.</returns>
IGpioPin this[int bcmPinNumber] { get; } IGpioPin this[Int32 bcmPinNumber] { get; }
/// <summary> /// <summary>
/// Gets the <see cref="IGpioPin"/> with the specified BCM pin. /// Gets the <see cref="IGpioPin"/> with the specified BCM pin.

View File

@ -1,19 +1,19 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
using System;
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interface for GPIO Pin on a RaspberryPi board. /// Interface for GPIO Pin on a RaspberryPi board.
/// </summary> /// </summary>
public interface IGpioPin public interface IGpioPin {
{
/// <summary> /// <summary>
/// Gets the <see cref="Abstractions.BcmPin"/>. /// Gets the <see cref="Abstractions.BcmPin"/>.
/// </summary> /// </summary>
/// <value> /// <value>
/// The pin number. /// The pin number.
/// </value> /// </value>
BcmPin BcmPin { get; } BcmPin BcmPin {
get;
}
/// <summary> /// <summary>
/// Gets the BCM chip (hardware) pin number. /// Gets the BCM chip (hardware) pin number.
@ -21,17 +21,23 @@
/// <value> /// <value>
/// The pin number. /// The pin number.
/// </value> /// </value>
int BcmPinNumber { get; } Int32 BcmPinNumber {
get;
}
/// <summary> /// <summary>
/// Gets the physical (header) pin number. /// Gets the physical (header) pin number.
/// </summary> /// </summary>
int PhysicalPinNumber { get; } Int32 PhysicalPinNumber {
get;
}
/// <summary> /// <summary>
/// Gets the pin's header (physical board) location. /// Gets the pin's header (physical board) location.
/// </summary> /// </summary>
GpioHeader Header { get; } GpioHeader Header {
get;
}
/// <summary> /// <summary>
/// Gets or sets the pin operating mode. /// Gets or sets the pin operating mode.
@ -39,7 +45,9 @@
/// <value> /// <value>
/// The pin mode. /// The pin mode.
/// </value> /// </value>
GpioPinDriveMode PinMode { get; set; } GpioPinDriveMode PinMode {
get; set;
}
/// <summary> /// <summary>
/// This sets or gets the pull-up or pull-down resistor mode on the pin, which should be set as an input. /// This sets or gets the pull-up or pull-down resistor mode on the pin, which should be set as an input.
@ -47,7 +55,9 @@
/// The parameter pud should be; PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v) /// The parameter pud should be; PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v)
/// The internal pull up/down resistors have a value of approximately 50KΩ on the Raspberry Pi. /// The internal pull up/down resistors have a value of approximately 50KΩ on the Raspberry Pi.
/// </summary> /// </summary>
GpioPinResistorPullMode InputPullMode { get; set; } GpioPinResistorPullMode InputPullMode {
get; set;
}
/// <summary> /// <summary>
/// Gets or sets a value indicating whether this <see cref="IGpioPin"/> is value. /// Gets or sets a value indicating whether this <see cref="IGpioPin"/> is value.
@ -55,20 +65,22 @@
/// <value> /// <value>
/// <c>true</c> if value; otherwise, <c>false</c>. /// <c>true</c> if value; otherwise, <c>false</c>.
/// </value> /// </value>
bool Value { get; set; } Boolean Value {
get; set;
}
/// <summary> /// <summary>
/// Reads the digital value on the pin as a boolean value. /// Reads the digital value on the pin as a boolean value.
/// </summary> /// </summary>
/// <returns>The state of the pin.</returns> /// <returns>The state of the pin.</returns>
bool Read(); Boolean Read();
/// <summary> /// <summary>
/// Writes the specified bit value. /// Writes the specified bit value.
/// This method performs a digital write. /// This method performs a digital write.
/// </summary> /// </summary>
/// <param name="value">if set to <c>true</c> [value].</param> /// <param name="value">if set to <c>true</c> [value].</param>
void Write(bool value); void Write(Boolean value);
/// <summary> /// <summary>
/// Writes the specified pin value. /// Writes the specified pin value.
@ -83,7 +95,7 @@
/// <param name="status">status to check.</param> /// <param name="status">status to check.</param>
/// <param name="timeOutMillisecond">timeout to reach status.</param> /// <param name="timeOutMillisecond">timeout to reach status.</param>
/// <returns>true/false.</returns> /// <returns>true/false.</returns>
bool WaitForValue(GpioPinValue status, int timeOutMillisecond); Boolean WaitForValue(GpioPinValue status, Int32 timeOutMillisecond);
/// <summary> /// <summary>
/// Registers the interrupt callback on the pin. Pin mode has to be set to Input. /// Registers the interrupt callback on the pin. Pin mode has to be set to Input.
@ -100,6 +112,6 @@
/// <param name="callback">The callback function. This function is called whenever the interrupt occurs. /// <param name="callback">The callback function. This function is called whenever the interrupt occurs.
/// The function is passed the GPIO, the current level, and the current tick /// The function is passed the GPIO, the current level, and the current tick
/// (The number of microseconds since boot).</param> /// (The number of microseconds since boot).</param>
void RegisterInterruptCallback(EdgeDetection edgeDetection, Action<int, int, uint> callback); void RegisterInterruptCallback(EdgeDetection edgeDetection, Action<Int32, Int32, UInt32> callback);
} }
} }

View File

@ -1,16 +1,17 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interfaces the I2c bus on the Raspberry Pi. /// Interfaces the I2c bus on the Raspberry Pi.
/// </summary> /// </summary>
public interface II2CBus public interface II2CBus {
{
/// <summary> /// <summary>
/// Gets the registered devices as a read only collection. /// Gets the registered devices as a read only collection.
/// </summary> /// </summary>
ReadOnlyCollection<II2CDevice> Devices { get; } ReadOnlyCollection<II2CDevice> Devices {
get;
}
/// <summary> /// <summary>
/// Gets the <see cref="II2CDevice"/> with the specified device identifier. /// Gets the <see cref="II2CDevice"/> with the specified device identifier.
@ -20,20 +21,20 @@
/// </value> /// </value>
/// <param name="deviceId">The device identifier.</param> /// <param name="deviceId">The device identifier.</param>
/// <returns>A reference to an I2C device.</returns> /// <returns>A reference to an I2C device.</returns>
II2CDevice this[int deviceId] { get; } II2CDevice this[Int32 deviceId] { get; }
/// <summary> /// <summary>
/// Gets the device by identifier. /// Gets the device by identifier.
/// </summary> /// </summary>
/// <param name="deviceId">The device identifier.</param> /// <param name="deviceId">The device identifier.</param>
/// <returns>The device reference.</returns> /// <returns>The device reference.</returns>
II2CDevice GetDeviceById(int deviceId); II2CDevice GetDeviceById(Int32 deviceId);
/// <summary> /// <summary>
/// Adds a device to the bus by its Id. If the device is already registered it simply returns the existing device. /// Adds a device to the bus by its Id. If the device is already registered it simply returns the existing device.
/// </summary> /// </summary>
/// <param name="deviceId">The device identifier.</param> /// <param name="deviceId">The device identifier.</param>
/// <returns>The device reference.</returns> /// <returns>The device reference.</returns>
II2CDevice AddDevice(int deviceId); II2CDevice AddDevice(Int32 deviceId);
} }
} }

View File

@ -1,17 +1,19 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interfaces a device on the I2C Bus. /// Interfaces a device on the I2C Bus.
/// </summary> /// </summary>
public interface II2CDevice public interface II2CDevice {
{
/// <summary> /// <summary>
/// Gets the device identifier. /// Gets the device identifier.
/// </summary> /// </summary>
/// <value> /// <value>
/// The device identifier. /// The device identifier.
/// </value> /// </value>
int DeviceId { get; } Int32 DeviceId {
get;
}
/// <summary> /// <summary>
/// Gets the standard POSIX file descriptor. /// Gets the standard POSIX file descriptor.
@ -19,59 +21,61 @@
/// <value> /// <value>
/// The file descriptor. /// The file descriptor.
/// </value> /// </value>
int FileDescriptor { get; } Int32 FileDescriptor {
get;
}
/// <summary> /// <summary>
/// Reads a byte from the specified file descriptor. /// Reads a byte from the specified file descriptor.
/// </summary> /// </summary>
/// <returns>The byte from device.</returns> /// <returns>The byte from device.</returns>
byte Read(); Byte Read();
/// <summary> /// <summary>
/// Reads a buffer of the specified length, one byte at a time. /// Reads a buffer of the specified length, one byte at a time.
/// </summary> /// </summary>
/// <param name="length">The length.</param> /// <param name="length">The length.</param>
/// <returns>The byte array from device.</returns> /// <returns>The byte array from device.</returns>
byte[] Read(int length); Byte[] Read(Int32 length);
/// <summary> /// <summary>
/// Writes a byte of data the specified file descriptor. /// Writes a byte of data the specified file descriptor.
/// </summary> /// </summary>
/// <param name="data">The data.</param> /// <param name="data">The data.</param>
void Write(byte data); void Write(Byte data);
/// <summary> /// <summary>
/// Writes a set of bytes to the specified file descriptor. /// Writes a set of bytes to the specified file descriptor.
/// </summary> /// </summary>
/// <param name="data">The data.</param> /// <param name="data">The data.</param>
void Write(byte[] data); void Write(Byte[] data);
/// <summary> /// <summary>
/// These write an 8 or 16-bit data value into the device register indicated. /// These write an 8 or 16-bit data value into the device register indicated.
/// </summary> /// </summary>
/// <param name="address">The register.</param> /// <param name="address">The register.</param>
/// <param name="data">The data.</param> /// <param name="data">The data.</param>
void WriteAddressByte(int address, byte data); void WriteAddressByte(Int32 address, Byte data);
/// <summary> /// <summary>
/// These write an 8 or 16-bit data value into the device register indicated. /// These write an 8 or 16-bit data value into the device register indicated.
/// </summary> /// </summary>
/// <param name="address">The register.</param> /// <param name="address">The register.</param>
/// <param name="data">The data.</param> /// <param name="data">The data.</param>
void WriteAddressWord(int address, ushort data); void WriteAddressWord(Int32 address, UInt16 data);
/// <summary> /// <summary>
/// These read an 8 or 16-bit value from the device register indicated. /// These read an 8 or 16-bit value from the device register indicated.
/// </summary> /// </summary>
/// <param name="address">The register.</param> /// <param name="address">The register.</param>
/// <returns>The address byte from device.</returns> /// <returns>The address byte from device.</returns>
byte ReadAddressByte(int address); Byte ReadAddressByte(Int32 address);
/// <summary> /// <summary>
/// These read an 8 or 16-bit value from the device register indicated. /// These read an 8 or 16-bit value from the device register indicated.
/// </summary> /// </summary>
/// <param name="address">The register.</param> /// <param name="address">The register.</param>
/// <returns>The address word from device.</returns> /// <returns>The address word from device.</returns>
ushort ReadAddressWord(int address); UInt16 ReadAddressWord(Int32 address);
} }
} }

View File

@ -1,17 +1,19 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interfaces a SPI Bus containing the 2 SPI channels. /// Interfaces a SPI Bus containing the 2 SPI channels.
/// </summary> /// </summary>
public interface ISpiBus public interface ISpiBus {
{
/// <summary> /// <summary>
/// Gets the default frequency. /// Gets the default frequency.
/// </summary> /// </summary>
/// <value> /// <value>
/// The default frequency. /// The default frequency.
/// </value> /// </value>
int DefaultFrequency { get; } Int32 DefaultFrequency {
get;
}
/// <summary> /// <summary>
/// Gets or sets the channel 0 frequency in Hz. /// Gets or sets the channel 0 frequency in Hz.
@ -19,7 +21,9 @@
/// <value> /// <value>
/// The channel0 frequency. /// The channel0 frequency.
/// </value> /// </value>
int Channel0Frequency { get; set; } Int32 Channel0Frequency {
get; set;
}
/// <summary> /// <summary>
/// Gets or sets the channel 1 frequency in Hz. /// Gets or sets the channel 1 frequency in Hz.
@ -27,7 +31,9 @@
/// <value> /// <value>
/// The channel1 frequency. /// The channel1 frequency.
/// </value> /// </value>
int Channel1Frequency { get; set; } Int32 Channel1Frequency {
get; set;
}
/// <summary> /// <summary>
/// Gets the SPI bus on channel 0. /// Gets the SPI bus on channel 0.
@ -35,7 +41,9 @@
/// <value> /// <value>
/// The channel0. /// The channel0.
/// </value> /// </value>
ISpiChannel Channel0 { get; } ISpiChannel Channel0 {
get;
}
/// <summary> /// <summary>
/// Gets the SPI bus on channel 1. /// Gets the SPI bus on channel 1.
@ -43,6 +51,8 @@
/// <value> /// <value>
/// The channel0. /// The channel0.
/// </value> /// </value>
ISpiChannel Channel1 { get; } ISpiChannel Channel1 {
get;
}
} }
} }

View File

@ -1,10 +1,10 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interfaces a SPI buses on the GPIO. /// Interfaces a SPI buses on the GPIO.
/// </summary> /// </summary>
public interface ISpiChannel public interface ISpiChannel {
{
/// <summary> /// <summary>
/// Gets the standard initialization file descriptor. /// Gets the standard initialization file descriptor.
/// anything negative means error. /// anything negative means error.
@ -12,24 +12,30 @@
/// <value> /// <value>
/// The file descriptor. /// The file descriptor.
/// </value> /// </value>
int FileDescriptor { get; } Int32 FileDescriptor {
get;
}
/// <summary> /// <summary>
/// Gets the channel. /// Gets the channel.
/// </summary> /// </summary>
int Channel { get; } Int32 Channel {
get;
}
/// <summary> /// <summary>
/// Gets the frequency. /// Gets the frequency.
/// </summary> /// </summary>
int Frequency { get; } Int32 Frequency {
get;
}
/// <summary> /// <summary>
/// Sends data and simultaneously receives the data in the return buffer. /// Sends data and simultaneously receives the data in the return buffer.
/// </summary> /// </summary>
/// <param name="buffer">The buffer.</param> /// <param name="buffer">The buffer.</param>
/// <returns>The read bytes from the ring-style bus.</returns> /// <returns>The read bytes from the ring-style bus.</returns>
byte[] SendReceive(byte[] buffer); Byte[] SendReceive(Byte[] buffer);
/// <summary> /// <summary>
/// Writes the specified buffer the the underlying FileDescriptor. /// Writes the specified buffer the the underlying FileDescriptor.
@ -38,6 +44,6 @@
/// like sending data over to those long RGB LED strips. /// like sending data over to those long RGB LED strips.
/// </summary> /// </summary>
/// <param name="buffer">The buffer.</param> /// <param name="buffer">The buffer.</param>
void Write(byte[] buffer); void Write(Byte[] buffer);
} }
} }

View File

@ -1,19 +1,19 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
using System;
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interface for system info. /// Interface for system info.
/// </summary> /// </summary>
public interface ISystemInfo public interface ISystemInfo {
{
/// <summary> /// <summary>
/// Gets the board revision (1 or 2). /// Gets the board revision (1 or 2).
/// </summary> /// </summary>
/// <value> /// <value>
/// The board revision. /// The board revision.
/// </value> /// </value>
BoardRevision BoardRevision { get; } BoardRevision BoardRevision {
get;
}
/// <summary> /// <summary>
/// Gets the library version. /// Gets the library version.
@ -21,6 +21,8 @@
/// <value> /// <value>
/// The library version. /// The library version.
/// </value> /// </value>
Version LibraryVersion { get; } Version LibraryVersion {
get;
}
} }
} }

View File

@ -1,12 +1,10 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
using System;
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interface to represent threading methods using interop. /// Interface to represent threading methods using interop.
/// </summary> /// </summary>
public interface IThreading public interface IThreading {
{
/// <summary> /// <summary>
/// Starts a new thread of execution which runs concurrently with your main program. /// Starts a new thread of execution which runs concurrently with your main program.
/// </summary> /// </summary>

View File

@ -1,21 +1,25 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary> /// <summary>
/// Interface for timing methods using interop. /// Interface for timing methods using interop.
/// </summary> /// </summary>
public interface ITiming public interface ITiming {
{
/// <summary> /// <summary>
/// This returns a number representing the number of milliseconds since system boot. /// This returns a number representing the number of milliseconds since system boot.
/// </summary> /// </summary>
/// <returns>The milliseconds since system boot.</returns> /// <returns>The milliseconds since system boot.</returns>
uint Milliseconds { get; } UInt32 Milliseconds {
get;
}
/// <summary> /// <summary>
/// This returns a number representing the number of microseconds since system boot. /// This returns a number representing the number of microseconds since system boot.
/// </summary> /// </summary>
/// <returns>The microseconds since system boot.</returns> /// <returns>The microseconds since system boot.</returns>
uint Microseconds { get; } UInt32 Microseconds {
get;
}
/// <summary> /// <summary>
/// This causes program execution to pause for at least how long milliseconds. /// This causes program execution to pause for at least how long milliseconds.
@ -23,7 +27,7 @@
/// Note that the maximum delay is an unsigned 32-bit integer or approximately 49 days. /// Note that the maximum delay is an unsigned 32-bit integer or approximately 49 days.
/// </summary> /// </summary>
/// <param name="millis">The number of milliseconds to sleep.</param> /// <param name="millis">The number of milliseconds to sleep.</param>
void SleepMilliseconds(uint millis); void SleepMilliseconds(UInt32 millis);
/// <summary> /// <summary>
/// This causes program execution to pause for at least how long microseconds. /// This causes program execution to pause for at least how long microseconds.
@ -35,6 +39,6 @@
/// especially if using threads. /// especially if using threads.
/// </summary> /// </summary>
/// <param name="micros">The number of microseconds to sleep.</param> /// <param name="micros">The number of microseconds to sleep.</param>
void SleepMicroseconds(uint micros); void SleepMicroseconds(UInt32 micros);
} }
} }

View File

@ -1,36 +1,29 @@
namespace Unosquare.RaspberryIO.Abstractions.Native
{
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Unosquare.RaspberryIO.Abstractions.Native {
/// <summary> /// <summary>
/// Represents a low-level exception, typically thrown when return codes from a /// Represents a low-level exception, typically thrown when return codes from a
/// low-level operation is non-zero or in some cases when it is less than zero. /// low-level operation is non-zero or in some cases when it is less than zero.
/// </summary> /// </summary>
/// <seealso cref="Exception" /> /// <seealso cref="Exception" />
public class HardwareException : Exception public class HardwareException : Exception {
{
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="HardwareException"/> class. /// Initializes a new instance of the <see cref="HardwareException"/> class.
/// </summary> /// </summary>
/// <param name="errorCode">The error code.</param> /// <param name="errorCode">The error code.</param>
/// <param name="component">The component.</param> /// <param name="component">The component.</param>
public HardwareException(int errorCode, string component) public HardwareException(Int32 errorCode, String component) : base($"A hardware exception occurred. Error Code: {errorCode}") {
: base($"A hardware exception occurred. Error Code: {errorCode}") this.ExtendedMessage = null;
{
ExtendedMessage = null;
try try {
{ this.ExtendedMessage = Standard.Strerror(errorCode);
ExtendedMessage = Standard.Strerror(errorCode); } catch {
}
catch
{
// Ignore // Ignore
} }
ErrorCode = errorCode; this.ErrorCode = errorCode;
Component = component; this.Component = component;
} }
/// <summary> /// <summary>
@ -39,7 +32,9 @@ namespace Unosquare.RaspberryIO.Abstractions.Native
/// <value> /// <value>
/// The error code. /// The error code.
/// </value> /// </value>
public int ErrorCode { get; } public Int32 ErrorCode {
get;
}
/// <summary> /// <summary>
/// Gets the component. /// Gets the component.
@ -47,7 +42,9 @@ namespace Unosquare.RaspberryIO.Abstractions.Native
/// <value> /// <value>
/// The component. /// The component.
/// </value> /// </value>
public string Component { get; } public String Component {
get;
}
/// <summary> /// <summary>
/// Gets the extended message (could be null). /// Gets the extended message (could be null).
@ -55,7 +52,9 @@ namespace Unosquare.RaspberryIO.Abstractions.Native
/// <value> /// <value>
/// The extended message. /// The extended message.
/// </value> /// </value>
public string? ExtendedMessage { get; } public String? ExtendedMessage {
get;
}
/// <summary> /// <summary>
/// Throws a new instance of a hardware error by retrieving the last error number (errno). /// Throws a new instance of a hardware error by retrieving the last error number (errno).
@ -63,9 +62,9 @@ namespace Unosquare.RaspberryIO.Abstractions.Native
/// <param name="className">Name of the class.</param> /// <param name="className">Name of the class.</param>
/// <param name="methodName">Name of the method.</param> /// <param name="methodName">Name of the method.</param>
/// <exception cref="HardwareException">When an error thrown by an API call occurs.</exception> /// <exception cref="HardwareException">When an error thrown by an API call occurs.</exception>
public static void Throw(string className, string methodName) => throw new HardwareException(Marshal.GetLastWin32Error(), $"{className}.{methodName}"); public static void Throw(String className, String methodName) => throw new HardwareException(Marshal.GetLastWin32Error(), $"{className}.{methodName}");
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => $"{nameof(HardwareException)}{(string.IsNullOrWhiteSpace(Component) ? string.Empty : $" on {Component}")}: ({ErrorCode}) - {Message}"; public override String ToString() => $"{nameof(HardwareException)}{(String.IsNullOrWhiteSpace(this.Component) ? String.Empty : $" on {this.Component}")}: ({this.ErrorCode}) - {this.Message}";
} }
} }

View File

@ -1,15 +1,13 @@
namespace Unosquare.RaspberryIO.Abstractions.Native using System;
{
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
namespace Unosquare.RaspberryIO.Abstractions.Native {
/// <summary> /// <summary>
/// Provides standard 'libc' calls using platform-invoke. /// Provides standard 'libc' calls using platform-invoke.
/// </summary> /// </summary>
public static class Standard public static class Standard {
{ internal const String LibCLibrary = "libc";
internal const string LibCLibrary = "libc";
#region LibC Calls #region LibC Calls
@ -18,27 +16,25 @@
/// </summary> /// </summary>
/// <param name="error">The error.</param> /// <param name="error">The error.</param>
/// <returns>The error string.</returns> /// <returns>The error string.</returns>
public static string Strerror(int error) public static String? Strerror(Int32 error) {
{ if(Type.GetType("Mono.Runtime") == null) {
if (Type.GetType("Mono.Runtime") == null) return Marshal.PtrToStringAnsi(StrError(error)); return Marshal.PtrToStringAnsi(StrError(error));
try
{
var buffer = new StringBuilder(256);
var result = Strerror(error, buffer, (ulong)buffer.Capacity);
return (result != -1) ? buffer.ToString() : null;
} }
catch (Exception)
{ try {
StringBuilder buffer = new StringBuilder(256);
Int32 result = Strerror(error, buffer, (UInt64)buffer.Capacity);
return (result != -1) ? buffer.ToString() : null;
} catch(Exception) {
return null; return null;
} }
} }
[DllImport(LibCLibrary, EntryPoint = "strerror", SetLastError = true)] [DllImport(LibCLibrary, EntryPoint = "strerror", SetLastError = true)]
private static extern IntPtr StrError(int errnum); private static extern IntPtr StrError(Int32 errnum);
[DllImport("MonoPosixHelper", EntryPoint = "Mono_Posix_Syscall_strerror_r", SetLastError = true)] [DllImport("MonoPosixHelper", EntryPoint = "Mono_Posix_Syscall_strerror_r", SetLastError = true)]
private static extern int Strerror(int error, [Out] StringBuilder buffer, ulong length); private static extern Int32 Strerror(Int32 error, [Out] StringBuilder buffer, UInt64 length);
#endregion #endregion
} }

View File

@ -16,6 +16,7 @@ This library enables developers to use the various Raspberry Pi's hardware modul
<PackageLicenseUrl>https://raw.githubusercontent.com/unosquare/raspberryio/master/LICENSE</PackageLicenseUrl> <PackageLicenseUrl>https://raw.githubusercontent.com/unosquare/raspberryio/master/LICENSE</PackageLicenseUrl>
<PackageTags>Raspberry Pi GPIO Camera SPI I2C Embedded IoT Mono C# .NET</PackageTags> <PackageTags>Raspberry Pi GPIO Camera SPI I2C Embedded IoT Mono C# .NET</PackageTags>
<LangVersion>8.0</LangVersion> <LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
</Project> </Project>