]> Repositories - hackapet/Adafruit_Blinka.git/blobdiff - python/digitalio/__init__.py
Top level package now defines only uart and spi ports. pin submodule handles pins
[hackapet/Adafruit_Blinka.git] / python / digitalio / __init__.py
index 5e42d1720ef6eea7f16d2ccff1085d2286660c87..db8a1a4637beddecbaa00ea543197ab9985ece3e 100644 (file)
@@ -1,60 +1,50 @@
 from machine import Pin
-from mcp import Enum
+from agnostic import board as boardId
+from mcp import Enum,ContextManaged
 
 
 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):
+class DigitalInOut(ContextManaged):
     _pin = None
 
     def __init__(self, pin):
         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
-
-    def __exit__(self):
-        self.deinit()
-
     @property
     def direction(self):
         return self.__direction
@@ -74,12 +64,12 @@ class DigitalInOut(object):
 
     @property
     def value(self):
-        return self._pin.value()
+        return self._pin.value() is 1
 
     @value.setter
     def value(self, val):
         if self.direction is Direction.OUTPUT:
-            self._pin.value(val)
+            self._pin.value(1 if val else 0)
         else:
             raise AttributeError("Not an output")
 
@@ -88,7 +78,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 +87,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")