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

We do not need to account for drift when we USE_WINDOWS_EVENTS #1666

Merged
merged 4 commits into from
Oct 2, 2023
Merged
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
23 changes: 11 additions & 12 deletions can/broadcastmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
log = logging.getLogger("can.bcm")

NANOSECONDS_IN_SECOND: Final[int] = 1_000_000_000
NANOSECONDS_IN_MILLISECOND: Final[int] = 1_000_000


class CyclicTask(abc.ABC):
Expand Down Expand Up @@ -316,19 +315,19 @@ def _run(self) -> None:
self.stop()
break

msg_due_time_ns += self.period_ns
if not USE_WINDOWS_EVENTS:
msg_due_time_ns += self.period_ns
if self.end_time is not None and time.perf_counter() >= self.end_time:
break
msg_index = (msg_index + 1) % len(self.messages)

# Compensate for the time it takes to send the message
delay_ns = msg_due_time_ns - time.perf_counter_ns()

if delay_ns > 0:
if USE_WINDOWS_EVENTS:
win32event.WaitForSingleObject(
self.event.handle,
int(round(delay_ns / NANOSECONDS_IN_MILLISECOND)),
)
else:
if USE_WINDOWS_EVENTS:
win32event.WaitForSingleObject(
self.event.handle,
win32event.INFINITE,
)
else:
# Compensate for the time it takes to send the message
delay_ns = msg_due_time_ns - time.perf_counter_ns()
if delay_ns > 0:
time.sleep(delay_ns / NANOSECONDS_IN_SECOND)