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> /// <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="bcmPinNumber">The BCM pin number.</param>
/// Gets the <see cref="IGpioPin"/> with the specified BCM pin. /// <returns>A reference to the GPIO pin.</returns>
/// </summary> IGpioPin this[Int32 bcmPinNumber] { get; }
/// <value>
/// The <see cref="IGpioPin"/>. /// <summary>
/// </value> /// Gets the <see cref="IGpioPin"/> with the specified BCM pin.
/// <param name="bcmPinNumber">The BCM pin number.</param> /// </summary>
/// <returns>A reference to the GPIO pin.</returns> /// <value>
IGpioPin this[int bcmPinNumber] { get; } /// 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"/>. /// <summary>
/// </value> /// Gets the <see cref="IGpioPin"/> with the specified pin number.
/// <param name="bcmPin">The BCM pin.</param> /// </summary>
/// <returns>A reference to the GPIO pin.</returns> /// <value>
IGpioPin this[BcmPin bcmPin] { get; } /// The <see cref="IGpioPin"/>.
/// </value>
/// <summary> /// <param name="pinNumber">The pin number in header P1.</param>
/// Gets the <see cref="IGpioPin"/> with the specified pin number. /// <returns>A reference to the GPIO pin.</returns>
/// </summary> IGpioPin this[P1 pinNumber] { get; }
/// <value>
/// The <see cref="IGpioPin"/>. /// <summary>
/// </value> /// Gets the <see cref="IGpioPin"/> with the specified pin number.
/// <param name="pinNumber">The pin number in header P1.</param> /// </summary>
/// <returns>A reference to the GPIO pin.</returns> /// <value>
IGpioPin this[P1 pinNumber] { get; } /// The <see cref="IGpioPin"/>.
/// </value>
/// <summary> /// <param name="pinNumber">The pin number in header P5.</param>
/// Gets the <see cref="IGpioPin"/> with the specified pin number. /// <returns>A reference to the GPIO pin.</returns>
/// </summary> IGpioPin this[P5 pinNumber] { get; }
/// <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> /// <summary>
/// Interface for GPIO Pin on a RaspberryPi board. /// Gets the <see cref="Abstractions.BcmPin"/>.
/// </summary> /// </summary>
public interface IGpioPin /// <value>
{ /// The pin number.
/// <summary> /// </value>
/// Gets the <see cref="Abstractions.BcmPin"/>. BcmPin BcmPin {
/// </summary> get;
/// <value> }
/// The pin number.
/// </value> /// <summary>
BcmPin BcmPin { get; } /// Gets the BCM chip (hardware) pin number.
/// </summary>
/// <summary> /// <value>
/// Gets the BCM chip (hardware) pin number. /// The pin number.
/// </summary> /// </value>
/// <value> Int32 BcmPinNumber {
/// The pin number. get;
/// </value> }
int BcmPinNumber { get; }
/// <summary>
/// <summary> /// Gets the physical (header) pin number.
/// Gets the physical (header) pin number. /// </summary>
/// </summary> Int32 PhysicalPinNumber {
int PhysicalPinNumber { get; } get;
}
/// <summary>
/// Gets the pin's header (physical board) location. /// <summary>
/// </summary> /// Gets the pin's header (physical board) location.
GpioHeader Header { get; } /// </summary>
GpioHeader Header {
/// <summary> get;
/// Gets or sets the pin operating mode. }
/// </summary>
/// <value> /// <summary>
/// The pin mode. /// Gets or sets the pin operating mode.
/// </value> /// </summary>
GpioPinDriveMode PinMode { get; set; } /// <value>
/// The pin mode.
/// <summary> /// </value>
/// This sets or gets the pull-up or pull-down resistor mode on the pin, which should be set as an input. GpioPinDriveMode PinMode {
/// Unlike the Arduino, the BCM2835 has both pull-up an down internal resistors. get; set;
/// 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> /// <summary>
GpioPinResistorPullMode InputPullMode { get; set; } /// 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.
/// <summary> /// The parameter pud should be; PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v)
/// Gets or sets a value indicating whether this <see cref="IGpioPin"/> is value. /// The internal pull up/down resistors have a value of approximately 50KΩ on the Raspberry Pi.
/// </summary> /// </summary>
/// <value> GpioPinResistorPullMode InputPullMode {
/// <c>true</c> if value; otherwise, <c>false</c>. get; set;
/// </value> }
bool Value { get; set; }
/// <summary>
/// <summary> /// Gets or sets a value indicating whether this <see cref="IGpioPin"/> is value.
/// Reads the digital value on the pin as a boolean value. /// </summary>
/// </summary> /// <value>
/// <returns>The state of the pin.</returns> /// <c>true</c> if value; otherwise, <c>false</c>.
bool Read(); /// </value>
Boolean Value {
/// <summary> get; set;
/// Writes the specified bit value. }
/// This method performs a digital write.
/// </summary> /// <summary>
/// <param name="value">if set to <c>true</c> [value].</param> /// Reads the digital value on the pin as a boolean value.
void Write(bool value); /// </summary>
/// <returns>The state of the pin.</returns>
/// <summary> Boolean Read();
/// Writes the specified pin value.
/// This method performs a digital write. /// <summary>
/// </summary> /// Writes the specified bit value.
/// <param name="value">The value.</param> /// This method performs a digital write.
void Write(GpioPinValue value); /// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <summary> void Write(Boolean value);
/// Wait for specific pin status.
/// </summary> /// <summary>
/// <param name="status">status to check.</param> /// Writes the specified pin value.
/// <param name="timeOutMillisecond">timeout to reach status.</param> /// This method performs a digital write.
/// <returns>true/false.</returns> /// </summary>
bool WaitForValue(GpioPinValue status, int timeOutMillisecond); /// <param name="value">The value.</param>
void Write(GpioPinValue value);
/// <summary>
/// Registers the interrupt callback on the pin. Pin mode has to be set to Input. /// <summary>
/// </summary> /// Wait for specific pin status.
/// <param name="edgeDetection">The edge detection.</param> /// </summary>
/// <param name="callback">The callback function. This function is called whenever /// <param name="status">status to check.</param>
/// the interrupt occurs.</param> /// <param name="timeOutMillisecond">timeout to reach status.</param>
void RegisterInterruptCallback(EdgeDetection edgeDetection, Action callback); /// <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>
/// </summary> /// Registers the interrupt callback on the pin. Pin mode has to be set to Input.
/// <param name="edgeDetection">The edge detection.</param> /// </summary>
/// <param name="callback">The callback function. This function is called whenever the interrupt occurs. /// <param name="edgeDetection">The edge detection.</param>
/// The function is passed the GPIO, the current level, and the current tick /// <param name="callback">The callback function. This function is called whenever
/// (The number of microseconds since boot).</param> /// the interrupt occurs.</param>
void RegisterInterruptCallback(EdgeDetection edgeDetection, Action<int, int, uint> callback); 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> /// <summary>
/// Interfaces the I2c bus on the Raspberry Pi. /// Gets the registered devices as a read only collection.
/// </summary> /// </summary>
public interface II2CBus ReadOnlyCollection<II2CDevice> Devices {
{ get;
/// <summary> }
/// Gets the registered devices as a read only collection.
/// </summary> /// <summary>
ReadOnlyCollection<II2CDevice> Devices { get; } /// Gets the <see cref="II2CDevice"/> with the specified device identifier.
/// </summary>
/// <summary> /// <value>
/// Gets the <see cref="II2CDevice"/> with the specified device identifier. /// The <see cref="II2CDevice"/>.
/// </summary> /// </value>
/// <value> /// <param name="deviceId">The device identifier.</param>
/// The <see cref="II2CDevice"/>. /// <returns>A reference to an I2C device.</returns>
/// </value> II2CDevice this[Int32 deviceId] { get; }
/// <param name="deviceId">The device identifier.</param>
/// <returns>A reference to an I2C device.</returns> /// <summary>
II2CDevice this[int deviceId] { get; } /// Gets the device by identifier.
/// </summary>
/// <summary> /// <param name="deviceId">The device identifier.</param>
/// Gets the device by identifier. /// <returns>The device reference.</returns>
/// </summary> II2CDevice GetDeviceById(Int32 deviceId);
/// <param name="deviceId">The device identifier.</param>
/// <returns>The device reference.</returns> /// <summary>
II2CDevice GetDeviceById(int deviceId); /// 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>
/// Adds a device to the bus by its Id. If the device is already registered it simply returns the existing device. /// <returns>The device reference.</returns>
/// </summary> II2CDevice AddDevice(Int32 deviceId);
/// <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> /// <summary>
int DeviceId { get; } /// Gets the standard POSIX file descriptor.
/// </summary>
/// <summary> /// <value>
/// Gets the standard POSIX file descriptor. /// The file descriptor.
/// </summary> /// </value>
/// <value> Int32 FileDescriptor {
/// The file descriptor. get;
/// </value> }
int 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(Int32 length);
byte[] Read(int 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(Int32 address, Byte data);
void WriteAddressByte(int 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(Int32 address, UInt16 data);
void WriteAddressWord(int address, ushort 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(Int32 address);
byte ReadAddressByte(int 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> UInt16 ReadAddressWord(Int32 address);
ushort ReadAddressWord(int 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> /// <summary>
int DefaultFrequency { get; } /// Gets or sets the channel 0 frequency in Hz.
/// </summary>
/// <summary> /// <value>
/// Gets or sets the channel 0 frequency in Hz. /// The channel0 frequency.
/// </summary> /// </value>
/// <value> Int32 Channel0Frequency {
/// The channel0 frequency. get; set;
/// </value> }
int Channel0Frequency { get; set; }
/// <summary>
/// <summary> /// Gets or sets the channel 1 frequency in Hz.
/// Gets or sets the channel 1 frequency in Hz. /// </summary>
/// </summary> /// <value>
/// <value> /// The channel1 frequency.
/// The channel1 frequency. /// </value>
/// </value> Int32 Channel1Frequency {
int Channel1Frequency { get; set; } get; set;
}
/// <summary>
/// Gets the SPI bus on channel 0. /// <summary>
/// </summary> /// Gets the SPI bus on channel 0.
/// <value> /// </summary>
/// The channel0. /// <value>
/// </value> /// The channel0.
ISpiChannel Channel0 { get; } /// </value>
ISpiChannel Channel0 {
/// <summary> get;
/// Gets the SPI bus on channel 1. }
/// </summary>
/// <value> /// <summary>
/// The channel0. /// Gets the SPI bus on channel 1.
/// </value> /// </summary>
ISpiChannel Channel1 { get; } /// <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. /// <summary>
/// </value> /// Gets the channel.
int FileDescriptor { get; } /// </summary>
Int32 Channel {
/// <summary> get;
/// Gets the channel. }
/// </summary>
int Channel { get; } /// <summary>
/// Gets the frequency.
/// <summary> /// </summary>
/// Gets the frequency. Int32 Frequency {
/// </summary> get;
int 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.
/// Do not use this method if you expect data back. /// Do not use this method if you expect data back.
/// This method is efficient if used in a fire-and-forget scenario /// This method is efficient if used in a fire-and-forget scenario
/// 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,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> /// <summary>
/// Interface for system info. /// Gets the board revision (1 or 2).
/// </summary> /// </summary>
public interface ISystemInfo /// <value>
{ /// The board revision.
/// <summary> /// </value>
/// Gets the board revision (1 or 2). BoardRevision BoardRevision {
/// </summary> get;
/// <value> }
/// The board revision.
/// </value> /// <summary>
BoardRevision BoardRevision { get; } /// Gets the library version.
/// </summary>
/// <summary> /// <value>
/// Gets the library version. /// The library version.
/// </summary> /// </value>
/// <value> Version LibraryVersion {
/// The library version. get;
/// </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> /// <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>
{ void StartThread(Action worker);
/// <summary>
/// Starts a new thread of execution which runs concurrently with your main program. /// <summary>
/// </summary> /// Starts a new thread of execution which runs concurrently with your main program.
/// <param name="worker">The thread routine.</param> /// </summary>
void StartThread(Action worker); /// <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> /// <summary>
/// <param name="userData">A pointer to the user data.</param> /// Stops the thread pointed at by handle.
/// <returns>A pointer to the new thread.</returns> /// </summary>
UIntPtr StartThreadEx(Action<UIntPtr> worker, UIntPtr userData); /// <param name="handle">A thread pointer returned by <see cref="StartThreadEx(Action{UIntPtr}, UIntPtr)"/>.</param>
void StopThreadEx(UIntPtr handle);
/// <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> /// <summary>
uint Milliseconds { get; } /// This returns a number representing the number of microseconds since system boot.
/// </summary>
/// <summary> /// <returns>The microseconds since system boot.</returns>
/// This returns a number representing the number of microseconds since system boot. UInt32 Microseconds {
/// </summary> get;
/// <returns>The microseconds since system boot.</returns> }
uint 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. /// Due to the multi-tasking nature of Linux it could be longer.
/// 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.
/// 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(UInt32 millis);
void SleepMilliseconds(uint 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. /// Due to the multi-tasking nature of Linux it could be longer.
/// 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.
/// 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 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
/// 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,
/// You may need to consider the implications of very short delays on the overall performance of the system, /// 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(UInt32 micros);
void SleepMicroseconds(uint 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> /// <summary>
/// Represents a low-level exception, typically thrown when return codes from a /// Initializes a new instance of the <see cref="HardwareException"/> class.
/// low-level operation is non-zero or in some cases when it is less than zero.
/// </summary> /// </summary>
/// <seealso cref="Exception" /> /// <param name="errorCode">The error code.</param>
public class HardwareException : Exception /// <param name="component">The component.</param>
{ public HardwareException(Int32 errorCode, String component) : base($"A hardware exception occurred. Error Code: {errorCode}") {
/// <summary> this.ExtendedMessage = null;
/// Initializes a new instance of the <see cref="HardwareException"/> class.
/// </summary> try {
/// <param name="errorCode">The error code.</param> this.ExtendedMessage = Standard.Strerror(errorCode);
/// <param name="component">The component.</param> } catch {
public HardwareException(int errorCode, string component) // Ignore
: base($"A hardware exception occurred. Error Code: {errorCode}") }
{
ExtendedMessage = null; this.ErrorCode = errorCode;
this.Component = component;
try }
{
ExtendedMessage = Standard.Strerror(errorCode); /// <summary>
} /// Gets the error code.
catch /// </summary>
{ /// <value>
// Ignore /// The error code.
} /// </value>
public Int32 ErrorCode {
ErrorCode = errorCode; get;
Component = component; }
}
/// <summary>
/// <summary> /// Gets the component.
/// Gets the error code. /// </summary>
/// </summary> /// <value>
/// <value> /// The component.
/// The error code. /// </value>
/// </value> public String Component {
public int ErrorCode { get; } get;
}
/// <summary>
/// Gets the component. /// <summary>
/// </summary> /// Gets the extended message (could be null).
/// <value> /// </summary>
/// The component. /// <value>
/// </value> /// The extended message.
public string Component { get; } /// </value>
public String? ExtendedMessage {
/// <summary> get;
/// Gets the extended message (could be null). }
/// </summary>
/// <value> /// <summary>
/// The extended message. /// Throws a new instance of a hardware error by retrieving the last error number (errno).
/// </value> /// </summary>
public string? ExtendedMessage { get; } /// <param name="className">Name of the class.</param>
/// <param name="methodName">Name of the method.</param>
/// <summary> /// <exception cref="HardwareException">When an error thrown by an API call occurs.</exception>
/// Throws a new instance of a hardware error by retrieving the last error number (errno). public static void Throw(String className, String methodName) => throw new HardwareException(Marshal.GetLastWin32Error(), $"{className}.{methodName}");
/// </summary>
/// <param name="className">Name of the class.</param> /// <inheritdoc />
/// <param name="methodName">Name of the method.</param> public override String ToString() => $"{nameof(HardwareException)}{(String.IsNullOrWhiteSpace(this.Component) ? String.Empty : $" on {this.Component}")}: ({this.ErrorCode}) - {this.Message}";
/// <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}";
}
} }

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) {
#region LibC Calls return Marshal.PtrToStringAnsi(StrError(error));
}
/// <summary>
/// Strerrors the specified error. try {
/// </summary> StringBuilder buffer = new StringBuilder(256);
/// <param name="error">The error.</param> Int32 result = Strerror(error, buffer, (UInt64)buffer.Capacity);
/// <returns>The error string.</returns> return (result != -1) ? buffer.ToString() : null;
public static string Strerror(int error) } catch(Exception) {
{ return null;
if (Type.GetType("Mono.Runtime") == null) return Marshal.PtrToStringAnsi(StrError(error)); }
}
try
{ [DllImport(LibCLibrary, EntryPoint = "strerror", SetLastError = true)]
var buffer = new StringBuilder(256); private static extern IntPtr StrError(Int32 errnum);
var result = Strerror(error, buffer, (ulong)buffer.Capacity);
return (result != -1) ? buffer.ToString() : null; [DllImport("MonoPosixHelper", EntryPoint = "Mono_Posix_Syscall_strerror_r", SetLastError = true)]
} private static extern Int32 Strerror(Int32 error, [Out] StringBuilder buffer, UInt64 length);
catch (Exception)
{ #endregion
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
}
} }

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>