Skip to content

Commit

Permalink
Merge pull request #911 from WildernessLabs/feature/interrupts
Browse files Browse the repository at this point in the history
interrupt events use a tick instead of time
  • Loading branch information
jorgedevs authored Feb 26, 2024
2 parents a17938a + ab2eea4 commit 4baa674
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DigitalInterruptPort : DigitalInterruptPortBase
public override bool State => state;
private bool state = false;

private DateTime lastUpdate;
private int lastUpdate;

/// <inheritdoc/>
public override ResistorMode Resistor
Expand Down Expand Up @@ -52,13 +52,13 @@ public DigitalInterruptPort(IPin pin, InterruptMode interruptMode = InterruptMod
/// <param name="newState">The new port state</param>
internal void Update(bool newState)
{
if (DateTime.UtcNow - lastUpdate < DebounceDuration)
var now = Environment.TickCount;

if (now - lastUpdate < DebounceDuration.TotalMilliseconds)
{
return;
}

var now = DateTime.UtcNow;

if (newState != state)
{
switch (InterruptMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override ResistorMode Resistor
public override bool State => state;
private bool state = false;

private DateTime lastUpdate;
private int lastUpdate;

private ResistorMode resistorMode = ResistorMode.Disabled;

Expand Down Expand Up @@ -68,13 +68,13 @@ public DigitalInterruptPort(IPin pin, InterruptMode interruptMode = InterruptMod
/// <param name="newState">The new port state</param>
internal void Update(bool newState)
{
if (DateTime.UtcNow - lastUpdate < DebounceDuration)
var now = Environment.TickCount;

if (now - lastUpdate < DebounceDuration.TotalMilliseconds)
{
return;
}

var now = DateTime.UtcNow;

if (newState != state)
{
switch (InterruptMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ internal void SetState(bool newState)
case InterruptMode.EdgeRising:
if (State)
{
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(State, DateTime.UtcNow), null));
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(State, Environment.TickCount), null));
}
break;
case InterruptMode.EdgeFalling:
if (!State)
{
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(State, DateTime.UtcNow), null));
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(State, Environment.TickCount), null));
}
break;
case InterruptMode.EdgeBoth:
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(State, DateTime.UtcNow), null));
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(State, Environment.TickCount), null));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int SampleCount
/// <summary>
/// Did we create the port(s) used by the peripheral
/// </summary>
readonly bool createdPort = false;
private readonly bool createdPort = false;

/// <summary>
/// Creates a new `SwitchingAnemometer` using the specific digital input
Expand Down Expand Up @@ -152,7 +152,9 @@ protected override async Task<Speed> ReadSensor()
StopUpdating();
}

if (samples?.Count > 0 && (DateTime.UtcNow - samples?.Peek().New.Time > NoWindTimeout))
var now = Environment.TickCount;

if (samples?.Count > 0 && (TimeSpan.FromMilliseconds(now - samples?.Peek().New.Time ?? 0) > NoWindTimeout))
{ //we've exceeded the no wind interval time
samples?.Clear(); //will force a zero reading
}
Expand All @@ -166,7 +168,7 @@ protected override async Task<Speed> ReadSensor()
{ // skip the first (old will be null)
if (sample.Old is { } old)
{
speedSum += SwitchIntervalToKmh(sample.New.Time - old.Time);
speedSum += SwitchIntervalToKmh(TimeSpan.FromMilliseconds(sample.New.Time - old.Time));
}
}

Expand Down

0 comments on commit 4baa674

Please sign in to comment.