]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/mcp2221/pin.py
added support for I2C bus 0,1,2 that where missing for the OPI zero2W implementation
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / mcp2221 / pin.py
index 139d8b6fc3b684d64b9cb8886047937a81c087ca..bf4ba9705065829aa4794578145f20524bb2631f 100644 (file)
@@ -1,24 +1,32 @@
+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""MCP2221 pin names"""
 from .mcp2221 import mcp2221
 
+
 class Pin:
     """A basic Pin class for use with MCP2221."""
 
     # pin modes
-    OUT     = 0
-    IN      = 1
-    ADC     = 2
-    DAC     = 3
+    OUT = 0
+    IN = 1
+    ADC = 2
+    DAC = 3
     # pin values
-    LOW     = 0
-    HIGH    = 1
+    LOW = 0
+    HIGH = 1
 
     def __init__(self, pin_id=None):
         self.id = pin_id
         self._mode = None
 
     def init(self, mode=IN, pull=None):
+        """Initialize the Pin"""
         if self.id is None:
             raise RuntimeError("Can not init a None type pin.")
+        if pull is not None:
+            raise NotImplementedError("Internal pullups and pulldowns not supported")
         if mode in (Pin.IN, Pin.OUT):
             # All pins can do GPIO
             mcp2221.gp_set_mode(self.id, mcp2221.GP_GPIO)
@@ -40,35 +48,36 @@ class Pin:
         self._mode = mode
 
     def value(self, val=None):
+        """Set or return the Pin Value"""
         # Digital In / Out
         if self._mode in (Pin.IN, Pin.OUT):
             # digital read
             if val is None:
                 return mcp2221.gpio_get_pin(self.id)
             # digital write
-            elif val in (Pin.LOW, Pin.HIGH):
+            if val in (Pin.LOW, Pin.HIGH):
                 mcp2221.gpio_set_pin(self.id, val)
+                return None
             # nope
-            else:
-                raise ValueError("Invalid value for pin.")
+            raise ValueError("Invalid value for pin.")
         # Analog In
-        elif self._mode == Pin.ADC:
+        if self._mode == Pin.ADC:
             if val is None:
                 # MCP2221 ADC is 10 bit, scale to 16 bit per CP API
                 return mcp2221.adc_read(self.id) * 64
-            else:
-                # read only
-                raise AttributeError("'AnalogIn' object has no attribute 'value'")
+            # read only
+            raise AttributeError("'AnalogIn' object has no attribute 'value'")
         # Analog Out
-        elif self._mode == Pin.DAC:
+        if self._mode == Pin.DAC:
             if val is None:
                 # write only
                 raise AttributeError("unreadable attribute")
-            else:
-                # scale 16 bit value to MCP2221 5 bit DAC (yes 5 bit)
-                mcp2221.dac_write(self.id, val // 2048)
-        else:
-            raise RuntimeError("No action for mode {} with value {}".format(self._mode, val))
+            # scale 16 bit value to MCP2221 5 bit DAC (yes 5 bit)
+            mcp2221.dac_write(self.id, val // 2048)
+            return None
+        raise RuntimeError(
+            "No action for mode {} with value {}".format(self._mode, val)
+        )
 
 
 # create pin instances for each pin
@@ -78,4 +87,4 @@ G2 = Pin(2)
 G3 = Pin(3)
 
 SCL = Pin()
-SDA = Pin()
\ No newline at end of file
+SDA = Pin()