]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
add sawtooth, tests
authorbrentru <brent@adafruit.com>
Mon, 20 May 2024 19:29:49 +0000 (15:29 -0400)
committerbrentru <brent@adafruit.com>
Mon, 20 May 2024 19:29:49 +0000 (15:29 -0400)
examples/generic_aio.py
examples/generic_dio.py
src/adafruit_blinka/board/generic_agnostic_board.py
src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py

index 5b1a0330e6bbebcd8339e295aa5f07f77e67e011..b33102eb2e2a6961af767d576a1f36b493c22eaf 100644 (file)
@@ -2,6 +2,8 @@ import pytest
 import board
 import analogio
 
 import board
 import analogio
 
+# Values for sine wave
+# (data points = 20, amplitude=100, frequency=1)
 sine_wave = [
     0,
     31,
 sine_wave = [
     0,
     31,
@@ -25,32 +27,79 @@ sine_wave = [
     -31,
 ]
 
     -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,
+]
+
+
 def test_ax_input_rand_int():
 def test_ax_input_rand_int():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_random = analogio.AnalogIn(board.Ax_INPUT_RAND_INT)
+    """Test random integer from pin Ax_INPUT_RAND_INT"""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_random = analogio.AnalogIn(board.Ax_INPUT_RAND_INT)
 
 
-  assert isinstance(pin_random.value, int)
+    assert isinstance(pin_random.value, int)
+
+    pin_random.deinit()
 
 
-  pin_random.deinit()
 
 def test_ax_input_fixed_int_pi():
 
 def test_ax_input_fixed_int_pi():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_pi = analogio.AnalogIn(board.Ax_INPUT_FIXED_INT_PI)
+    """Test fixed integer from pin Ax_INPUT_FIXED_INT_PI"""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_pi = analogio.AnalogIn(board.Ax_INPUT_FIXED_INT_PI)
+
+    assert pin_pi.value == 31415
 
 
-  assert pin_pi.value == 31415
+    pin_pi.deinit()
 
 
-  pin_pi.deinit()
 
 def test_ax_input_sine_wave():
 
 def test_ax_input_sine_wave():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_sine_wave = analogio.AnalogIn(board.Ax_OUTPUT_WAVE_SINE)
+    """Test sine wave from pin Ax_OUTPUT_WAVE_SINE"""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_sine_wave = analogio.AnalogIn(board.Ax_OUTPUT_WAVE_SINE)
+
+    # Run through the sine wave once
+    for i in range(len(sine_wave)):
+        assert pin_sine_wave.value == sine_wave[i]
+
+    # Run through the sine wave again to ensure it loops back correctly
+    for i in range(len(sine_wave)):
+        assert pin_sine_wave.value == sine_wave[i]
+
+    pin_sine_wave.deinit()
+
+
+def test_ax_input_saw_wave():
+    """Test sawtooth wave from pin Ax_OUTPUT_WAVE_SAW"""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_sine_wave = analogio.AnalogIn(board.Ax_OUTPUT_WAVE_SAW)
 
 
-  # Run through the sine wave once
-  for i in range(len(sine_wave)):
-    assert pin_sine_wave.value == sine_wave[i]
+    # Run through the sine wave once
+    for i in range(len(sawtooth_wave)):
+        assert pin_sine_wave.value == sawtooth_wave[i]
 
 
-  # Run through the sine wave again to ensure it loops back correctly
-  for i in range(len(sine_wave)):
-    assert pin_sine_wave.value == sine_wave[i]
+    # Run through the sine wave again to ensure it loops back correctly
+    for i in range(len(sawtooth_wave)):
+        assert pin_sine_wave.value == sawtooth_wave[i]
 
 
-  pin_sine_wave.deinit()
+    pin_sine_wave.deinit()
index b31b547ebae68066eb81513cf3f5df07eee345f2..98fde5807f6a96cac368440f8dbfdeaf6303c4ce 100644 (file)
@@ -4,59 +4,74 @@ import digitalio
 
 # Digital output pins
 
 
 # Digital output pins
 
+
 def test_Dx_OUTPUT_TRUE():
 def test_Dx_OUTPUT_TRUE():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_out = digitalio.DigitalInOut(board.Dx_OUTPUT)
-  pin_out.direction = digitalio.Direction.OUTPUT
-  # Test setting the value and reading it back
-  pin_out.value = True
-  assert pin_out.value == True
-  pin_out.value = False
-  assert pin_out.value == True
-  pin_out.deinit()
+    """Test digital output pin functionality."""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_out = digitalio.DigitalInOut(board.Dx_OUTPUT)
+    pin_out.direction = digitalio.Direction.OUTPUT
+    # Test setting the value and reading it back
+    pin_out.value = True
+    assert pin_out.value == True
+    pin_out.value = False
+    assert pin_out.value == True
+    pin_out.deinit()
+
 
 # Digital Input Pins
 
 
 # Digital Input Pins
 
+
 def test_Dx_INPUT_TRUE():
 def test_Dx_INPUT_TRUE():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_true = digitalio.DigitalInOut(board.Dx_INPUT_TRUE)
-  pin_true.direction = digitalio.Direction.INPUT
-  assert pin_true.value == True
-  assert pin_true.value == True # Test subsequent call does not change value
-  pin_true.deinit()
+    """Test digital input pin Dx_INPUT_TRUE."""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_true = digitalio.DigitalInOut(board.Dx_INPUT_TRUE)
+    pin_true.direction = digitalio.Direction.INPUT
+    assert pin_true.value == True
+    assert pin_true.value == True  # Test subsequent call does not change value
+    pin_true.deinit()
+
 
 def test_Dx_INPUT_TRUE_PULL_DOWN():
 
 def test_Dx_INPUT_TRUE_PULL_DOWN():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_true = digitalio.DigitalInOut(board.Dx_INPUT_TRUE)
-  pin_true.direction = digitalio.Direction.INPUT
-  assert pin_true.value == True
-  assert pin_true.value == True # Test subsequent call does not change value
-  pin_true.pull = digitalio.Pull.DOWN
-  assert pin_true.value == False
-  pin_true.deinit()
+    """Test digital input pin Dx_INPUT_TRUE w/pull down."""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_true = digitalio.DigitalInOut(board.Dx_INPUT_TRUE)
+    pin_true.direction = digitalio.Direction.INPUT
+    assert pin_true.value == True
+    assert pin_true.value == True  # Test subsequent call does not change value
+    pin_true.pull = digitalio.Pull.DOWN
+    assert pin_true.value == False
+    pin_true.deinit()
+
 
 def test_Dx_INPUT_FALSE_PULL_UP():
 
 def test_Dx_INPUT_FALSE_PULL_UP():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_false = digitalio.DigitalInOut(board.Dx_INPUT_FALSE)
-  pin_false.direction = digitalio.Direction.INPUT
-  assert pin_false.value == False
-  assert pin_false.value == False # Test subsequent call does not change value
-  pin_false.pull = digitalio.Pull.UP
-  assert pin_false.value == False
-  pin_false.deinit()
+    """Test digital input pin Dx_INPUT_FALSE w/pull up."""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_false = digitalio.DigitalInOut(board.Dx_INPUT_FALSE)
+    pin_false.direction = digitalio.Direction.INPUT
+    assert pin_false.value == False
+    assert pin_false.value == False  # Test subsequent call does not change value
+    pin_false.pull = digitalio.Pull.UP
+    assert pin_false.value == False
+    pin_false.deinit()
+
 
 def test_Dx_INPUT_FALSE():
 
 def test_Dx_INPUT_FALSE():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_false = digitalio.DigitalInOut(board.Dx_INPUT_FALSE)
-  pin_false.direction = digitalio.Direction.INPUT
-  assert pin_false.value == False
-  assert pin_false.value == False # Test subsequent call does not change value
-  pin_false.deinit()
+    """Test digital input pin Dx_INPUT_FALSE"""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_false = digitalio.DigitalInOut(board.Dx_INPUT_FALSE)
+    pin_false.direction = digitalio.Direction.INPUT
+    assert pin_false.value == False
+    assert pin_false.value == False  # Test subsequent call does not change value
+    pin_false.deinit()
+
 
 def test_Dx_INPUT_TOGGLE():
 
 def test_Dx_INPUT_TOGGLE():
-  assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
-  pin_toggle = digitalio.DigitalInOut(board.Dx_INPUT_TOGGLE)
-  pin_toggle.direction = digitalio.Direction.INPUT
-  assert pin_toggle.value == True
-  assert pin_toggle.value == False # Test subsequent call does change value for this pin
-  pin_toggle.deinit()
+    """Test digital input pin Dx_INPUT_TOGGLE"""
+    assert board.board_id == "GENERIC_AGNOSTIC_BOARD"
+    pin_toggle = digitalio.DigitalInOut(board.Dx_INPUT_TOGGLE)
+    pin_toggle.direction = digitalio.Direction.INPUT
+    assert pin_toggle.value == True
+    assert (
+        pin_toggle.value == False
+    )  # Test subsequent call does change value for this pin
+    pin_toggle.deinit()
index 7643aefa9d0b679ff80ea8f16fef325f17b50508..ece517d6b7491930595608e2380fbaf30dfe98f1 100644 (file)
@@ -19,7 +19,7 @@ NEOPIXEL = pin.D6
 Ax_INPUT_RAND_INT = pin.A0
 Ax_INPUT_FIXED_INT_PI = pin.A1
 Ax_OUTPUT_WAVE_SINE = pin.A2
 Ax_INPUT_RAND_INT = pin.A0
 Ax_INPUT_FIXED_INT_PI = pin.A1
 Ax_OUTPUT_WAVE_SINE = pin.A2
-Ax_OUTPUT_WAVE_SAWTOOTH = pin.A3
+Ax_OUTPUT_WAVE_SAW = pin.A3
 
 # I2C pins
 SDA = pin.SDA
 
 # I2C pins
 SDA = pin.SDA
index fbe74ec056f088af7599bf45fb2a2af0ea1589fa..3ff957d3de8bf0e8dc26b3baf843b914e2655968 100644 (file)
@@ -4,7 +4,7 @@
 """generic_agnostic_board pin interface"""
 import random
 
 """generic_agnostic_board pin interface"""
 import random
 
-# Values for sine wave analog output
+# Values for sine wave
 # (data points = 20, amplitude=100, frequency=1)
 sine_wave = [
     0,
 # (data points = 20, amplitude=100, frequency=1)
 sine_wave = [
     0,
@@ -29,31 +29,32 @@ sine_wave = [
     -31,
 ]
 
     -31,
 ]
 
-# Values for a square wave analog output
+# Values for a sawtooth wave
 # (data points = 20, amplitude=100)
 # (data points = 20, amplitude=100)
-square_wave_int = [
-    100,
-    -100,
-    100,
-    -100,
-    100,
-    -100,
-    100,
-    -100,
-    100,
-    -100,
-    100,
-    -100,
-    100,
-    -100,
-    100,
+sawtooth_wave = [
     -100,
     -100,
-    100,
+    -80,
+    -60,
+    -40,
+    -20,
+    0,
+    20,
+    40,
+    60,
+    80,
     -100,
     -100,
-    100,
-    -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"""
 
@@ -99,13 +100,13 @@ class Pin:
             self._wave_idx = (self._wave_idx + 1) % len(sine_wave)
         return sine_wave[self._wave_idx]
 
             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"""
+    def return_sawtooth_wave(self):
+        """Returns the next value in the sawtooth wave"""
         if self._wave_idx is None:
             self._wave_idx = 0
         else:
         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]
+            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
 
     def __init__(self, pin_id=None):
         self.id = pin_id
@@ -125,8 +126,8 @@ class Pin:
             7: self.return_random_int,  # Ax_INPUT_RAND_INT
             8: self.return_fixed_int_pi,  # Ax_INPUT_FIXED_INT_PI
             9: self.return_sine_wave,  # Ax_OUTPUT_WAVE_SINE
             7: self.return_random_int,  # Ax_INPUT_RAND_INT
             8: self.return_fixed_int_pi,  # Ax_INPUT_FIXED_INT_PI
             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
+            10: self.return_sawtooth_wave,  # Ax_OUTPUT_WAVE_SAW
+            11: self.return_toggle,  # Dx_INPUT_TOGGLE
         }
 
     def init(self, mode=IN, pull=None):
         }
 
     def init(self, mode=IN, pull=None):