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