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