diff --git a/Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs b/Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs new file mode 100644 index 000000000..351dcd2f9 --- /dev/null +++ b/Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs @@ -0,0 +1,109 @@ +using Meadow.Hardware; +using Meadow.Units; +using System; + +namespace Meadow.Foundation.Sensors; + +/// +/// Represents a potentiometer - a variable resistor that can be measured through an analog input. +/// Implements both IPotentiometer and IDisposable interfaces. +/// +public class Potentiometer : IPotentiometer, IDisposable +{ + private IAnalogInputPort inputPort; + private Voltage referenceVoltage; + private bool portCreated = false; + + /// + /// Gets whether this instance has been disposed. + /// + public bool IsDisposed { get; private set; } + + /// + /// Gets the maximum resistance value of the potentiometer. + /// + public Resistance MaxResistance { get; } + + /// + /// Initializes a new instance of the Potentiometer class with default reference voltage. + /// + /// The input port to read the potentiometer value from. + /// The maximum resistance value of the potentiometer. + public Potentiometer(IAnalogInputPort inputPort, Resistance maxResistance) + { + this.inputPort = inputPort; + MaxResistance = maxResistance; + referenceVoltage = inputPort.ReferenceVoltage; + } + + /// + /// Initializes a new instance of the Potentiometer class with default reference voltage. + /// + /// The input pin to read the potentiometer value from. + /// The maximum resistance value of the potentiometer. + public Potentiometer(IPin inputPin, Resistance maxResistance) + { + inputPort = inputPin.CreateAnalogInputPort(1); + MaxResistance = maxResistance; + referenceVoltage = inputPort.ReferenceVoltage; + portCreated = true; + } + + /// + /// Initializes a new instance of the Potentiometer class with a specified reference voltage. + /// + /// The input pin to read the potentiometer value from. + /// The maximum resistance value of the potentiometer. + /// The reference voltage used for analog-to-digital conversion. + public Potentiometer(IPin inputPin, Resistance maxResistance, Voltage referenceVoltage) + { + inputPort = inputPin.CreateAnalogInputPort(1); + MaxResistance = maxResistance; + this.referenceVoltage = referenceVoltage; + portCreated = true; + } + + /// + /// 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. + /// + /// Thrown when attempting to set the resistance value. + 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"); + } + } + + /// + /// Releases the unmanaged resources used by the Potentiometer and optionally releases the managed resources. + /// + /// True to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + if (disposing) + { + if (portCreated) inputPort.Dispose(); + } + IsDisposed = true; + } + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } +} \ No newline at end of file