diff --git a/rapiduino/components/__init__.py b/rapiduino/components/__init__.py index 5692595..e69de29 100644 --- a/rapiduino/components/__init__.py +++ b/rapiduino/components/__init__.py @@ -1,44 +0,0 @@ -import uuid -from abc import ABC, abstractmethod -from typing import Tuple - -from rapiduino.boards.arduino import Arduino -from rapiduino.boards.pins import Pin -from rapiduino.globals.common import PinMode, PinState - - -class BaseComponent(ABC): - def __init__(self, board: Arduino, pins: Tuple[Pin, ...]) -> None: - self._pins = pins - self.__board = board - self.__token = uuid.uuid4().hex - - def connect(self) -> None: - self.__board.register_component(self.__token, self._pins) - self._setup() - - def disconnect(self) -> None: - self.__board.deregister_component(self.__token) - - def _pin_mode(self, pin_no: int, mode: PinMode) -> None: - self.__board.pin_mode(pin_no, mode, self.__token) - - def _digital_read(self, pin_no: int) -> PinState: - return self.__board.digital_read(pin_no, self.__token) - - def _digital_write(self, pin_no: int, state: PinState) -> None: - self.__board.digital_write(pin_no, state, self.__token) - - def _analog_read(self, pin_no: int) -> int: - return self.__board.analog_read(pin_no, self.__token) - - def _analog_write(self, pin_no: int, value: int) -> None: - self.__board.analog_write(pin_no, value, self.__token) - - @abstractmethod - def _setup(self) -> None: - """Implement this method to set the initial pin mode and state. - Always explicitly set the state you want. Do not rely on - Arduino defaults, as this component may be being "hotswapped", - so may be in an unexpected state. - """ diff --git a/rapiduino/components/base.py b/rapiduino/components/base.py new file mode 100644 index 0000000..5692595 --- /dev/null +++ b/rapiduino/components/base.py @@ -0,0 +1,44 @@ +import uuid +from abc import ABC, abstractmethod +from typing import Tuple + +from rapiduino.boards.arduino import Arduino +from rapiduino.boards.pins import Pin +from rapiduino.globals.common import PinMode, PinState + + +class BaseComponent(ABC): + def __init__(self, board: Arduino, pins: Tuple[Pin, ...]) -> None: + self._pins = pins + self.__board = board + self.__token = uuid.uuid4().hex + + def connect(self) -> None: + self.__board.register_component(self.__token, self._pins) + self._setup() + + def disconnect(self) -> None: + self.__board.deregister_component(self.__token) + + def _pin_mode(self, pin_no: int, mode: PinMode) -> None: + self.__board.pin_mode(pin_no, mode, self.__token) + + def _digital_read(self, pin_no: int) -> PinState: + return self.__board.digital_read(pin_no, self.__token) + + def _digital_write(self, pin_no: int, state: PinState) -> None: + self.__board.digital_write(pin_no, state, self.__token) + + def _analog_read(self, pin_no: int) -> int: + return self.__board.analog_read(pin_no, self.__token) + + def _analog_write(self, pin_no: int, value: int) -> None: + self.__board.analog_write(pin_no, value, self.__token) + + @abstractmethod + def _setup(self) -> None: + """Implement this method to set the initial pin mode and state. + Always explicitly set the state you want. Do not rely on + Arduino defaults, as this component may be being "hotswapped", + so may be in an unexpected state. + """ diff --git a/rapiduino/components/led.py b/rapiduino/components/led.py index f4960f4..367dd95 100644 --- a/rapiduino/components/led.py +++ b/rapiduino/components/led.py @@ -1,6 +1,6 @@ from rapiduino.boards.arduino import Arduino from rapiduino.boards.pins import Pin -from rapiduino.components import BaseComponent +from rapiduino.components.base import BaseComponent from rapiduino.globals.common import HIGH, LOW, OUTPUT diff --git a/tests/test_components/test_init.py b/tests/test_components/test_base.py similarity index 97% rename from tests/test_components/test_init.py rename to tests/test_components/test_base.py index 454687e..b7b52d7 100644 --- a/tests/test_components/test_init.py +++ b/tests/test_components/test_base.py @@ -12,7 +12,7 @@ CMD_PINMODE, ) from rapiduino.communication.serial import SerialConnection -from rapiduino.components import BaseComponent +from rapiduino.components.base import BaseComponent from rapiduino.globals.common import HIGH, INPUT DIGITAL_PIN_NUM = 2