Browse Source

Definition of I2C bus-type per-board. Segregated busio tests into i2c vs uart

master^2
Cefn Hoile 3 years ago
parent
commit
546aa0533b
3 changed files with 91 additions and 38 deletions
  1. +7
    -0
      test/src/testing/board/i2c.py
  2. +46
    -38
      test/src/testing/implementation/all/i2c.py
  3. +38
    -0
      test/src/testing/implementation/all/uart.py

+ 7
- 0
test/src/testing/board/i2c.py View File

@ -0,0 +1,7 @@
from adafruit_blinka import agnostic
if agnostic.board in ("feather_m0_express", "feather_huzzah"):
from bitbangio import I2C
elif agnostic.board == "pyboard":
from busio import I2C
else:
raise NotImplementedError("Board not supported")

test/src/testing/implementation/all/busio.py → test/src/testing/implementation/all/i2c.py View File

@ -1,23 +1,55 @@
from unittest import TestCase
import gc
from testing import yes_no
gc.collect()
from unittest import TestCase
gc.collect()
from testing.board.i2c import I2C
gc.collect()
class TestBME280Interactive(TestCase):
def test_read_value(self):
if not(yes_no("Is BME280 wired to SCL {} SDA {}".format(board.SCL, board.SDA))):
return # test trivially passed
import board
gc.collect()
import adafruit_bme280
gc.collect()
i2c = I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
temperature = bme280.temperature
humidity = bme280.humidity
pressure = bme280.pressure
altitude = bme280.altitude
self.assertTrue(type(temperature) is float )
self.assertTrue(type(humidity) is float )
self.assertTrue(type(pressure) is float )
self.assertTrue(type(altitude) is float )
self.assertTrue( -50 <= temperature <= 50)
self.assertTrue( 0 <= humidity <= 100)
self.assertTrue( 900 <= pressure <= 1100)
self.assertTrue( -1000 <= altitude <= 9,848)
class TestMMA8451Interactive(TestCase):
def test_read_value(self):
import math
gc.collect()
import board
gc.collect()
if not(yes_no("Is MMA8451 wired to SCL {} SDA {} and held still".format(board.SCL, board.SDA))):
return # test trivially passed
import busio
# from https://github.com/adafruit/Adafruit_CircuitPython_MMA8451/blob/29e31a0bb836367bc73763b83513105252b7b264/examples/simpletest.py
import adafruit_mma8451
# Initialize I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)
i2c = I2C(board.SCL, board.SDA)
sensor = adafruit_mma8451.MMA8451(i2c)
x, y, z = sensor.acceleration
absolute = math.sqrt(x**2, y**2, z**2)
absolute = math.sqrt(x**2 + y**2 + z**2)
self.assertTrue(9 <=absolute <= 11, "Not earth gravity")
orientation = sensor.orientation
@ -40,41 +72,17 @@ class TestBNO055Interactive(TestCase):
https://github.com/adafruit/Adafruit_CircuitPython_BNO055/blob/bdf6ada21e7552c242bc470d4d2619b480b4c574/examples/values.py
"""
import board
import busio
gc.collect()
import adafruit_bno055
i2c = busio.I2C(board.SCL, board.SDA)
gc.collect()
i2c = I2C(board.SCL, board.SDA)
sensor = adafruit_bno055.BNO055(i2c)
sensor.temperature
sensor.accelerometer
sensor.magnetometer
sensor.gyroscope
self.assertTrue(9 <= sensor.gravity <= 11)
self.assertTrue(sensor.temperature != 0)
self.assertTrue(sensor.acceleration != (0,0,0))
self.assertTrue(sensor.magnetometer != (0,0,0))
self.assertTrue(sensor.gyroscope != (0,0,0))
self.assertTrue(sensor.quaternion != (0,0,0,0))
sensor.euler
sensor.quaternion
sensor.linear_acceleration
sensor.gravity
class TestGPSInteractive(TestCase):
def test_read_value(self):
import microcontroller
import busio
import adafruit_gps
# configure the last available UART (first uart often for REPL)
uartId, txId, rxId = microcontroller.uartPorts[-1]
txPin = microcontroller.Pin(txId)
rxPin = microcontroller.Pin(rxId)
uart = busio.UART(txPin, rxPin, baudrate=9600, timeout=3000)
gps = adafruit_gps.GPS(uart)
gps.send_command('PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
gps.send_command('PMTK220,1000')
gps.update()
self.awaitTrue("GPS fix", lambda: gps.has_fix)
self.assertTrue(gps.satellites is not None)
self.assertTrue(-90 <= gps.latitude < 90)
self.assertTrue(-180 <= gps.longitude < 180)

+ 38
- 0
test/src/testing/implementation/all/uart.py View File

@ -0,0 +1,38 @@
import gc
from unittest import TestCase
from testing import await_true
gc.collect()
class TestGPSInteractive(TestCase):
def test_read_value(self):
import adafruit_blinka
adafruit_blinka.patch_system() # needed before adafruit_gps imports time
import microcontroller.pin
gc.collect()
import busio
gc.collect()
import adafruit_gps
gc.collect()
# configure the last available UART (first uart often for REPL)
uartId, uartTx, uartRx = microcontroller.pin.uartPorts[0]
uart = busio.UART(uartTx, uartRx, baudrate=9600, timeout=3000)
gps = adafruit_gps.GPS(uart)
gps.send_command('PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
gps.send_command('PMTK220,1000')
def try_fix():
gps.update()
return gps.has_fix
await_true("GPS fix", try_fix)
self.assertTrue(gps.satellites is not None)
self.assertTrue(-90 <= gps.latitude < 90)
self.assertTrue(-180 <= gps.longitude < 180)

Loading…
Cancel
Save