import board
import analogio
+# Values for sine wave
+# (data points = 20, amplitude=100, frequency=1)
sine_wave = [
0,
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():
- 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()
# 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()
"""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,
-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"""
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
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):