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