Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interrupt events use a tick instead of time #911

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading