From 2e51aeea4c14d473c82432f78b1537163e567354 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Thu, 20 Jun 2024 11:49:05 -0500 Subject: [PATCH] update F7 for PWM updates --- source/Meadow.Core/Hardware/SoftPwmPort.cs | 8 +++---- .../f7/Meadow.F7/Hardware/PwmPort.cs | 21 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/source/Meadow.Core/Hardware/SoftPwmPort.cs b/source/Meadow.Core/Hardware/SoftPwmPort.cs index 1062cd48..a3de49d1 100644 --- a/source/Meadow.Core/Hardware/SoftPwmPort.cs +++ b/source/Meadow.Core/Hardware/SoftPwmPort.cs @@ -23,17 +23,17 @@ public class SoftPwmPort : IPwmPort /// public TimeSpan Duration { - get => TimeSpan.FromSeconds(Period * DutyCycle); + get => TimeSpan.FromSeconds(Period.TotalSeconds * DutyCycle); set { } } /// /// Period of PWM /// - public double Period + public TimeSpan Period { - get => 1 / (float)frequency.Hertz; - set => frequency = new Frequency(1 / value, Units.Frequency.UnitType.Hertz); + get => TimeSpan.FromSeconds(1 / frequency.Hertz); + set => frequency = new Frequency(1 / value.TotalSeconds, Frequency.UnitType.Hertz); } /// diff --git a/source/implementations/f7/Meadow.F7/Hardware/PwmPort.cs b/source/implementations/f7/Meadow.F7/Hardware/PwmPort.cs index 2408b443..cba7f0fd 100644 --- a/source/implementations/f7/Meadow.F7/Hardware/PwmPort.cs +++ b/source/implementations/f7/Meadow.F7/Hardware/PwmPort.cs @@ -13,7 +13,7 @@ public class PwmPort : PwmPortBase { private bool _isRunning = false; private Frequency _frequency; - private float _dutyCycle; + private double _dutyCycle; private bool _inverted; // dirty dirty hack @@ -68,7 +68,6 @@ internal static PwmPort From( if (success.Item1) { var port = new PwmPort(pin, ioController, channel, inverted, isOnboard); - port.TimeScale = TimeScale.Seconds; port.Frequency = frequency; port.DutyCycle = dutyCycle; port.Inverted = inverted; @@ -130,7 +129,7 @@ public override Frequency Frequency /// /// The percentage of time the PWM pulse is high (in the range of 0.0 to 1.0) /// - public override float DutyCycle + public override double DutyCycle { get => _dutyCycle; set @@ -158,34 +157,34 @@ public override float DutyCycle /// /// The amount of time, in seconds, that the a PWM pulse is high. This will always be less than or equal to the Period /// - public override float Duration + public override TimeSpan Duration { - get => DutyCycle * Period; + get => TimeSpan.FromSeconds(DutyCycle * Period.TotalSeconds); set { if (value > Period) throw new ArgumentOutOfRangeException(nameof(Duration), "Duration must be less than or equal to Period"); // clamp - if (value < 0) { value = 0; } + if (value.TotalSeconds < 0) { value = TimeSpan.Zero; } - DutyCycle = value / Period; + DutyCycle = value.TotalSeconds / Period.TotalSeconds; } } /// /// The reciprocal of the PWM frequency - in seconds. /// - public override float Period + public override TimeSpan Period { - get => 1.0f / (float)Frequency.Hertz * (float)TimeScale; + get => TimeSpan.FromSeconds(1.0f / Frequency.Hertz); set { - Frequency = new Frequency(1.0f * (float)TimeScale / value); + Frequency = new Frequency(1.0f / value.TotalSeconds); } } private void UpdateChannel() { - UPD.PWM.Start(PwmChannelInfo, (uint)Frequency.Hertz, Inverted ? (1.0f - DutyCycle) : DutyCycle); + UPD.PWM.Start(PwmChannelInfo, (uint)Frequency.Hertz, (float)(Inverted ? (1.0f - DutyCycle) : DutyCycle)); }