]> Repositories - Adafruit_Blinka-hackapet.git/blob - test/src/testing/universal/i2c.py
184fee4021e80925f3772d56d80de17051486f89
[Adafruit_Blinka-hackapet.git] / test / src / testing / universal / i2c.py
1 import gc
2 from testing import yes_no
3
4 gc.collect()
5 from unittest import TestCase
6
7 gc.collect()
8 from testing.board.i2c import I2C
9
10 gc.collect()
11
12
13 class TestBME280Interactive(TestCase):
14     def test_read_value(self):
15
16         import board
17
18         gc.collect()
19         import adafruit_bme280
20
21         gc.collect()
22
23         if not (
24             yes_no("Is BME280 wired to SCL {} SDA {}".format(board.SCL, board.SDA))
25         ):
26             return  # test trivially passed
27
28         i2c = I2C(board.SCL, board.SDA)
29         bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
30         temperature = bme280.temperature
31         humidity = bme280.humidity
32         pressure = bme280.pressure
33         altitude = bme280.altitude
34         self.assertTrue(type(temperature) is float)
35         self.assertTrue(type(humidity) is float)
36         self.assertTrue(type(pressure) is float)
37         self.assertTrue(type(altitude) is float)
38
39         self.assertTrue(-50 <= temperature <= 50)
40         self.assertTrue(0 <= humidity <= 100)
41         self.assertTrue(900 <= pressure <= 1100)
42         self.assertTrue(-1000 <= altitude <= 9, 848)
43
44
45 class TestMMA8451Interactive(TestCase):
46     def test_read_value(self):
47         import math
48
49         gc.collect()
50         import board
51
52         gc.collect()
53
54         if not (
55             yes_no(
56                 "Is MMA8451 wired to SCL {} SDA {} and held still".format(
57                     board.SCL, board.SDA
58                 )
59             )
60         ):
61             return  # test trivially passed
62         # from https://github.com/adafruit/Adafruit_CircuitPython_MMA8451/blob/29e31a0bb836367bc73763b83513105252b7b264/examples/simpletest.py
63         import adafruit_mma8451
64
65         i2c = I2C(board.SCL, board.SDA)
66         sensor = adafruit_mma8451.MMA8451(i2c)
67
68         x, y, z = sensor.acceleration
69         absolute = math.sqrt(x ** 2 + y ** 2 + z ** 2)
70         self.assertTrue(9 <= absolute <= 11, "Not earth gravity")
71
72         orientation = sensor.orientation
73         self.assertTrue(
74             orientation
75             in (
76                 adafruit_mma8451.PL_PUF,
77                 adafruit_mma8451.PL_PUB,
78                 adafruit_mma8451.PL_PDF,
79                 adafruit_mma8451.PL_PDB,
80                 adafruit_mma8451.PL_LRF,
81                 adafruit_mma8451.PL_LRB,
82                 adafruit_mma8451.PL_LLF,
83                 adafruit_mma8451.PL_LLB,
84             )
85         )
86
87
88 class TestBNO055Interactive(TestCase):
89     def test_read_value(self):
90         """
91         Access all sensor values as per
92         https://github.com/adafruit/Adafruit_CircuitPython_BNO055/blob/bdf6ada21e7552c242bc470d4d2619b480b4c574/examples/values.py
93         Note I have not successfully run this test. Possibly a hardware issue with module I have.
94         See https://forums.adafruit.com/viewtopic.php?f=60&t=131665
95         """
96         import board
97
98         gc.collect()
99         import adafruit_bno055
100
101         gc.collect()
102         i2c = I2C(board.SCL, board.SDA)
103         sensor = adafruit_bno055.BNO055(i2c)
104
105         self.assertTrue(9 <= sensor.gravity <= 11)
106         self.assertTrue(sensor.temperature != 0)
107         self.assertTrue(sensor.acceleration != (0, 0, 0))
108         self.assertTrue(sensor.magnetometer != (0, 0, 0))
109         self.assertTrue(sensor.gyroscope != (0, 0, 0))
110         self.assertTrue(sensor.quaternion != (0, 0, 0, 0))
111         sensor.euler
112         sensor.linear_acceleration