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,28 +1,20 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary>
/// Represents Definitions for GPIO information.
/// </summary>
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 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*/ };
/// <summary> /// <summary>
/// Represents Definitions for GPIO information. /// BCMs to physical pin number.
/// </summary> /// </summary>
public static class Definitions /// <param name="rev">The rev.</param>
{ /// <param name="bcmPin">The BCM pin.</param>
private static readonly int[] GpioToPhysR1 = /// <returns>The physical pin number.</returns>
{ public static Int32 BcmToPhysicalPinNumber(BoardRevision rev, BcmPin bcmPin) => rev == BoardRevision.Rev1 ? GpioToPhysR1[(Int32)bcmPin] : GpioToPhysR2[(Int32)bcmPin];
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 =
{
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>
/// BCMs to physical pin number.
/// </summary>
/// <param name="rev">The rev.</param>
/// <param name="bcmPin">The BCM pin.</param>
/// <returns>The physical pin number.</returns>
public static int BcmToPhysicalPinNumber(BoardRevision rev, BcmPin bcmPin) =>
rev == BoardRevision.Rev1 ? GpioToPhysR1[(int)bcmPin] : GpioToPhysR2[(int)bcmPin];
}
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,11 @@
namespace Unosquare.RaspberryIO.Abstractions namespace Unosquare.RaspberryIO.Abstractions {
{ /// <summary>
/// Interface for bootstrapping an <see cref="Abstractions"/> implementation.
/// </summary>
public interface IBootstrap {
/// <summary> /// <summary>
/// Interface for bootstrapping an <see cref="Abstractions"/> implementation. /// Bootstraps an <see cref="Abstractions"/> implementation.
/// </summary> /// </summary>
public interface IBootstrap void Bootstrap();
{ }
/// <summary>
/// Bootstraps an <see cref="Abstractions"/> implementation.
/// </summary>
void Bootstrap();
}
} }

View File

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

View File

@ -1,105 +1,117 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
using System; namespace Unosquare.RaspberryIO.Abstractions {
/// <summary>
/// Interface for GPIO Pin on a RaspberryPi board.
/// </summary>
public interface IGpioPin {
/// <summary>
/// Gets the <see cref="Abstractions.BcmPin"/>.
/// </summary>
/// <value>
/// The pin number.
/// </value>
BcmPin BcmPin {
get;
}
/// <summary> /// <summary>
/// Interface for GPIO Pin on a RaspberryPi board. /// Gets the BCM chip (hardware) pin number.
/// </summary> /// </summary>
public interface IGpioPin /// <value>
{ /// The pin number.
/// <summary> /// </value>
/// Gets the <see cref="Abstractions.BcmPin"/>. Int32 BcmPinNumber {
/// </summary> get;
/// <value>
/// The pin number.
/// </value>
BcmPin BcmPin { get; }
/// <summary>
/// Gets the BCM chip (hardware) pin number.
/// </summary>
/// <value>
/// The pin number.
/// </value>
int BcmPinNumber { get; }
/// <summary>
/// Gets the physical (header) pin number.
/// </summary>
int PhysicalPinNumber { get; }
/// <summary>
/// Gets the pin's header (physical board) location.
/// </summary>
GpioHeader Header { get; }
/// <summary>
/// Gets or sets the pin operating mode.
/// </summary>
/// <value>
/// The pin mode.
/// </value>
GpioPinDriveMode PinMode { get; set; }
/// <summary>
/// This sets or gets the pull-up or pull-down resistor mode on the pin, which should be set as an input.
/// Unlike the Arduino, the BCM2835 has both pull-up an down internal resistors.
/// 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.
/// </summary>
GpioPinResistorPullMode InputPullMode { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="IGpioPin"/> is value.
/// </summary>
/// <value>
/// <c>true</c> if value; otherwise, <c>false</c>.
/// </value>
bool Value { get; set; }
/// <summary>
/// Reads the digital value on the pin as a boolean value.
/// </summary>
/// <returns>The state of the pin.</returns>
bool Read();
/// <summary>
/// Writes the specified bit value.
/// This method performs a digital write.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
void Write(bool value);
/// <summary>
/// Writes the specified pin value.
/// This method performs a digital write.
/// </summary>
/// <param name="value">The value.</param>
void Write(GpioPinValue value);
/// <summary>
/// Wait for specific pin status.
/// </summary>
/// <param name="status">status to check.</param>
/// <param name="timeOutMillisecond">timeout to reach status.</param>
/// <returns>true/false.</returns>
bool WaitForValue(GpioPinValue status, int timeOutMillisecond);
/// <summary>
/// Registers the interrupt callback on the pin. Pin mode has to be set to Input.
/// </summary>
/// <param name="edgeDetection">The edge detection.</param>
/// <param name="callback">The callback function. This function is called whenever
/// the interrupt occurs.</param>
void RegisterInterruptCallback(EdgeDetection edgeDetection, Action callback);
/// <summary>
/// Registers the interrupt callback on the pin. Pin mode has to be set to Input.
/// </summary>
/// <param name="edgeDetection">The edge detection.</param>
/// <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 number of microseconds since boot).</param>
void RegisterInterruptCallback(EdgeDetection edgeDetection, Action<int, int, uint> callback);
} }
/// <summary>
/// Gets the physical (header) pin number.
/// </summary>
Int32 PhysicalPinNumber {
get;
}
/// <summary>
/// Gets the pin's header (physical board) location.
/// </summary>
GpioHeader Header {
get;
}
/// <summary>
/// Gets or sets the pin operating mode.
/// </summary>
/// <value>
/// The pin mode.
/// </value>
GpioPinDriveMode PinMode {
get; set;
}
/// <summary>
/// This sets or gets the pull-up or pull-down resistor mode on the pin, which should be set as an input.
/// Unlike the Arduino, the BCM2835 has both pull-up an down internal resistors.
/// 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.
/// </summary>
GpioPinResistorPullMode InputPullMode {
get; set;
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="IGpioPin"/> is value.
/// </summary>
/// <value>
/// <c>true</c> if value; otherwise, <c>false</c>.
/// </value>
Boolean Value {
get; set;
}
/// <summary>
/// Reads the digital value on the pin as a boolean value.
/// </summary>
/// <returns>The state of the pin.</returns>
Boolean Read();
/// <summary>
/// Writes the specified bit value.
/// This method performs a digital write.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
void Write(Boolean value);
/// <summary>
/// Writes the specified pin value.
/// This method performs a digital write.
/// </summary>
/// <param name="value">The value.</param>
void Write(GpioPinValue value);
/// <summary>
/// Wait for specific pin status.
/// </summary>
/// <param name="status">status to check.</param>
/// <param name="timeOutMillisecond">timeout to reach status.</param>
/// <returns>true/false.</returns>
Boolean WaitForValue(GpioPinValue status, Int32 timeOutMillisecond);
/// <summary>
/// Registers the interrupt callback on the pin. Pin mode has to be set to Input.
/// </summary>
/// <param name="edgeDetection">The edge detection.</param>
/// <param name="callback">The callback function. This function is called whenever
/// the interrupt occurs.</param>
void RegisterInterruptCallback(EdgeDetection edgeDetection, Action callback);
/// <summary>
/// Registers the interrupt callback on the pin. Pin mode has to be set to Input.
/// </summary>
/// <param name="edgeDetection">The edge detection.</param>
/// <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 number of microseconds since boot).</param>
void RegisterInterruptCallback(EdgeDetection edgeDetection, Action<Int32, Int32, UInt32> callback);
}
} }

View File

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

View File

@ -1,77 +1,81 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary>
/// Interfaces a device on the I2C Bus.
/// </summary>
public interface II2CDevice {
/// <summary> /// <summary>
/// Interfaces a device on the I2C Bus. /// Gets the device identifier.
/// </summary> /// </summary>
public interface II2CDevice /// <value>
{ /// The device identifier.
/// <summary> /// </value>
/// Gets the device identifier. Int32 DeviceId {
/// </summary> get;
/// <value>
/// The device identifier.
/// </value>
int DeviceId { get; }
/// <summary>
/// Gets the standard POSIX file descriptor.
/// </summary>
/// <value>
/// The file descriptor.
/// </value>
int FileDescriptor { get; }
/// <summary>
/// Reads a byte from the specified file descriptor.
/// </summary>
/// <returns>The byte from device.</returns>
byte 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>
byte[] Read(int length);
/// <summary>
/// Writes a byte of data the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
void Write(byte data);
/// <summary>
/// Writes a set of bytes to the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
void Write(byte[] 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>
void WriteAddressByte(int address, byte 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>
void WriteAddressWord(int address, ushort data);
/// <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>
byte ReadAddressByte(int address);
/// <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>
ushort ReadAddressWord(int address);
} }
/// <summary>
/// Gets the standard POSIX file descriptor.
/// </summary>
/// <value>
/// The file descriptor.
/// </value>
Int32 FileDescriptor {
get;
}
/// <summary>
/// Reads a byte from the specified file descriptor.
/// </summary>
/// <returns>The byte from device.</returns>
Byte 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>
Byte[] Read(Int32 length);
/// <summary>
/// Writes a byte of data the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
void Write(Byte data);
/// <summary>
/// Writes a set of bytes to the specified file descriptor.
/// </summary>
/// <param name="data">The data.</param>
void Write(Byte[] 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>
void WriteAddressByte(Int32 address, Byte 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>
void WriteAddressWord(Int32 address, UInt16 data);
/// <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>
Byte ReadAddressByte(Int32 address);
/// <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>
UInt16 ReadAddressWord(Int32 address);
}
} }

View File

@ -1,48 +1,58 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary>
/// Interfaces a SPI Bus containing the 2 SPI channels.
/// </summary>
public interface ISpiBus {
/// <summary> /// <summary>
/// Interfaces a SPI Bus containing the 2 SPI channels. /// Gets the default frequency.
/// </summary> /// </summary>
public interface ISpiBus /// <value>
{ /// The default frequency.
/// <summary> /// </value>
/// Gets the default frequency. Int32 DefaultFrequency {
/// </summary> get;
/// <value>
/// The default frequency.
/// </value>
int DefaultFrequency { get; }
/// <summary>
/// Gets or sets the channel 0 frequency in Hz.
/// </summary>
/// <value>
/// The channel0 frequency.
/// </value>
int Channel0Frequency { get; set; }
/// <summary>
/// Gets or sets the channel 1 frequency in Hz.
/// </summary>
/// <value>
/// The channel1 frequency.
/// </value>
int Channel1Frequency { get; set; }
/// <summary>
/// Gets the SPI bus on channel 0.
/// </summary>
/// <value>
/// The channel0.
/// </value>
ISpiChannel Channel0 { get; }
/// <summary>
/// Gets the SPI bus on channel 1.
/// </summary>
/// <value>
/// The channel0.
/// </value>
ISpiChannel Channel1 { get; }
} }
/// <summary>
/// Gets or sets the channel 0 frequency in Hz.
/// </summary>
/// <value>
/// The channel0 frequency.
/// </value>
Int32 Channel0Frequency {
get; set;
}
/// <summary>
/// Gets or sets the channel 1 frequency in Hz.
/// </summary>
/// <value>
/// The channel1 frequency.
/// </value>
Int32 Channel1Frequency {
get; set;
}
/// <summary>
/// Gets the SPI bus on channel 0.
/// </summary>
/// <value>
/// The channel0.
/// </value>
ISpiChannel Channel0 {
get;
}
/// <summary>
/// Gets the SPI bus on channel 1.
/// </summary>
/// <value>
/// The channel0.
/// </value>
ISpiChannel Channel1 {
get;
}
}
} }

View File

@ -1,43 +1,49 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary>
/// Interfaces a SPI buses on the GPIO.
/// </summary>
public interface ISpiChannel {
/// <summary> /// <summary>
/// Interfaces a SPI buses on the GPIO. /// Gets the standard initialization file descriptor.
/// anything negative means error.
/// </summary> /// </summary>
public interface ISpiChannel /// <value>
{ /// The file descriptor.
/// <summary> /// </value>
/// Gets the standard initialization file descriptor. Int32 FileDescriptor {
/// anything negative means error. get;
/// </summary>
/// <value>
/// The file descriptor.
/// </value>
int FileDescriptor { get; }
/// <summary>
/// Gets the channel.
/// </summary>
int Channel { get; }
/// <summary>
/// Gets the frequency.
/// </summary>
int Frequency { get; }
/// <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>
byte[] SendReceive(byte[] buffer);
/// <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>
void Write(byte[] buffer);
} }
/// <summary>
/// Gets the channel.
/// </summary>
Int32 Channel {
get;
}
/// <summary>
/// Gets the frequency.
/// </summary>
Int32 Frequency {
get;
}
/// <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>
Byte[] SendReceive(Byte[] buffer);
/// <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>
void Write(Byte[] buffer);
}
} }

View File

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

View File

@ -1,30 +1,28 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
using System; namespace Unosquare.RaspberryIO.Abstractions {
/// <summary>
/// Interface to represent threading methods using interop.
/// </summary>
public interface IThreading {
/// <summary>
/// Starts a new thread of execution which runs concurrently with your main program.
/// </summary>
/// <param name="worker">The thread routine.</param>
void StartThread(Action worker);
/// <summary> /// <summary>
/// Interface to represent threading methods using interop. /// Starts a new thread of execution which runs concurrently with your main program.
/// </summary> /// </summary>
public interface IThreading /// <param name="worker">The thread routine.</param>
{ /// <param name="userData">A pointer to the user data.</param>
/// <summary> /// <returns>A pointer to the new thread.</returns>
/// Starts a new thread of execution which runs concurrently with your main program. UIntPtr StartThreadEx(Action<UIntPtr> worker, UIntPtr userData);
/// </summary>
/// <param name="worker">The thread routine.</param>
void StartThread(Action worker);
/// <summary> /// <summary>
/// Starts a new thread of execution which runs concurrently with your main program. /// Stops the thread pointed at by handle.
/// </summary> /// </summary>
/// <param name="worker">The thread routine.</param> /// <param name="handle">A thread pointer returned by <see cref="StartThreadEx(Action{UIntPtr}, UIntPtr)"/>.</param>
/// <param name="userData">A pointer to the user data.</param> void StopThreadEx(UIntPtr handle);
/// <returns>A pointer to the new thread.</returns> }
UIntPtr StartThreadEx(Action<UIntPtr> worker, UIntPtr userData);
/// <summary>
/// Stops the thread pointed at by handle.
/// </summary>
/// <param name="handle">A thread pointer returned by <see cref="StartThreadEx(Action{UIntPtr}, UIntPtr)"/>.</param>
void StopThreadEx(UIntPtr handle);
}
} }

View File

@ -1,40 +1,44 @@
namespace Unosquare.RaspberryIO.Abstractions using System;
{
namespace Unosquare.RaspberryIO.Abstractions {
/// <summary>
/// Interface for timing methods using interop.
/// </summary>
public interface ITiming {
/// <summary> /// <summary>
/// Interface for timing methods using interop. /// This returns a number representing the number of milliseconds since system boot.
/// </summary> /// </summary>
public interface ITiming /// <returns>The milliseconds since system boot.</returns>
{ UInt32 Milliseconds {
/// <summary> get;
/// This returns a number representing the number of milliseconds since system boot.
/// </summary>
/// <returns>The milliseconds since system boot.</returns>
uint Milliseconds { get; }
/// <summary>
/// This returns a number representing the number of microseconds since system boot.
/// </summary>
/// <returns>The microseconds since system boot.</returns>
uint Microseconds { get; }
/// <summary>
/// This causes program execution to pause for at least how long milliseconds.
/// Due to the multi-tasking nature of Linux it could be longer.
/// Note that the maximum delay is an unsigned 32-bit integer or approximately 49 days.
/// </summary>
/// <param name="millis">The number of milliseconds to sleep.</param>
void SleepMilliseconds(uint millis);
/// <summary>
/// This causes program execution to pause for at least how long microseconds.
/// Due to the multi-tasking nature of Linux it could be longer.
/// Note that the maximum delay is an unsigned 32-bit integer microseconds or approximately 71 minutes.
/// Delays under 100 microseconds are timed using a hard-coded loop continually polling the system time,
/// Delays over 100 microseconds are done using the system nanosleep() function
/// You may need to consider the implications of very short delays on the overall performance of the system,
/// especially if using threads.
/// </summary>
/// <param name="micros">The number of microseconds to sleep.</param>
void SleepMicroseconds(uint micros);
} }
/// <summary>
/// This returns a number representing the number of microseconds since system boot.
/// </summary>
/// <returns>The microseconds since system boot.</returns>
UInt32 Microseconds {
get;
}
/// <summary>
/// This causes program execution to pause for at least how long milliseconds.
/// Due to the multi-tasking nature of Linux it could be longer.
/// Note that the maximum delay is an unsigned 32-bit integer or approximately 49 days.
/// </summary>
/// <param name="millis">The number of milliseconds to sleep.</param>
void SleepMilliseconds(UInt32 millis);
/// <summary>
/// This causes program execution to pause for at least how long microseconds.
/// Due to the multi-tasking nature of Linux it could be longer.
/// Note that the maximum delay is an unsigned 32-bit integer microseconds or approximately 71 minutes.
/// Delays under 100 microseconds are timed using a hard-coded loop continually polling the system time,
/// Delays over 100 microseconds are done using the system nanosleep() function
/// You may need to consider the implications of very short delays on the overall performance of the system,
/// especially if using threads.
/// </summary>
/// <param name="micros">The number of microseconds to sleep.</param>
void SleepMicroseconds(UInt32 micros);
}
} }

View File

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

View File

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