Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Raspberry.IO.Components.Converters.Mcp3002
{
public static class Mcp3002AnalogPinExtensionMethods
{
#region Methods

/// <summary>
/// Creates an analog input pin.
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="channel">The channel.</param>
/// <returns>The pin.</returns>
public static Mcp3002InputAnalogPin In(this Mcp3002SpiConnection connection, Mcp3002Channel channel)
{
return new Mcp3002InputAnalogPin(connection, channel);
}

#endregion
}
}
8 changes: 8 additions & 0 deletions Raspberry.IO.Components/Converters/Mcp3002/Mcp3002Channel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Raspberry.IO.Components.Converters.Mcp3002
{
public enum Mcp3002Channel
{
Channel0 = 0,
Channel1 = 1
}
}
Original file line number Diff line number Diff line change
@@ -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

/// <summary>
/// Initializes a new instance of the <see cref="Mcp3002InputAnalogPin" /> class.
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="channel">The channel.</param>
public Mcp3002InputAnalogPin(Mcp3002SpiConnection connection, Mcp3002Channel channel)
{
this.connection = connection;
this.channel = channel;
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose(){}

#endregion

#region Methods

/// <summary>
/// Reads the value of the pin.
/// </summary>
/// <returns>
/// The value.
/// </returns>
public AnalogValue Read()
{
return connection.Read(channel);
}

#endregion
}
}
85 changes: 85 additions & 0 deletions Raspberry.IO.Components/Converters/Mcp3002/Mcp3002SpiConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#region References

using System;
using Raspberry.IO.SerialPeripheralInterface;

#endregion

namespace Raspberry.IO.Components.Converters.Mcp3002
{
/// <summary>
/// Represents a connection to MCP3002 ADC converter.
/// </summary>
public class Mcp3002SpiConnection : IDisposable
{
#region Fields

private readonly SpiConnection spiConnection;

#endregion

#region Instance Management

/// <summary>
/// Initializes a new instance of the <see cref="Mcp3002SpiConnection"/> class.
/// </summary>
/// <param name="clockPin">The clock pin.</param>
/// <param name="slaveSelectPin">The slave select pin.</param>
/// <param name="misoPin">The miso pin.</param>
/// <param name="mosiPin">The mosi pin.</param>
public Mcp3002SpiConnection(IOutputBinaryPin clockPin, IOutputBinaryPin slaveSelectPin, IInputBinaryPin misoPin, IOutputBinaryPin mosiPin)
{
spiConnection = new SpiConnection(clockPin, slaveSelectPin, misoPin, mosiPin, Endianness.LittleEndian);
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
void IDisposable.Dispose()
{
Close();
}

#endregion

#region Methods

/// <summary>
/// Reads the specified channel.
/// </summary>
/// <param name="channel">The channel.</param>
/// <returns>The value</returns>
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);
}
}

/// <summary>
/// Closes this instance.
/// </summary>
public void Close()
{
spiConnection.Close();
}

#endregion
}
}
4 changes: 4 additions & 0 deletions Raspberry.IO.Components/Raspberry.IO.Components.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<Compile Include="Controllers\Tlc59711\Tlc59711Settings.cs" />
<Compile Include="Controllers\Tlc59711\Tlc59711Connection.cs" />
<Compile Include="Controllers\Tlc59711\Tlc59711Device.cs" />
<Compile Include="Converters\Mcp3002\Mcp3002AnalogPinExtensionMethods.cs" />
<Compile Include="Converters\Mcp3002\Mcp3002Channel.cs" />
<Compile Include="Converters\Mcp3002\Mcp3002InputAnalogPin.cs" />
<Compile Include="Converters\Mcp3002\Mcp3002SpiConnection.cs" />
<Compile Include="Converters\Mcp3008\Mcp3008AnalogPinExtensionMethods.cs" />
<Compile Include="Controllers\Pca9685\IPwmDevice.cs" />
<Compile Include="Controllers\Pca9685\PwmChannel.cs" />
Expand Down