Skip to content
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
62 changes: 62 additions & 0 deletions ev3dev/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import ctypes
import re
import stat
import select
import time
from os.path import abspath
from struct import pack, unpack
from subprocess import Popen
Expand Down Expand Up @@ -314,6 +316,8 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam

# ~autogen

self._poll = None

# ~autogen generic-get-set classes.motor>currentClass

@property
Expand Down Expand Up @@ -806,6 +810,64 @@ def reset(self, **kwargs):

# ~autogen

def wait(self, cond, timeout=None):
"""
Blocks until ``cond(self.state)`` is ``True``. The condition is
checked when there is an I/O event related to the ``state`` attribute.
Exits early when ``timeout`` (in milliseconds) is reached.

Returns ``True`` if the condition is met, and ``False`` if the timeout
is reached.
"""

tic = time.time()

if self._poll is None:
if self._state is None:
self._state = self._attribute_file_open('state')
self._poll = select.poll()
self._poll.register(self._state, select.POLLPRI)

while True:
self._poll.poll(None if timeout is None else timeout)

if timeout is not None and time.time() >= tic + timeout / 1000:
return False

if cond(self.state):
return True


def wait_until(self, s, timeout=None):
"""
Blocks until ``s`` is in ``self.state``. The condition is checked when
there is an I/O event related to the ``state`` attribute. Exits early
when ``timeout`` (in milliseconds) is reached.

Returns ``True`` if the condition is met, and ``False`` if the timeout
is reached.

Example::

m.wait_until('stalled')
"""
return self.wait(lambda state: s in state, timeout)

def wait_while(self, s, timeout=None):
"""
Blocks until ``s`` is not in ``self.state``. The condition is checked
when there is an I/O event related to the ``state`` attribute. Exits
early when ``timeout`` (in milliseconds) is reached.

Returns ``True`` if the condition is met, and ``False`` if the timeout
is reached.

Example::

m.wait_while('running')
"""
return self.wait(lambda state: s not in state, timeout)

def list_motors(name_pattern=Motor.SYSTEM_DEVICE_NAME_CONVENTION, **kwargs):
"""
This is a generator function that enumerates all tacho motors that match
Expand Down