Skip to content

Commit

Permalink
We do not need to account for drift when USE_WINDOWS_EVENTS
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreluctg authored Sep 27, 2023
1 parent db177b3 commit 074e03a
Showing 1 changed file with 11 additions and 12 deletions.
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,
-1,
)
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)

0 comments on commit 074e03a

Please sign in to comment.