+++ /dev/null
-# About Adafruit_Micropython_Blinka
-
-This repository is structured around integration tests rooted at the `test/src`
-directory, intended to test the compatibility layer rooted in `src`.
-
-The tests offer a procedural way to assert equivalence between 'native' CircuitPython behaviour and behaviour of the **adafruit_blinka** compatibility layer.
-
-The structure of the testing modules permits test suites to be imported and configured selectively on different implementations, platforms and boards (see `adafruit_blinka.agnostic.py` for definitions of these terms).
-
-Automated introspection of the python runtime combines with interactive prompts
-to configure a scenario for testing (e.g. which platform, which board, what is wired to it)
-so the same routines can be carried out on Micropython boards, dual boards running either CircuitPython or Micropython, or dedicated CircuitPython boards.
-
-Typically the tests have first run on a native CircuitPython platform, and are then used to
-prove equivalence on a Micropython platform running the **adafruit_blinka** compatibility layer.
-
-# Tests so far
-
-Tests of compatible versions of **digitalio**, **board** and **microcontroller** have successfully demonstrated
-the same code running on either platform, setting and getting pin values and using pull.
-
-Tests have also proven compatibility of the following unmodified CircuitPython libraries...
-
-* adafruit_bme280
-* adafruit_mma8451
-* adafruit_gps
-
-...which proves the fundamentals of bitbangio.I2C, busio.I2C and busio.UART
-
-# Example
-
-To take a minimal example, the following should assert the default behaviour of the DigitalInOut
-constructor, checks the behaviour of switch_to_input/output(), configures a pin as a pull-up button, a pull-down button and an LED.
-
-```python
-from testing import test_module_name
-test_module_name("testing.universal.digitalio")
-```
-
-Or to take a more involved example of constructing a test suite requiring hardware,
-the following should verify I2C communication with a BME280 module.
-
-```python
-import unittest
-import testing.universal.i2c
-suite = unittest.TestSuite()
-suite.addTest(testing.universal.i2c.TestBME280Interactive)
-runner = unittest.TestRunner()
-runner.run(suite)
-```
-
-
-To prove this on a newly-flashed Feather Huzzah running Micropython 1.9.3,
-it should be possible (on a posix-compliant platform with adafruit_ampy installed)
-to `cd test/scripts` then run `./upload_feather_huzzah_micropython_put.sh` to
-synchronize relevant files to the filesystem of the huzzah, reset the huzzah then
-connect using `screen /dev/ttyUSB0 115200` before running the above commands.
-
-Micropython hosts require a micropython repository alongside
-the Adafruit_Micropython_Blinka repository. For circuitpython,
-the repository is expected to be called circuitpython_2.2.3.
-In each case, the matching version should have been checked out from github
-and `make` needs to have been run in the `mpy-cross` folder. This provides a tool
-to make bytecode-compiled .mpy versions of all .py files before upload so that
-tests can be achieved within the limited memory available on many target platforms.
-
-## Comments
-
-There are reference routines in `test/scripts` like `upload_feather_huzzah_micropython_put.sh` which execute a selective bytecode-compile to .mpy format and an ampy upload for CircuitPython/Micropython on esp8266, or `upload_pyboard_micropython_cp.sh` which selectively bytecode-compiles and synchronizes files with cp to the CIRCUITPY or PYBFLASH disk mount for stm32 and samd21 platforms.
\ No newline at end of file
"""
for key in dir(cls):
val = getattr(cls, key)
- if type(val) is cls:
+ if isinstance(cls, val):
yield (key, val)
class ContextManaged:
+ """An object that automatically deinitializes hardware with a context manager."""
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.deinit()
+ def deinit(self):
+ """Free any hardware used by the object."""
+ pass
+
class Lockable(ContextManaged):
+ """An object that must be locked to prevent collisions on a microcontroller resource."""
_locked = False
def try_lock(self):
+ """Attempt to grab the lock. Return True on success, False if the lock is already taken."""
if self._locked:
return False
- else:
- self._locked = True
- return True
+ self._locked = True
+ return True
def unlock(self):
+ """Release the lock so others may use the resource."""
if self._locked:
self._locked = False
else:
raise ValueError("Not locked")
def patch_system():
+ """Patch modules that may be different due to the platform."""
import sys
from adafruit_blinka.agnostic import time
- sys.modules['time'] = time
\ No newline at end of file
+ sys.modules['time'] = time
import sys
gc.collect()
-try:
- microcontroller = sys.platform
-except:
- microcontroller = None
+
+# We intentionally are patching into this namespace as module names so skip the name check.
+# pylint: disable=invalid-name
+microcontroller = sys.platform
board = None
if microcontroller is not None:
+"""Platform agnostic time implementation"""
+
from adafruit_blinka import agnostic
+
+# We intentionally are patching into this namespace so skip the wildcard check.
+# pylint: disable=unused-wildcard-import,wildcard-import
+
if agnostic.implementation == "circuitpython":
from time import *
elif agnostic.implementation == "micropython":
from utime import sleep
from ucollections import namedtuple
- _struct_time = namedtuple("struct_time", ("tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst"))
+ _struct_time = namedtuple("struct_time", ("tm_year", "tm_mon", "tm_mday",
+ "tm_hour", "tm_min", "tm_sec",
+ "tm_wday", "tm_yday", "tm_isdst"))
- def marshal_time(tm_year, tm_mon, tm_mday, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=-1, tm_yday=-1, tm_isdst=-1):
+ #pylint: disable=too-many-arguments
+ def _marshal_time(tm_year, tm_mon, tm_mday, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=-1,
+ tm_yday=-1, tm_isdst=-1):
+ """Construct struct_time with default values."""
_struct_time(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
- def struct_time(t):
- return marshal_time(*t)
+ def struct_time(time_tuple):
+ """Create a struct_time"""
+ return _marshal_time(*time_tuple)
- total_ms = 0
- prev_ticks_ms = utime.ticks_ms()
+ #pylint: disable=invalid-name
+ _total_ms = 0
+ _prev_ticks_ms = utime.ticks_ms()
def monotonic():
- """
- Assumes that monotonic is called more frequently than the wraparound of micropython's utime.ticks_ms()
- """
- global prev_ticks_ms, total_ms
+ """A monotonically increasing time in seconds. No defined start time."""
+ # Assumes that monotonic is called more frequently than the wraparound of micropython's
+ # utime.ticks_ms()
+ global _prev_ticks_ms, _total_ms #pylint: disable=global-statement
ticks_ms = utime.ticks_ms()
- total_ms += utime.ticks_diff(ticks_ms, prev_ticks_ms)
- prev_ticks_ms = ticks_ms
- return total_ms * 0.001
\ No newline at end of file
+ _total_ms += utime.ticks_diff(ticks_ms, _prev_ticks_ms)
+ _prev_ticks_ms = ticks_ms
+ return _total_ms * 0.001
+"""Feather Huzzah pin names"""
+
from adafruit_blinka.microcontroller.esp8266 import pin
-# TODO need equiv of INPUT_PULL_DOWN_16 ? see https://tttapa.github.io/ESP8266/Chap04%20-%20Microcontroller.html
+# TODO need equiv of INPUT_PULL_DOWN_16 ?
+# See https://tttapa.github.io/ESP8266/Chap04%20-%20Microcontroller.html
GPIO0 = pin.GPIO0
GPIO1 = pin.GPIO1
TX = GPIO1
SDA = GPIO4
-SCL = GPIO5
\ No newline at end of file
+SCL = GPIO5
+"""NodeMCU pin names"""
+
from adafruit_blinka.microcontroller.esp8266 import pin
D0 = pin.GPIO16
TX0 = D10
# GPIO0 and GPIO2 have built-in pull-ups on common ESP8266
-# breakout boards making them suitable for I2C SDA and SCL
\ No newline at end of file
+# breakout boards making them suitable for I2C SDA and SCL
+"""PyBoard pin names"""
+
from adafruit_blinka.microcontroller.stm32 import pin
X1 = pin.A0
+"""ESP8266 pin names"""
+
from microcontroller import Pin
GPIO0 = Pin(0)
TOUT = Pin("TOUT")
# ordered as spiId, sckId, mosiId, misoId
-spiPorts = ((1, GPIO14, GPIO13, GPIO12))
+SPI_PORTS = ((1, GPIO14, GPIO13, GPIO12))
# ordered as uartId, txId, rxId
-uartPorts = (
+UART_PORTS = (
(0, GPIO1, GPIO3),
- # (0, GPIO15, GPIO13) # TODO secondary pins for UART0 configurable from Micropython? How to flag?
+ # TODO secondary pins for UART0 configurable from Micropython? How to flag?
+ # (0, GPIO15, GPIO13)
(1, GPIO2, None))
-i2cPorts = ()
\ No newline at end of file
+I2C_PORTS = ()
+"""STM32 pins"""
+
from microcontroller import Pin
A0 = Pin('A0')
D2 = Pin('D2')
# ordered as spiId, sckId, mosiId, misoId
-spiPorts = ((1, B13, B15, B14), (2, A5, A6, A7))
+SPI_PORTS = ((1, B13, B15, B14), (2, A5, A6, A7))
# ordered as uartId, txId, rxId
-uartPorts = (
+UART_PORTS = (
(1, B6, B7),
(2, A2, A3),
(3, B10, B11),
(6, C6, C7),
)
-i2cPorts = (
+I2C_PORTS = (
(1, B6, B7),
(2, B10, B11),
)
-
-from adafruit_blinka import Enum, agnostic
+"""Microcontroller pins"""
+from adafruit_blinka import Enum, agnostic
class Pin(Enum):
- def __init__(self, id):
+ """Reference Pin object"""
+ def __init__(self, pin_id):
"""Identifier for pin, referencing platform-specific pin id"""
- self.id = id
+ self._id = pin_id
def __repr__(self):
import board
return "microcontroller.pin.{}".format(key)
return repr(self)
+# We intentionally are patching into this namespace so skip the wildcard check.
+# pylint: disable=unused-wildcard-import,wildcard-import
if agnostic.microcontroller == "esp8266":
from adafruit_blinka.microcontroller.esp8266 import *
elif agnostic.microcontroller == "stm32":
from adafruit_blinka.microcontroller.stm32 import *
else:
- raise NotImplementedError("Microcontroller not supported")
\ No newline at end of file
+ raise NotImplementedError("Microcontroller not supported")
+"""Pins named after their chip name."""
+
from adafruit_blinka import agnostic
+# We intentionally are patching into this namespace so skip the wildcard check.
+# pylint: disable=unused-wildcard-import,wildcard-import
+
if agnostic.microcontroller == "esp8266":
from adafruit_blinka.microcontroller.esp8266.pin import *
elif agnostic.microcontroller == "stm32":
from adafruit_blinka.microcontroller.stm32.pin import *
else:
- raise NotImplementedError("Microcontroller not supported")
\ No newline at end of file
+ raise NotImplementedError("Microcontroller not supported")