Repository navigation
Add seconds and elapsed checks to stopwatch #667
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
118a975
Add seconds and elapsed checks to stopwatch
WasabiFan 7f1e4d2
Update ev3dev2/stopwatch.py
WasabiFan ef13fb2
Update ev3dev2/stopwatch.py
WasabiFan 1ffbe43
Merge branch 'ev3dev-stretch' into feature/pure-stopwatch
WasabiFan 038e0b4
Improve behavior or restart and add new reset
WasabiFan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,76 +9,131 @@ | |
| else: | ||
| import datetime as dt | ||
|
|
||
|
|
||
| def get_ticks_ms(): | ||
| if is_micropython(): | ||
| return utime.ticks_ms() | ||
| else: | ||
| return int(dt.datetime.timestamp(dt.datetime.now()) * 1000) | ||
|
|
||
| class StopWatchAlreadyStartedException(Exception): | ||
| """ | ||
| Exception raised when start() is called on a StopWatch which was already start()ed and not yet | ||
| stopped. | ||
| """ | ||
| pass | ||
|
|
||
| class StopWatch(object): | ||
|
|
||
| """ | ||
| A timer class which lets you start timing and then check the amount of time | ||
| elapsed. | ||
| """ | ||
| def __init__(self, desc=None): | ||
| """ | ||
| Initializes the StopWatch but does not start it. | ||
|
|
||
| desc: | ||
| A string description to print when stringifying. | ||
| """ | ||
| self.desc = desc | ||
| self._value = 0 | ||
| self.start_time = None | ||
| self.prev_update_time = None | ||
| self._start_time = None | ||
| self._stopped_total_time = None | ||
|
|
||
| def __str__(self): | ||
| if self.desc is not None: | ||
| return self.desc | ||
| else: | ||
| return self.__class__.__name__ | ||
| name = self.desc if self.desc is not None else self.__class__.__name__ | ||
| return "{}: {}".format(name, self.hms_str) | ||
|
|
||
| def start(self): | ||
| assert self.start_time is None, "%s is already running" % self | ||
| self.start_time = get_ticks_ms() | ||
|
|
||
| def update(self): | ||
|
|
||
| if self.start_time is None: | ||
| return | ||
|
|
||
| current_time = get_ticks_ms() | ||
| """ | ||
| Starts the timer. If the timer is already running, resets it. | ||
|
|
||
| if self.prev_update_time is None: | ||
| delta = current_time - self.start_time | ||
| else: | ||
| delta = current_time - self.prev_update_time | ||
| Raises a :py:class:`ev3dev2.stopwatch.StopWatchAlreadyStartedException` if already started. | ||
| """ | ||
| if self.is_started: | ||
| raise StopWatchAlreadyStartedException() | ||
|
|
||
| self._value += delta | ||
| self.prev_update_time = current_time | ||
| self._stopped_total_time = None | ||
| self._start_time = get_ticks_ms() | ||
|
|
||
| def stop(self): | ||
|
|
||
| if self.start_time is None: | ||
| """ | ||
| Stops the timer. The time value of this Stopwatch is paused and will not continue increasing. | ||
| """ | ||
| if self._start_time is None: | ||
| return | ||
|
|
||
| self.update() | ||
| self.start_time = None | ||
| self.prev_update_time = None | ||
| self._stopped_total_time = get_ticks_ms() - self._start_time | ||
| self._start_time = None | ||
|
|
||
| def reset(self): | ||
| self.stop() | ||
| self._value = 0 | ||
| """ | ||
| Resets the timer and leaves it stopped. | ||
| """ | ||
| self._start_time = None | ||
| self._stopped_total_time = None | ||
|
|
||
| def restart(self): | ||
| """ | ||
| Resets and then starts the timer. | ||
| """ | ||
| self.reset() | ||
| self.start() | ||
|
dwalton76 marked this conversation as resolved.
Resolved
|
||
|
|
||
| @property | ||
| def is_started(self): | ||
| """ | ||
| True if the StopWatch has been started but not stoped (i.e., it's currently running), false otherwise. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| """ | ||
| return self._start_time is not None | ||
|
|
||
| @property | ||
| def value_ms(self): | ||
| """ | ||
| Returns the value of the stopwatch in milliseconds | ||
| """ | ||
| self.update() | ||
| return self._value | ||
| if self._stopped_total_time is not None: | ||
| return self._stopped_total_time | ||
|
|
||
| return get_ticks_ms() - self._start_time if self._start_time is not None else 0 | ||
|
|
||
| @property | ||
| def value_secs(self): | ||
| """ | ||
| Returns the value of the stopwatch in seconds | ||
| """ | ||
| return self.value_ms / 1000 | ||
|
|
||
| @property | ||
| def value_hms(self): | ||
| """ | ||
| Returns the value of the stopwatch in HH:MM:SS.msec format | ||
| Returns this StopWatch's elapsed time as a tuple | ||
| ``(hours, minutes, seconds, milliseconds)``. | ||
| """ | ||
| self.update() | ||
| (hours, x) = divmod(int(self._value), 3600000) | ||
| (hours, x) = divmod(int(self.value_ms), 3600000) | ||
| (mins, x) = divmod(x, 60000) | ||
| (secs, x) = divmod(x, 1000) | ||
| return hours, mins, secs, x | ||
|
|
||
| @property | ||
| def hms_str(self): | ||
| """ | ||
| Returns the stringified value of the stopwatch in HH:MM:SS.msec format | ||
| """ | ||
| return '%02d:%02d:%02d.%03d' % self.value_hms | ||
|
|
||
| def is_elapsed_ms(self, duration_ms): | ||
| """ | ||
| Returns True if this timer has measured at least ``duration_ms`` | ||
| milliseconds. | ||
| Otherwise, returns False. If ``duration_ms`` is None, returns False. | ||
| """ | ||
|
|
||
| return duration_ms is not None and self.value_ms >= duration_ms | ||
|
|
||
| def is_elapsed_secs(self, duration_secs): | ||
| """ | ||
| Returns True if this timer has measured at least ``duration_secs`` seconds. | ||
| Otherwise, returns False. If ``duration_secs`` is None, returns False. | ||
| """ | ||
|
|
||
| return duration_secs is not None and self.value_secs >= duration_secs | ||
|
|
||
| return '%02d:%02d:%02d.%03d' % (hours, mins, secs, x) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.