Skip to content

Commit

Permalink
Updated documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Feb 10, 2019
1 parent 8beb351 commit 9e08917
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
77 changes: 74 additions & 3 deletions docs/usage_host.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,79 @@ API Library
-----------

The API library provides the ``Interface`` class. A class instance is made by
passing the path to a serial device.
passing the path to a serial device to the constructor.

.. code:: python
>>> from simple_rpc import Interface
>>>
>>> interface = Interface('/dev/ttyACM0')
Every exported method will show up as a class method of the ``interface``
class instance. These methods can be used like any normal class methods.
Every exported method will show up as a class method of the ``interface`` class
instance. These methods can be used like any normal class methods.
Alternatively, the exported methods can be called by name using the
``call_method()`` function.

The constructor takes the following parameters.

.. list-table:: Constructor parameters.
:header-rows: 1

* - name
- optional
- description
* - ``device``
- no
- Serial device name.
* - ``baudrate``
- yes
- Baud rate.
* - ``wait``
- yes
- Time in seconds before communication starts.
* - ``autoconnect``
- yes
- Automatically connect.

The following standard methods are available.

.. list-table:: Class methods.
:header-rows: 1

* - name
- description
* - ``open()``
- Connect to serial device.
* - ``close()``
- Disconnect from serial device.
* - ``is_open()``
- Query serial device state.
* - ``call_method()``
- Execute a method.

If the connection should not be made instantly, the ``autoconnect`` parameter
can be used in combination with the ``open()`` function.

.. code:: python
>>> interface = Interface('/dev/ttyACM0', autoconnect=False)
>>> # Do something.
>>> interface.open()
The connection state can be queried using the ``is_open()`` function and it can
be closed using the ``close()`` function.

.. code:: python
>>> if interface.is_open():
>>> interface.close()
Additionally, the ``with`` statement is supported for easy opening and closing.

.. code:: python
>>> with Interface('/dev/ttyACM0') as interface:
>>> interface.version()
Example
^^^^^^^
Expand All @@ -31,6 +94,14 @@ class method of the ``interface`` class instance.
>>> interface.inc(1)
2
Alternatively, the exported method can be called using the ``call_mathod()``
function.

.. code:: python
>>> interface.call_method('inc', 1)
2
To get more information about this class method, the built-in ``help()``
function can be used.

Expand Down
5 changes: 3 additions & 2 deletions simple_rpc/simple_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def _get_methods(self):

def open(self):
"""Connect to serial device."""
if self._connection.isOpen():
if self.is_open():
return

self._connection.port = self._device
Expand All @@ -231,7 +231,7 @@ def open(self):

def close(self):
"""Disconnect from serial device."""
if not self._connection.isOpen():
if not self.is_open():
return

for method in self.methods:
Expand All @@ -243,6 +243,7 @@ def close(self):
self._connection.close()

def is_open(self):
"""Query serial device state."""
return self._connection.isOpen()


Expand Down
6 changes: 5 additions & 1 deletion tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@


class TestLib(object):
def test_open(self):
def test_pre_open(self):
assert not _interface.is_open()
assert _interface.methods == {}

def test_open(self):
_interface.open()
assert _interface.is_open()
assert _interface.methods != {}
Expand Down Expand Up @@ -61,5 +63,7 @@ def test_close(self):
assert _interface.is_open()
assert _interface.methods != {}
_interface.close()

def test_post_close(self):
assert not _interface.is_open()
assert _interface.methods == {}

0 comments on commit 9e08917

Please sign in to comment.