From 546aa0533b3270fb8922fc8fb1bc2419b5a7ed39 Mon Sep 17 00:00:00 2001 From: Cefn Hoile Date: Mon, 26 Feb 2018 19:53:07 +0000 Subject: [PATCH] Definition of I2C bus-type per-board. Segregated busio tests into i2c vs uart --- test/src/testing/board/i2c.py | 7 ++ .../implementation/all/{busio.py => i2c.py} | 84 ++++++++++--------- test/src/testing/implementation/all/uart.py | 38 +++++++++ 3 files changed, 91 insertions(+), 38 deletions(-) create mode 100644 test/src/testing/board/i2c.py rename test/src/testing/implementation/all/{busio.py => i2c.py} (50%) create mode 100644 test/src/testing/implementation/all/uart.py diff --git a/test/src/testing/board/i2c.py b/test/src/testing/board/i2c.py new file mode 100644 index 0000000..0109560 --- /dev/null +++ b/test/src/testing/board/i2c.py @@ -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") \ No newline at end of file diff --git a/test/src/testing/implementation/all/busio.py b/test/src/testing/implementation/all/i2c.py similarity index 50% rename from test/src/testing/implementation/all/busio.py rename to test/src/testing/implementation/all/i2c.py index 099499e..0a2d29c 100644 --- a/test/src/testing/implementation/all/busio.py +++ b/test/src/testing/implementation/all/i2c.py @@ -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) \ No newline at end of file diff --git a/test/src/testing/implementation/all/uart.py b/test/src/testing/implementation/all/uart.py new file mode 100644 index 0000000..05a917f --- /dev/null +++ b/test/src/testing/implementation/all/uart.py @@ -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) + -- 2.49.0