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