1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/dist/tests/if_lib/base_device.py
Yegor Yefremov ff525eb354 dist/tests/if_lib: move testing infrastructure files to a stand-alone package
Create if_lib package containing all the modules and adapt the *.py files
to import each other using the intra-package references.

The idea behind a package is to invoke test.py either by permanently
modifying PYTHONPATH in user profile via adding path to $RIOTBASE/dist/tests
or make temporary PYTHONPATH changes during the invocation:

PYTHONPATH=$PYTHONPATH:$RIOTBASE/dist/tests python3 test.py

Leave periph_i2c_if.py in the same folder as test.py as this file is
just a Python wrapper around periph specific main.c.

Update BPT memory map. Use definitions generated with the latest code
generator. Both routine names and mapping have changed.

Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com>
2018-08-02 13:34:47 +02:00

75 lines
2.4 KiB
Python

# Copyright (C) 2018 Kevin Weiss <kevin.weiss@haw-hamburg.de>
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
"""@package PyToAPI
This module provice the base interface for a device to a driver.
"""
import logging
from . import driver_manager
class BaseDevice:
"""Instance for devices to connect and untilize drivers."""
def __init__(self, *args, **kwargs):
self._driver = driver_manager.driver_from_config(*args, **kwargs)
def close(self):
"""Closes the device connection."""
self._driver.close()
def _read(self):
"""Reads data from the driver."""
return self._driver.read()
def _write(self, data):
"""Writes data to the driver."""
return self._driver.write(data)
def is_connected_to_board(self):
"""Dummy - confirm if a connection is for target board."""
logging.warning("Check if board is connected: dummy should be"
" implmeneted in subclasses")
raise NotImplementedError()
@classmethod
def from_autodetect(cls, *args, **dev_config):
"""Connects to a range of possible configurations."""
configs = driver_manager.available_configs(*args, **dev_config)
logging.debug("Configs: %r", configs)
for config in configs:
for retry in range(0, 2):
logging.debug("Autodetect attempt: %d", retry)
conn = cls(**config)
try:
if conn.is_connected_to_board():
return conn
except Exception as err:
logging.debug("Cannot connect: %r", err)
conn.close()
raise ValueError("Could not locate board, check if board is"
"connected or is_connected_to_board is correct")
@classmethod
def copy_driver(cls, device):
"""Copies an the driver instance so multiple devices can use one driver."""
logging.debug("Cloning Driver: %r", device._driver)
return cls(dev_type='driver', driver=device._driver)
def main():
"""Tests basic usage of the class
Used for unit testing, information should be confirm with DEBUG info.
"""
logging.getLogger().setLevel(logging.DEBUG)
BaseDevice()
if __name__ == "__main__":
main()