-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/stepper-online
- Loading branch information
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |