]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py
add pull modes
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_agnostic_board / pin.py
index 77eb4b0f60bfbdc88d95a007cd3fb743ad7e8a5d..1f6818e64c750c1a9e85984cbe718cad42a2daae 100644 (file)
@@ -16,6 +16,10 @@ class Pin:
     # pin values
     LOW = 0
     HIGH = 1
+    # pin pulls
+    PULL_NONE = 0
+    PULL_UP = 1
+    PULL_DOWN = 2
 
     def return_toggle(self):
         """Returns the pin's expected value, toggling between True and False"""
@@ -41,41 +45,33 @@ class Pin:
     def __init__(self, pin_id=None):
         self.id = pin_id
         self._mode = None
-        self.previous_value = None
+        self._pull = None
+        self.previous_value = False
         self.current_value = None
-        # TODO: Can we simplify the pin behavior dict and the pin map dict?
+
         # mapping of pin definition names to expected behavior
-        self.expected_pin_behavior = {
-            "Dx_INPUT_TRUE": self.return_true,
-            "Dx_INPUT_FALSE": self.return_false,
-            "Dx_INPUT_TRUE_THEN_FALSE": self.return_toggle,
-            "Dx_INPUT_TRUE_PULL_UP": self.return_true,
-            "Dx_INPUT_TRUE_PULL_DOWN": self.return_true,
-            "Dx_OUTPUT_TRUE": self.return_true,
-            "Dx_OUTPUT_FALSE": self.return_false,
-            "Ax_INPUT_RAND_INT": self.return_random_int,
-        }
-        # mapping of pin numbers to pin definition names
-        self.pin_number_to_pin_definition_name = {
-            0: "Dx_INPUT_TRUE",
-            1: "Dx_INPUT_FALSE",
-            2: "Dx_INPUT_TRUE_PULL_UP",
-            3: "Dx_INPUT_TRUE_PULL_DOWN",
-            4: "Dx_OUTPUT_TRUE",
-            5: "Dx_OUTPUT_FALSE",
-            6: "NEOPIXEL",
-            7: "Ax_INPUT_RAND_INT",
-            8: "Ax_INPUT_FIXED_INT_PI",
-            9: "Ax_OUTPUT_WAVE_SINE",
-            10: "Ax_OUTPUT_WAVE_SAWTOOTH",
+        self.pin_behavior = {
+            0: self.return_true,  # Dx_INPUT_TRUE
+            1: self.return_false,  # Dx_INPUT_FALSE
+            2: self.return_true,  # Dx_INPUT_TRUE_PULL_UP
+            3: self.return_true,  # Dx_INPUT_TRUE_PULL_DOWN
+            4: self.return_true,  # Dx_OUTPUT_TRUE
+            5: self.return_false,  # Dx_OUTPUT_FALSE
+            6: self.return_true,  # NEOPIXEL
+            7: self.return_random_int,  # Ax_INPUT_RAND_INT
+            8: self.return_fixed_int_pi,  # Ax_INPUT_FIXED_INT_PI
+            9: self.return_true,  # Ax_OUTPUT_WAVE_SINE
+            10: self.return_true,  # Ax_OUTPUT_WAVE_SAWTOOTH
+            11: self.return_toggle,  # Dx_INPUT_TOGGLE
+            # Add other mappings as needed
         }
 
     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")
+        pull = Pin.PULL_NONE if pull is None else pull
+        self._pull = pull
         self._mode = mode
 
     def write(self, new_value):
@@ -84,14 +80,20 @@ class Pin:
         self.current_value = new_value
 
     def read(self):
+        print("\nread mode: ", self._mode)
         """Returns the pin's expected value."""
         self.previous_value = self.current_value
-        # lookup the pin id's name from the mapping
-        pin_name = self.pin_number_to_pin_definition_name.get(self.id)
-        if pin_name:
-            self.current_value = self.expected_pin_behavior[pin_name]()
-        else:  # default to False if the pin is not in the mapping
+        # perform a lookup on the pin_behavior dict to get the value
+        self.current_value = self.pin_behavior.get(self.id)()
+
+        # is pin a pull up and pin is LOW?
+        if self._pull == Pin.PULL_UP and self.current_value == False:
+            self.current_value = True
+        # is pin a pull down and pin is HIGH?
+        if self._pull == Pin.PULL_DOWN and self.current_value == True:
+            print("switching PDR to False")
             self.current_value = False
+
         return self.current_value
 
     def value(self, val=None):
@@ -139,6 +141,8 @@ A1 = Pin(8)
 A2 = Pin(9)
 A3 = Pin(10)
 
+D7 = Pin(11)
+
 # I2C pins
 SDA = Pin()
 SCL = Pin()