Skip to content

Commit

Permalink
Merge branch 'develop' into feature/stepper-online
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Jan 3, 2025
2 parents ea2ccf6 + 1404f60 commit a8e499a
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using Meadow.Hardware;
using Meadow.Units;
using System;

namespace Meadow.Foundation.Sensors;

/// <summary>
/// Represents a potentiometer - a variable resistor that can be measured through an analog input.
/// Implements both IPotentiometer and IDisposable interfaces.
/// </summary>
public class Potentiometer : IPotentiometer, IDisposable
{
private IAnalogInputPort inputPort;
private Voltage referenceVoltage;
private bool portCreated = false;

/// <summary>
/// Gets whether this instance has been disposed.
/// </summary>
public bool IsDisposed { get; private set; }

/// <summary>
/// Gets the maximum resistance value of the potentiometer.
/// </summary>
public Resistance MaxResistance { get; }

/// <summary>
/// Initializes a new instance of the Potentiometer class with default reference voltage.
/// </summary>
/// <param name="inputPort">The input port to read the potentiometer value from.</param>
/// <param name="maxResistance">The maximum resistance value of the potentiometer.</param>
public Potentiometer(IAnalogInputPort inputPort, Resistance maxResistance)
{
this.inputPort = inputPort;
MaxResistance = maxResistance;
referenceVoltage = inputPort.ReferenceVoltage;
}

/// <summary>
/// Initializes a new instance of the Potentiometer class with default reference voltage.
/// </summary>
/// <param name="inputPin">The input pin to read the potentiometer value from.</param>
/// <param name="maxResistance">The maximum resistance value of the potentiometer.</param>
public Potentiometer(IPin inputPin, Resistance maxResistance)
{
inputPort = inputPin.CreateAnalogInputPort(1);
MaxResistance = maxResistance;
referenceVoltage = inputPort.ReferenceVoltage;
portCreated = true;
}

/// <summary>
/// Initializes a new instance of the Potentiometer class with a specified reference voltage.
/// </summary>
/// <param name="inputPin">The input pin to read the potentiometer value from.</param>
/// <param name="maxResistance">The maximum resistance value of the potentiometer.</param>
/// <param name="referenceVoltage">The reference voltage used for analog-to-digital conversion.</param>
public Potentiometer(IPin inputPin, Resistance maxResistance, Voltage referenceVoltage)
{
inputPort = inputPin.CreateAnalogInputPort(1);
MaxResistance = maxResistance;
this.referenceVoltage = referenceVoltage;
portCreated = true;
}

/// <summary>
/// Gets the current resistance value of the potentiometer by reading the analog input.
/// Setting the resistance throws NotSupportedException as potentiometer value can only be changed mechanically.
/// </summary>
/// <exception cref="NotSupportedException">Thrown when attempting to set the resistance value.</exception>
public Resistance Resistance
{
get
{
var adcVoltage = inputPort.Read().Result;
return new Resistance(MaxResistance.Ohms * adcVoltage.Volts / referenceVoltage.Volts);
}
set
{
throw new NotSupportedException("Standard potentiometer resistance is set mechanically");
}
}

/// <summary>
/// Releases the unmanaged resources used by the Potentiometer and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">True to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
if (portCreated) inputPort.Dispose();
}
IsDisposed = true;
}
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}

0 comments on commit a8e499a

Please sign in to comment.