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