]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py
Untangle code and remove pylint disables
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_agnostic_board / pin.py
index de6e1811da9ab68d83793f07b926aab964c5c78c..5830c30ef601bf17f6f52b6ef3892c0fdf082b99 100644 (file)
@@ -4,6 +4,56 @@
 """generic_agnostic_board pin interface"""
 import random
 
 """generic_agnostic_board pin interface"""
 import random
 
+# Values for sine wave
+# (data points = 20, amplitude=100, frequency=1)
+sine_wave = [
+    0,
+    31,
+    59,
+    81,
+    95,
+    100,
+    95,
+    81,
+    59,
+    31,
+    0,
+    -31,
+    -59,
+    -81,
+    -95,
+    -100,
+    -95,
+    -81,
+    -59,
+    -31,
+]
+
+# Values for a sawtooth wave
+# (data points = 20, amplitude=100)
+sawtooth_wave = [
+    -100,
+    -80,
+    -60,
+    -40,
+    -20,
+    0,
+    20,
+    40,
+    60,
+    80,
+    -100,
+    -80,
+    -60,
+    -40,
+    -20,
+    0,
+    20,
+    40,
+    60,
+    80,
+]
+
 
 class Pin:
     """A basic Pin class for use with generic_agnostic_board"""
 
 class Pin:
     """A basic Pin class for use with generic_agnostic_board"""
@@ -16,6 +66,12 @@ class Pin:
     # pin values
     LOW = 0
     HIGH = 1
     # pin values
     LOW = 0
     HIGH = 1
+    # pin pulls
+    PULL_NONE = 0
+    PULL_UP = 1
+    PULL_DOWN = 2
+
+    # pylint: disable=no-self-use
 
     def return_toggle(self):
         """Returns the pin's expected value, toggling between True and False"""
 
     def return_toggle(self):
         """Returns the pin's expected value, toggling between True and False"""
@@ -38,11 +94,29 @@ class Pin:
         """Returns the first five digits of Pi, 31415"""
         return 31415
 
         """Returns the first five digits of Pi, 31415"""
         return 31415
 
+    def return_sine_wave(self):
+        """Returns the next value in the sine wave"""
+        if self._wave_idx is None:
+            self._wave_idx = 0
+        else:
+            self._wave_idx = (self._wave_idx + 1) % len(sine_wave)
+        return sine_wave[self._wave_idx]
+
+    def return_sawtooth_wave(self):
+        """Returns the next value in the sawtooth wave"""
+        if self._wave_idx is None:
+            self._wave_idx = 0
+        else:
+            self._wave_idx = (self._wave_idx + 1) % len(sawtooth_wave)
+        return sawtooth_wave[self._wave_idx]
+
     def __init__(self, pin_id=None):
         self.id = pin_id
         self._mode = None
     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
         self.current_value = None
+        self._wave_idx = None
 
         # mapping of pin definition names to expected behavior
         self.pin_behavior = {
 
         # mapping of pin definition names to expected behavior
         self.pin_behavior = {
@@ -50,22 +124,20 @@ class Pin:
             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
             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
+            4: self.return_true,  # Dx_OUTPUT
             7: self.return_random_int,  # Ax_INPUT_RAND_INT
             8: self.return_fixed_int_pi,  # Ax_INPUT_FIXED_INT_PI
             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
-            # Add other mappings as needed
+            9: self.return_sine_wave,  # Ax_INPUT_WAVE_SINE
+            10: self.return_sawtooth_wave,  # Ax_INPUT_WAVE_SAW
+            11: self.return_toggle,  # Dx_INPUT_TOGGLE
         }
 
     def init(self, mode=IN, pull=None):
         """Initialize the Pin"""
         if self.id is None:
             raise RuntimeError("Can not init a None type pin.")
         }
 
     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):
         self._mode = mode
 
     def write(self, new_value):
@@ -78,6 +150,13 @@ class Pin:
         self.previous_value = self.current_value
         # perform a lookup on the pin_behavior dict to get the value
         self.current_value = self.pin_behavior.get(self.id)()
         self.previous_value = self.current_value
         # 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 is False:
+            self.current_value = False
+        # is pin a pull down and pin is HIGH?
+        if self._pull == Pin.PULL_DOWN and self.current_value is True:
+            self.current_value = False
         return self.current_value
 
     def value(self, val=None):
         return self.current_value
 
     def value(self, val=None):
@@ -101,8 +180,8 @@ class Pin:
         # Analog Out
         if self._mode == Pin.DAC:
             if val is None:
         # Analog Out
         if self._mode == Pin.DAC:
             if val is None:
-                # write only
-                raise AttributeError("unreadable attribute")
+                self.previous_value = self.current_value
+                return self.current_value
             self.write(val)
             return None
         raise RuntimeError(
             self.write(val)
             return None
         raise RuntimeError(
@@ -116,7 +195,6 @@ D1 = Pin(1)
 D2 = Pin(2)
 D3 = Pin(3)
 D4 = Pin(4)
 D2 = Pin(2)
 D3 = Pin(3)
 D4 = Pin(4)
-D5 = Pin(5)
 # Special "digital" pins
 D6 = Pin(6)
 # Analog pins
 # Special "digital" pins
 D6 = Pin(6)
 # Analog pins
@@ -124,6 +202,12 @@ A0 = Pin(7)
 A1 = Pin(8)
 A2 = Pin(9)
 A3 = Pin(10)
 A1 = Pin(8)
 A2 = Pin(9)
 A3 = Pin(10)
+A4 = Pin(12)
+
+# Special digital pins for pixels
+D7 = Pin(11)
+D8 = Pin(13)
+D9 = Pin(14)
 
 # I2C pins
 SDA = Pin()
 
 # I2C pins
 SDA = Pin()
@@ -136,6 +220,9 @@ MOSI = Pin()
 MISO = Pin()
 CS = Pin()
 
 MISO = Pin()
 CS = Pin()
 
+spiPorts = ((0, SCK, MOSI, MISO),)
+
+
 # UART pins
 UART_TX = Pin()
 UART_RX = Pin()
 # UART pins
 UART_TX = Pin()
 UART_RX = Pin()