From: Cefn Hoile Date: Mon, 19 Feb 2018 14:01:43 +0000 (+0000) Subject: Initialisations to None help with code completion editors. Context-manager signature... X-Git-Tag: 0.1.0~4^2~86 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/479d57930558f6aee1209bef304ab0d2db00e925 Initialisations to None help with code completion editors. Context-manager signature and return now compliant. Exception-raising where Pull.DOWN not supported. Commented, PEP8 and removed hash cruft. --- diff --git a/python/digitalio/__init__.py b/python/digitalio/__init__.py index 5e42d17..3905e02 100644 --- a/python/digitalio/__init__.py +++ b/python/digitalio/__init__.py @@ -1,30 +1,29 @@ from machine import Pin +from agnostic import board as boardId from mcp import Enum class DriveMode(Enum): - pass - - + PUSH_PULL=None + OPEN_DRAIN=None DriveMode.PUSH_PULL = DriveMode() DriveMode.OPEN_DRAIN = DriveMode() class Direction(Enum): - pass - - + INPUT=None + OUTPUT=None Direction.INPUT = Direction() Direction.OUTPUT = Direction() class Pull(Enum): - pass - - + UP=None + DOWN=None + #NONE=None Pull.UP = Pull() Pull.DOWN = Pull() -Pull.NONE = Pull() +#Pull.NONE = Pull() class DigitalInOut(object): @@ -34,25 +33,22 @@ class DigitalInOut(object): self._pin = Pin(pin.id) self.direction = Direction.INPUT - def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL): - self.direction=Direction.OUTPUT - self.value=value - self.drive_mode=drive_mode - + self.direction = Direction.OUTPUT + self.value = value + self.drive_mode = drive_mode def switch_to_input(self, pull=None): - self.direction=Direction.INPUT - self.pull=pull - + self.direction = Direction.INPUT + self.pull = pull def deinit(self): del self._pin def __enter__(self): - pass + return self - def __exit__(self): + def __exit__(self, exc_type, exc_value, traceback): self.deinit() @property @@ -88,7 +84,7 @@ class DigitalInOut(object): if self.direction is Direction.INPUT: return self.__pull else: - raise AttributeError("Not an input") # + raise AttributeError("Not an input") @pull.setter def pull(self, pul): @@ -97,18 +93,21 @@ class DigitalInOut(object): if pul is Pull.UP: self._pin.init(mode=Pin.IN, pull=Pin.PULL_UP) elif pul is Pull.DOWN: - self._pin.init(mode=Pin.IN, pull=Pin.PULL_DOWN) + if hasattr(Pin, "PULL_DOWN"): + self._pin.init(mode=Pin.IN, pull=Pin.PULL_DOWN) + else: + raise NotImplementedError("{} unsupported on {}".format(Pull.DOWN, boardId)) elif pul is None: self._pin.init(mode=Pin.IN, pull=None) else: - raise AttributeError("Not a Pull")# + raise AttributeError("Not a Pull") else: - raise AttributeError("Not an input") # + raise AttributeError("Not an input") @property def drive_mode(self): if self.direction is Direction.OUTPUT: - return self.__drive_mode# + return self.__drive_mode # else: raise AttributeError("Not an output")