Skip to content

Commit

Permalink
Move BaseComponent into base.py
Browse files Browse the repository at this point in the history
  • Loading branch information
samwedge committed Feb 4, 2021
1 parent ebf2abe commit c8bd164
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 46 deletions.
44 changes: 0 additions & 44 deletions rapiduino/components/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
"""
44 changes: 44 additions & 0 deletions rapiduino/components/base.py
Original file line number Diff line number Diff line change
@@ -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.
"""
2 changes: 1 addition & 1 deletion rapiduino/components/led.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c8bd164

Please sign in to comment.