]> Repositories - Adafruit_Blinka-hackapet.git/blob - tests/test_generic_agnostic_board_analogio.py
remove tmp file
[Adafruit_Blinka-hackapet.git] / tests / test_generic_agnostic_board_analogio.py
1 # SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 import pytest  # pylint: disable=unused-import
5 import board
6 import analogio
7
8
9 # Analog Outputs
10 def test_Ax_OUTPUT():
11     """Test analog output pin functionality."""
12     assert board.board_id == "OS_AGNOSTIC_BOARD"
13     pin_out = analogio.AnalogOut(board.Ax_OUTPUT)
14
15     # Test boundaries of setting the value and reading it back
16     pin_out.value = 0
17     assert pin_out.value == 0
18     pin_out.value = 65535
19     assert pin_out.value == 65535
20
21     pin_out.deinit()
22
23
24 # Analog Inputs
25
26 # Values for sine wave
27 # (data points = 20, amplitude=100, frequency=1)
28 sine_wave = [
29     0,
30     31,
31     59,
32     81,
33     95,
34     100,
35     95,
36     81,
37     59,
38     31,
39     0,
40     -31,
41     -59,
42     -81,
43     -95,
44     -100,
45     -95,
46     -81,
47     -59,
48     -31,
49 ]
50
51 # Values for a sawtooth wave
52 # (data points = 20, amplitude=100)
53 sawtooth_wave = [
54     -100,
55     -80,
56     -60,
57     -40,
58     -20,
59     0,
60     20,
61     40,
62     60,
63     80,
64     -100,
65     -80,
66     -60,
67     -40,
68     -20,
69     0,
70     20,
71     40,
72     60,
73     80,
74 ]
75
76
77 def test_Ax_INPUT_RAND_INT():
78     """Test random integer from pin Ax_INPUT_RAND_INT"""
79     assert board.board_id == "OS_AGNOSTIC_BOARD"
80     pin_random = analogio.AnalogIn(board.Ax_INPUT_RAND_INT)
81
82     assert isinstance(pin_random.value, int)
83
84     pin_random.deinit()
85
86
87 def test_Ax_INPUT_FIXED_INT_PI():
88     """Test fixed integer from pin Ax_INPUT_FIXED_INT_PI"""
89     assert board.board_id == "OS_AGNOSTIC_BOARD"
90     pin_pi = analogio.AnalogIn(board.Ax_INPUT_FIXED_INT_PI)
91
92     assert pin_pi.value == 31415
93
94     pin_pi.deinit()
95
96
97 def test_Ax_INPUT_WAVE_SINE():
98     """Test sine wave from pin Ax_INPUT_WAVE_SINE"""
99     assert board.board_id == "OS_AGNOSTIC_BOARD"
100     pin_sine_wave = analogio.AnalogIn(board.Ax_INPUT_WAVE_SINE)
101
102     # Run through the sine wave once
103     for expected_value in sine_wave:
104         assert pin_sine_wave.value == expected_value
105
106     # Run through the sine wave again to ensure it loops back correctly
107     for expected_value in sine_wave:
108         assert pin_sine_wave.value == expected_value
109
110     pin_sine_wave.deinit()
111
112
113 def test_Ax_INPUT_WAVE_SAW():
114     """Test sawtooth wave from pin Ax_INPUT_WAVE_SAW"""
115     assert board.board_id == "OS_AGNOSTIC_BOARD"
116     pin_saw_wave = analogio.AnalogIn(board.Ax_INPUT_WAVE_SAW)
117
118     # Run through the sine wave once
119     for expected_value in sawtooth_wave:
120         assert pin_saw_wave.value == expected_value
121
122     # Run through the sine wave again to ensure it loops back correctly
123     for expected_value in sawtooth_wave:
124         assert pin_saw_wave.value == expected_value
125
126     pin_saw_wave.deinit()