X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka.git/blobdiff_plain/cbaee77564e37047937a9c642d13f20b53821845..a8662a122f5985fc07bac4046d450dcb5e1854f6:/src/busio.py diff --git a/src/busio.py b/src/busio.py index 9702848..ede1e6c 100755 --- a/src/busio.py +++ b/src/busio.py @@ -7,6 +7,8 @@ See `CircuitPython:busio` in CircuitPython for more details. * Author(s): cefn """ +import threading + from adafruit_blinka import Enum, Lockable, agnostic from adafruit_blinka.agnostic import board_id, detector import adafruit_platformdetect.board as ap_board @@ -27,14 +29,19 @@ class I2C(Lockable): from machine import I2C as _I2C from microcontroller.pin import i2cPorts for portId, portScl, portSda in i2cPorts: - if scl == portScl and sda == portSda: - self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency) - break + try: + if scl == portScl and sda == portSda: + self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency) + break + except RuntimeError: + pass else: - raise NotImplementedError( + raise ValueError( "No Hardware I2C on (scl,sda)={}\nValid I2C ports: {}".format((scl, sda), i2cPorts) ) + self._lock = threading.RLock() + def deinit(self): try: del self._i2c @@ -42,9 +49,11 @@ class I2C(Lockable): pass def __enter__(self): + self._lock.acquire() return self def __exit__(self, exc_type, exc_value, traceback): + self._lock.release() self.deinit() def scan(self): @@ -95,7 +104,7 @@ class SPI(Lockable): self._pins = (portSck, portMosi, portMiso) break else: - raise NotImplementedError( + raise ValueError( "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}". format((clock, MOSI, MISO), spiPorts)) @@ -230,7 +239,7 @@ class UART(Lockable): ) break else: - raise NotImplementedError( + raise ValueError( "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format((tx, rx), uartPorts) )