]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py
add square wave generation
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_agnostic_board / pin.py
index cb21e06e29b3a560bd0ed91170bea5214a14833f..fbe74ec056f088af7599bf45fb2a2af0ea1589fa 100644 (file)
@@ -4,6 +4,55 @@
 """generic_agnostic_board pin interface"""
 import random
 
+# Values for sine wave analog output
+# (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 square wave analog output
+# (data points = 20, amplitude=100)
+square_wave_int = [
+    100,
+    -100,
+    100,
+    -100,
+    100,
+    -100,
+    100,
+    -100,
+    100,
+    -100,
+    100,
+    -100,
+    100,
+    -100,
+    100,
+    -100,
+    100,
+    -100,
+    100,
+    -100
+]
 
 class Pin:
     """A basic Pin class for use with generic_agnostic_board"""
@@ -42,12 +91,29 @@ class Pin:
         """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_square_wave(self):
+        """Returns the next value in the square wave"""
+        if self._wave_idx is None:
+            self._wave_idx = 0
+        else:
+            self._wave_idx = (self._wave_idx + 1) % len(square_wave_int)
+        return square_wave_int[self._wave_idx]
+
     def __init__(self, pin_id=None):
         self.id = pin_id
         self._mode = None
         self._pull = None
         self.previous_value = False
         self.current_value = None
+        self._wave_idx = None
 
         # mapping of pin definition names to expected behavior
         self.pin_behavior = {
@@ -55,15 +121,12 @@ 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
-            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
-            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
+            9: self.return_sine_wave,  # Ax_OUTPUT_WAVE_SINE
+            10: self.return_square_wave,  # Ax_OUTPUT_WAVE_SAWTOOTH
+            11: self.return_toggle  # Dx_INPUT_TOGGLE
         }
 
     def init(self, mode=IN, pull=None):
@@ -91,7 +154,6 @@ class Pin:
         # is pin a pull down and pin is HIGH?
         if self._pull == Pin.PULL_DOWN and self.current_value == True:
             self.current_value = False
-
         return self.current_value
 
     def value(self, val=None):
@@ -130,7 +192,6 @@ D1 = Pin(1)
 D2 = Pin(2)
 D3 = Pin(3)
 D4 = Pin(4)
-D5 = Pin(5)
 # Special "digital" pins
 D6 = Pin(6)
 # Analog pins