-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3.2.6 - Add ability to control default time source
Also add 'saturated' check for circular float buffers
- Loading branch information
Showing
17 changed files
with
205 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule config
updated
9 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
major: 3 | ||
minor: 2 | ||
patch: 5 | ||
patch: 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Test the 'math.analysis.buffer' module. | ||
""" | ||
|
||
# module under test | ||
from vcorelib.math.analysis.buffer import FloatBuffer | ||
|
||
|
||
def test_float_buffer_basic(): | ||
"""Test basic interactions with a circular buffer.""" | ||
|
||
buffer = FloatBuffer() | ||
assert not buffer.saturated | ||
|
||
buffer(1.0) | ||
assert not buffer.saturated | ||
|
||
for _ in range(buffer.depth - 2): | ||
buffer(1.0) | ||
assert not buffer.saturated | ||
|
||
buffer(1.0) | ||
assert buffer.saturated | ||
|
||
buffer.reset() | ||
assert not buffer.saturated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
A module implementing a simple time-keeper interface. | ||
""" | ||
|
||
# built-in | ||
from contextlib import contextmanager | ||
from time import time_ns as _time_ns | ||
from typing import Callable, Iterator | ||
|
||
# internal | ||
from vcorelib.math.constants import to_nanos | ||
|
||
TimeSource = Callable[[], int] | ||
|
||
|
||
class SimulatedTime: | ||
"""A simple simulated-time interface.""" | ||
|
||
def __init__(self, step_dt_ns: int, start_ns: int = 0) -> None: | ||
"""Initialize this instance.""" | ||
|
||
self._start_ns = start_ns | ||
self._step = 0 | ||
|
||
assert step_dt_ns >= 1, step_dt_ns | ||
self._step_dt_ns = step_dt_ns | ||
|
||
def step(self, count: int = 1) -> None: | ||
"""Step forward (or backwards) in time.""" | ||
self._step += count | ||
|
||
def step_s(self, time_s: float) -> None: | ||
"""Step some number of seconds.""" | ||
self.step(to_nanos(time_s) // self._step_dt_ns) | ||
|
||
def __call__(self) -> int: | ||
"""Get the current simulated time.""" | ||
return self._start_ns + (self._step * self._step_dt_ns) | ||
|
||
|
||
class TimeKeeper: | ||
"""A simple nanosecond time keeping interface.""" | ||
|
||
def __init__(self, source: TimeSource = _time_ns) -> None: | ||
"""Initialize this instance.""" | ||
|
||
self.source = source | ||
|
||
@contextmanager | ||
def simulated( | ||
self, step_dt_ns: int = 1, start_ns: int = None | ||
) -> Iterator[SimulatedTime]: | ||
"""Take over time resolution with a simulated time instance.""" | ||
|
||
# Use a realistic starting timestamp value if one isn't provided. | ||
if start_ns is None: | ||
start_ns = self() | ||
|
||
sim_time = SimulatedTime(step_dt_ns, start_ns=start_ns) | ||
|
||
# Update 'source' to power simulated time resolution. | ||
orig = self.source | ||
self.source = sim_time | ||
|
||
try: | ||
yield sim_time | ||
finally: | ||
self.source = orig | ||
|
||
def __call__(self) -> int: | ||
"""Get time.""" | ||
return self.source() | ||
|
||
|
||
TIME = TimeKeeper() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters