Skip to content

Commit

Permalink
update F7 for PWM updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Jun 20, 2024
1 parent 3853669 commit 2e51aee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
8 changes: 4 additions & 4 deletions source/Meadow.Core/Hardware/SoftPwmPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public class SoftPwmPort : IPwmPort
/// </summary>
public TimeSpan Duration
{
get => TimeSpan.FromSeconds(Period * DutyCycle);
get => TimeSpan.FromSeconds(Period.TotalSeconds * DutyCycle);
set { }
}

/// <summary>
/// Period of PWM
/// </summary>
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);
}

/// <summary>
Expand Down
21 changes: 10 additions & 11 deletions source/implementations/f7/Meadow.F7/Hardware/PwmPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -130,7 +129,7 @@ public override Frequency Frequency
/// <summary>
/// The percentage of time the PWM pulse is high (in the range of 0.0 to 1.0)
/// </summary>
public override float DutyCycle
public override double DutyCycle
{
get => _dutyCycle;
set
Expand Down Expand Up @@ -158,34 +157,34 @@ public override float DutyCycle
/// <summary>
/// The amount of time, in seconds, that the a PWM pulse is high. This will always be less than or equal to the Period
/// </summary>
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;
}
}

/// <summary>
/// The reciprocal of the PWM frequency - in seconds.
/// </summary>
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));
}


Expand Down

0 comments on commit 2e51aee

Please sign in to comment.