From: brentru Date: Mon, 20 May 2024 19:29:49 +0000 (-0400) Subject: add sawtooth, tests X-Git-Tag: 8.39.2^2~13 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/22e03b686ec9ff0eb7bd11985d55cb33b525672f add sawtooth, tests --- diff --git a/examples/generic_aio.py b/examples/generic_aio.py index 5b1a033..b33102e 100644 --- a/examples/generic_aio.py +++ b/examples/generic_aio.py @@ -2,6 +2,8 @@ import pytest import board import analogio +# Values for sine wave +# (data points = 20, amplitude=100, frequency=1) sine_wave = [ 0, 31, @@ -25,32 +27,79 @@ sine_wave = [ -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(): - 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(): - 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(): - 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() diff --git a/examples/generic_dio.py b/examples/generic_dio.py index b31b547..98fde58 100644 --- a/examples/generic_dio.py +++ b/examples/generic_dio.py @@ -4,59 +4,74 @@ import digitalio # Digital output pins + 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 + 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(): - 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(): - 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(): - 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(): - 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() diff --git a/src/adafruit_blinka/board/generic_agnostic_board.py b/src/adafruit_blinka/board/generic_agnostic_board.py index 7643aef..ece517d 100644 --- a/src/adafruit_blinka/board/generic_agnostic_board.py +++ b/src/adafruit_blinka/board/generic_agnostic_board.py @@ -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_OUTPUT_WAVE_SAWTOOTH = pin.A3 +Ax_OUTPUT_WAVE_SAW = pin.A3 # I2C pins SDA = pin.SDA diff --git a/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py b/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py index fbe74ec..3ff957d 100644 --- a/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py +++ b/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py @@ -4,7 +4,7 @@ """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, @@ -29,31 +29,32 @@ sine_wave = [ -31, ] -# Values for a square wave analog output +# Values for a sawtooth wave # (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, + -80, + -60, + -40, + -20, + 0, + 20, + 40, + 60, + 80, -100, - 100, - -100 + -80, + -60, + -40, + -20, + 0, + 20, + 40, + 60, + 80, ] + 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] - 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: - 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 @@ -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 - 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):