X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka.git/blobdiff_plain/cbaee77564e37047937a9c642d13f20b53821845..b3f89e94392499446b659e829e666eb0631abbfd:/src/busio.py diff --git a/src/busio.py b/src/busio.py index 9702848..aa9b2e1 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 @@ -26,15 +28,22 @@ class I2C(Lockable): else: from machine import I2C as _I2C from microcontroller.pin import i2cPorts + busnum = None 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) + busnum = portId + 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 +51,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 +106,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 +241,7 @@ class UART(Lockable): ) break else: - raise NotImplementedError( + raise ValueError( "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format((tx, rx), uartPorts) )