diff --git a/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002AnalogPinExtensionMethods.cs b/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002AnalogPinExtensionMethods.cs new file mode 100644 index 0000000..a42fc51 --- /dev/null +++ b/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002AnalogPinExtensionMethods.cs @@ -0,0 +1,20 @@ +namespace Raspberry.IO.Components.Converters.Mcp3002 +{ + public static class Mcp3002AnalogPinExtensionMethods + { + #region Methods + + /// + /// Creates an analog input pin. + /// + /// The connection. + /// The channel. + /// The pin. + public static Mcp3002InputAnalogPin In(this Mcp3002SpiConnection connection, Mcp3002Channel channel) + { + return new Mcp3002InputAnalogPin(connection, channel); + } + + #endregion + } +} \ No newline at end of file diff --git a/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002Channel.cs b/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002Channel.cs new file mode 100644 index 0000000..8c06ae2 --- /dev/null +++ b/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002Channel.cs @@ -0,0 +1,8 @@ +namespace Raspberry.IO.Components.Converters.Mcp3002 +{ + public enum Mcp3002Channel + { + Channel0 = 0, + Channel1 = 1 + } +} \ No newline at end of file diff --git a/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002InputAnalogPin.cs b/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002InputAnalogPin.cs new file mode 100644 index 0000000..71e01ca --- /dev/null +++ b/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002InputAnalogPin.cs @@ -0,0 +1,47 @@ +namespace Raspberry.IO.Components.Converters.Mcp3002 +{ + public class Mcp3002InputAnalogPin : IInputAnalogPin + { + #region Fields + + private readonly Mcp3002SpiConnection connection; + private readonly Mcp3002Channel channel; + + #endregion + + #region Instance Management + + /// + /// Initializes a new instance of the class. + /// + /// The connection. + /// The channel. + public Mcp3002InputAnalogPin(Mcp3002SpiConnection connection, Mcp3002Channel channel) + { + this.connection = connection; + this.channel = channel; + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose(){} + + #endregion + + #region Methods + + /// + /// Reads the value of the pin. + /// + /// + /// The value. + /// + public AnalogValue Read() + { + return connection.Read(channel); + } + + #endregion + } +} \ No newline at end of file diff --git a/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002SpiConnection.cs b/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002SpiConnection.cs new file mode 100644 index 0000000..c46f2b8 --- /dev/null +++ b/Raspberry.IO.Components/Converters/Mcp3002/Mcp3002SpiConnection.cs @@ -0,0 +1,85 @@ +#region References + +using System; +using Raspberry.IO.SerialPeripheralInterface; + +#endregion + +namespace Raspberry.IO.Components.Converters.Mcp3002 +{ + /// + /// Represents a connection to MCP3002 ADC converter. + /// + public class Mcp3002SpiConnection : IDisposable + { + #region Fields + + private readonly SpiConnection spiConnection; + + #endregion + + #region Instance Management + + /// + /// Initializes a new instance of the class. + /// + /// The clock pin. + /// The slave select pin. + /// The miso pin. + /// The mosi pin. + public Mcp3002SpiConnection(IOutputBinaryPin clockPin, IOutputBinaryPin slaveSelectPin, IInputBinaryPin misoPin, IOutputBinaryPin mosiPin) + { + spiConnection = new SpiConnection(clockPin, slaveSelectPin, misoPin, mosiPin, Endianness.LittleEndian); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + void IDisposable.Dispose() + { + Close(); + } + + #endregion + + #region Methods + + /// + /// Reads the specified channel. + /// + /// The channel. + /// The value + public AnalogValue Read(Mcp3002Channel channel) + { + using(spiConnection.SelectSlave()) + { + // Start bit + spiConnection.Write(true); + + // Channel is single-ended + spiConnection.Write(true); + + // Channel Id + spiConnection.Write((byte)channel, 1); + + // Let one clock to sample + spiConnection.Synchronize(); + + // Read 10 bits + var data = (int)spiConnection.Read(10); + + return new AnalogValue(data, 0x3FF); + } + } + + /// + /// Closes this instance. + /// + public void Close() + { + spiConnection.Close(); + } + + #endregion + } +} \ No newline at end of file diff --git a/Raspberry.IO.Components/Raspberry.IO.Components.csproj b/Raspberry.IO.Components/Raspberry.IO.Components.csproj index eed52df..2b926d6 100644 --- a/Raspberry.IO.Components/Raspberry.IO.Components.csproj +++ b/Raspberry.IO.Components/Raspberry.IO.Components.csproj @@ -65,6 +65,10 @@ + + + +