Skip to content

Commit

Permalink
feature(riot_driver): Initial implementation of riot driver using mak…
Browse files Browse the repository at this point in the history
…e term
  • Loading branch information
MrKevinWeiss committed Jun 24, 2019
1 parent 76ba244 commit 3e8387e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 22 deletions.
4 changes: 2 additions & 2 deletions riot_pal/base_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ def close(self):
"""Closes the device connection."""
self._driver.close()

def _read(self):
def _readline(self):
"""Reads data from the driver.
Returns:
str: string of data if success, driver defined error if failed.
"""
return self._driver.read()
return self._driver.readline()

def _write(self, data):
"""Writes data to the driver.
Expand Down
4 changes: 2 additions & 2 deletions riot_pal/dut_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def send_cmd(self, send_cmd):
result - Either success, error or timeout.
"""
self._write(send_cmd)
response = self._read()
response = self._readline()
cmd_info = {'cmd': send_cmd, 'data': None}
while response != '':
if self.COMMAND in response:
Expand All @@ -66,7 +66,7 @@ def send_cmd(self, send_cmd):
cmd_info['msg'] = clean_msg.replace('\n', '')
cmd_info['result'] = self.RESULT_ERROR
break
response = self._read()
response = self._readline()

if response == '':
cmd_info['result'] = self.RESULT_TIMEOUT
Expand Down
2 changes: 1 addition & 1 deletion riot_pal/ll_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def send_cmd(self, send_cmd, to_byte_array=False):
result - Either success, error or timeout.
"""
self._write(send_cmd)
data = self._read()
data = self._readline()
cmd_info = {'cmd': send_cmd}
if data == "":
cmd_info['msg'] = "Timeout occured"
Expand Down
38 changes: 27 additions & 11 deletions riot_pal/riot_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,41 @@
"""RIOT Driver for RIOT PAL
This module handles generic connection and IO to the make term.
"""
import logging
import os
import pexpect


class RiotDriver:
"""Contains all reusable functions for connecting, sending and recieveing
"""Contains all reusable functions for connecting, sending and receiving
data.
"""

def __init__(self):
raise NotImplementedError()
def __init__(self, timeout=5, path=None):
if path is not None:
os.chdir(path)
self.child = pexpect.spawnu("make term", timeout=timeout,
codec_errors='replace')

def close(self):
"""Close serial connection."""
raise NotImplementedError()
"""Closes the spawned process"""
self.child.close()

def read(self):
"""Read and decode data."""
raise NotImplementedError()
def readline(self):
"""Reads a line from a make term process and strips away all additional
data so only the output of the device is left."""
try:
response = self.child.readline()
response = response.split('# ', 1)[-1]
response = response.replace('\n', '')
response = response.replace('\r', '')
except (ValueError, TypeError, pexpect.TIMEOUT) as exc:
logging.debug(exc)
response = "ERR"
logging.debug("Response: %s", response)
return response

def write(self, data):
"""Tries write data."""
raise NotImplementedError()
"""Tries write data and adds a newline."""
logging.debug("Writing: %s", data)
self.child.write(data + '\n')
4 changes: 2 additions & 2 deletions riot_pal/serial_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs):
# Used to clear the cpu and mcu buffer from startup junk data
time.sleep(0.1)
self.write('')
self.read()
self.readline()

def _connect(self, *args, **kwargs):
if 'timeout' not in kwargs:
Expand Down Expand Up @@ -61,7 +61,7 @@ def close(self):
logging.debug("Closing %s", self._dev.port)
self._dev.close()

def read(self):
def readline(self):
"""Read and decode to utf-8 data.
Returns:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name="riot_pal",
version="0.2.3",
version="0.2.4",
author="RIOT OS",
author_email="[email protected]",
license="MIT",
Expand Down
4 changes: 2 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Testing a package locally
```
pip uninstall riot_pal
pip3 uninstall riot_pal
python3 setup.py sdist
pip install dist/riot_pal-x.x.x.tar.gz
pip3 install dist/riot_pal-x.x.x.tar.gz
```

Upload to pip
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serial_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _open_write_read_close(regtest, *args, **kwrgs):
try:
ser_drvr = SerialDriver(*args, **kwrgs)
ser_drvr.write('')
regtest.write(ser_drvr.read())
regtest.write(ser_drvr.readline())
ser_drvr.close()
regtest.write('SUCCESS\n')
except (SerialException, TypeError) as exc:
Expand Down

0 comments on commit 3e8387e

Please sign in to comment.