diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Power.CurrentTransducer/Driver/CurrentTransducer.cs b/Source/Meadow.Foundation.Peripherals/Sensors.Power.CurrentTransducer/Driver/CurrentTransducer.cs
index bafcc1616..1dd9cf411 100644
--- a/Source/Meadow.Foundation.Peripherals/Sensors.Power.CurrentTransducer/Driver/CurrentTransducer.cs
+++ b/Source/Meadow.Foundation.Peripherals/Sensors.Power.CurrentTransducer/Driver/CurrentTransducer.cs
@@ -1,157 +1,157 @@
using Meadow.Hardware;
+using Meadow.Peripherals.Sensors;
using Meadow.Units;
using System;
using System.Threading;
using System.Threading.Tasks;
-namespace Meadow.Foundation.Sensors.Power
+namespace Meadow.Foundation.Sensors.Power;
+
+///
+/// Represents a general Current Transducer (CT) sensor
+///
+public partial class CurrentTransducer : SamplingSensorBase, ICurrentSensor
{
///
- /// Represents a general Current Transducer (CT) sensor
+ /// The analog input port connected to the transducer
+ ///
+ protected IAnalogInputPort AnalogPort { get; private set; } = default!;
+
+ ///
+ /// The maximum voltage the CT outputs
+ ///
+ protected Voltage MaxVoltage { get; private set; } = default!;
+
+ ///
+ /// The sensed current at the maximum output voltage
+ ///
+ protected Current MaxCurrent { get; private set; } = default!;
+
+ ///
+ /// The minimum voltage the CT outputs
+ ///
+ protected Voltage MinVoltage { get; private set; } = default!;
+
+ ///
+ /// The sensed current at the minimum output voltage
///
- public partial class CurrentTransducer : SamplingSensorBase
+ protected Current MinCurrent { get; private set; } = default!;
+
+ ///
+ /// The minimum output voltage
+ ///
+ protected Current MinVoltageDelta { get; private set; } = default!;
+
+ ///
+ /// The last sensed Current
+ ///
+ public Current? Current { get; protected set; }
+
+ ///
+ /// Creates a new CurrentTransducer instance
+ ///
+ /// The analog input port connected to the transducer
+ /// The maximum voltage the CT outputs
+ /// The sensed current at the maximum output voltage
+ /// The minimum voltage the CT outputs
+ /// The sensed current at the minimum output voltage
+ public CurrentTransducer(IAnalogInputPort analogPort, Voltage maxVoltage, Current maxCurrent, Voltage? minVoltage = null, Current? minCurrent = null)
{
- ///
- /// The analog input port connected to the transducer
- ///
- protected IAnalogInputPort AnalogPort { get; private set; } = default!;
-
- ///
- /// The maximum voltage the CT outputs
- ///
- protected Voltage MaxVoltage { get; private set; } = default!;
-
- ///
- /// The sensed current at the maximum output voltage
- ///
- protected Current MaxCurrent { get; private set; } = default!;
-
- ///
- /// The minimum voltage the CT outputs
- ///
- protected Voltage MinVoltage { get; private set; } = default!;
-
- ///
- /// The sensed current at the minimum output voltage
- ///
- protected Current MinCurrent { get; private set; } = default!;
-
- ///
- /// The minimum output voltage
- ///
- protected Current MinVoltageDelta { get; private set; } = default!;
-
- ///
- /// The last sensed Current
- ///
- public Current? Current { get; protected set; }
-
- ///
- /// Creates a new CurrentTransducer instance
- ///
- /// The analog input port connected to the transducer
- /// The maximum voltage the CT outputs
- /// The sensed current at the maximum output voltage
- /// The minimum voltage the CT outputs
- /// The sensed current at the minimum output voltage
- public CurrentTransducer(IAnalogInputPort analogPort, Voltage maxVoltage, Current maxCurrent, Voltage? minVoltage = null, Current? minCurrent = null)
- {
- Initialize(analogPort, maxVoltage, maxCurrent, minVoltage, minCurrent);
- }
+ Initialize(analogPort, maxVoltage, maxCurrent, minVoltage, minCurrent);
+ }
- ///
- /// Creates a new CurrentTransducer instance
- ///
- protected CurrentTransducer()
- {
- }
+ ///
+ /// Creates a new CurrentTransducer instance
+ ///
+ protected CurrentTransducer()
+ {
+ }
- ///
- /// Initializes the CurrentTransducer instance
- /// Use this method when a derived class must do pre-initialization work
- ///
- /// The analog input port connected to the transducer
- /// The maximum voltage the CT outputs
- /// The sensed current at the maximum output voltage
- /// The minimum voltage the CT outputs
- /// The sensed current at the minimum output voltage
- protected virtual void Initialize(IAnalogInputPort analogPort, Voltage maxVoltage, Current maxCurrent, Voltage? minVoltage = null, Current? minCurrent = null)
- {
- AnalogPort = analogPort;
- MaxVoltage = maxVoltage;
- MaxCurrent = maxCurrent;
- MinVoltage = minVoltage ?? new Voltage(0, Voltage.UnitType.Volts);
- MinCurrent = minCurrent ?? new Current(0, Units.Current.UnitType.Amps);
-
- AnalogPort.Subscribe
- (
- IAnalogInputPort.CreateObserver(
- result =>
+ ///
+ /// Initializes the CurrentTransducer instance
+ /// Use this method when a derived class must do pre-initialization work
+ ///
+ /// The analog input port connected to the transducer
+ /// The maximum voltage the CT outputs
+ /// The sensed current at the maximum output voltage
+ /// The minimum voltage the CT outputs
+ /// The sensed current at the minimum output voltage
+ protected virtual void Initialize(IAnalogInputPort analogPort, Voltage maxVoltage, Current maxCurrent, Voltage? minVoltage = null, Current? minCurrent = null)
+ {
+ AnalogPort = analogPort;
+ MaxVoltage = maxVoltage;
+ MaxCurrent = maxCurrent;
+ MinVoltage = minVoltage ?? new Voltage(0, Voltage.UnitType.Volts);
+ MinCurrent = minCurrent ?? new Current(0, Units.Current.UnitType.Amps);
+
+ AnalogPort.Subscribe
+ (
+ IAnalogInputPort.CreateObserver(
+ result =>
+ {
+ ChangeResult changeResult = new()
{
- ChangeResult changeResult = new()
- {
- New = ConvertVoltageToCurrent(result.New),
- Old = Current
- };
- Current = changeResult.New;
- RaiseEventsAndNotify(changeResult);
- }
- )
- );
- }
+ New = ConvertVoltageToCurrent(result.New),
+ Old = Current
+ };
+ Current = changeResult.New;
+ RaiseEventsAndNotify(changeResult);
+ }
+ )
+ );
+ }
- ///
- /// Converts an output voltage from the CT to a sensed current using linear interpolation
- ///
- /// The ADC voltage read by the AnalogPort
- /// The current being sensed by the CT
- protected virtual Current ConvertVoltageToCurrent(Voltage voltage)
- {
- // the default implementation just does a simple linear conversion
- return new Current(
- (MaxCurrent.Amps - MinCurrent.Amps) /
- (MaxVoltage.Volts - MinVoltage.Volts)
- * voltage.Volts);
- }
+ ///
+ /// Converts an output voltage from the CT to a sensed current using linear interpolation
+ ///
+ /// The ADC voltage read by the AnalogPort
+ /// The current being sensed by the CT
+ protected virtual Current ConvertVoltageToCurrent(Voltage voltage)
+ {
+ // the default implementation just does a simple linear conversion
+ return new Current(
+ (MaxCurrent.Amps - MinCurrent.Amps) /
+ (MaxVoltage.Volts - MinVoltage.Volts)
+ * voltage.Volts);
+ }
- ///
- protected override async Task ReadSensor()
- {
- var adc = await AnalogPort.Read();
- var newCurrent = ConvertVoltageToCurrent(adc);
- Current = newCurrent;
- return newCurrent;
- }
+ ///
+ protected override async Task ReadSensor()
+ {
+ var adc = await AnalogPort.Read();
+ var newCurrent = ConvertVoltageToCurrent(adc);
+ Current = newCurrent;
+ return newCurrent;
+ }
- ///
- public override void StartUpdating(TimeSpan? updateInterval = null)
+ ///
+ public override void StartUpdating(TimeSpan? updateInterval = null)
+ {
+ lock (samplingLock)
{
- lock (samplingLock)
- {
- if (IsSampling) return;
- IsSampling = true;
+ if (IsSampling) return;
+ IsSampling = true;
- base.SamplingTokenSource = new CancellationTokenSource();
- CancellationToken ct = SamplingTokenSource.Token;
+ base.SamplingTokenSource = new CancellationTokenSource();
+ CancellationToken ct = SamplingTokenSource.Token;
- AnalogPort.StartUpdating(updateInterval);
- }
+ AnalogPort.StartUpdating(updateInterval);
}
+ }
- ///
- public override void StopUpdating()
+ ///
+ public override void StopUpdating()
+ {
+ lock (samplingLock)
{
- lock (samplingLock)
- {
- if (!IsSampling) return;
+ if (!IsSampling) return;
- AnalogPort.StopUpdating();
+ AnalogPort.StopUpdating();
- SamplingTokenSource?.Cancel();
+ SamplingTokenSource?.Cancel();
- // state machine
- IsSampling = false;
- }
+ // state machine
+ IsSampling = false;
}
}
}
\ No newline at end of file