From: Cefn Hoile Date: Sat, 24 Feb 2018 21:03:45 +0000 (+0000) Subject: Moved locking behaviour into common superclass. Moved context-managed behaviour into... X-Git-Tag: 0.1.0~4^2~46 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/ef9e1aa6b026f4f309c00f10002382701e9a2c1c Moved locking behaviour into common superclass. Moved context-managed behaviour into common superclass. Implemented bitbangio.SPI busio.SPI, busio.I2C, busio.UART --- diff --git a/python/bitbangio/__init__.py b/python/bitbangio/__init__.py index 31350bf..8276d61 100644 --- a/python/bitbangio/__init__.py +++ b/python/bitbangio/__init__.py @@ -1,9 +1,11 @@ +from mcp import Lockable from machine import I2C as _I2C from machine import Pin +import agnostic -class I2C: +class I2C(Lockable): def __init__(self, scl, sda, frequency=400000): - self._locked = False + super().__init() self.init(scl, sda, frequency) def init(self, scl, sda, frequency): @@ -26,19 +28,6 @@ class I2C: def scan(self): return self._i2c.scan() - def try_lock(self): - if self._locked: - return False - else: - self._locked=True - return True - - def unlock(self): - if self._locked: - self._locked = False - else: - raise ValueError("Not locked") - def readfrom_into(self, address, buffer, start=0, end=None): if start is not 0 or end is not None: if end is None: @@ -53,3 +42,26 @@ class I2C: end = len(buffer) buffer = memoryview(buffer)[start:end] return self._i2c.writeto(address, buffer, stop) + +# TODO untested, as actually busio.SPI was on tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :( +class SPI(Lockable): + def __init__(self, clock, MOSI=None, MISO=None): + self._spi = SPI(-1) + self._pins = (clock.id, MOSI.id, MISO.id) + + def configure(self, baudrate=100000, polarity=0, phase=0, bits=8): + if self._locked: + # TODO verify if _spi obj 'caches' sck, mosi, miso to avoid storing in _attributeIds (duplicated in busio) + # i.e. #init ignores MOSI=None rather than unsetting + self._spi.init(baudrate=baudrate, polarity=polarity, phase=phase, bits = bits, firstbit = SPI.MSB, sck = Pin(self._pins[0]), mosi=Pin(self._pins[1]), miso=Pin(self._pins[2])) + else: + raise RuntimeError("First call try_lock()") + + def write(self, buf): + return self._spi.write(buf) + + def readinto(self, buf): + return self.readinto(buf) + + def write_readinto(self, buffer_out, buffer_in) + return self.write_readinto(buffer_out, buffer_in) diff --git a/python/digitalio/__init__.py b/python/digitalio/__init__.py index e90513d..db8a1a4 100644 --- a/python/digitalio/__init__.py +++ b/python/digitalio/__init__.py @@ -1,6 +1,6 @@ from machine import Pin from agnostic import board as boardId -from mcp import Enum +from mcp import Enum,ContextManaged class DriveMode(Enum): @@ -26,7 +26,7 @@ Pull.DOWN = Pull() #Pull.NONE = Pull() -class DigitalInOut(object): +class DigitalInOut(ContextManaged): _pin = None def __init__(self, pin): @@ -45,12 +45,6 @@ class DigitalInOut(object): def deinit(self): del self._pin - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_value, traceback): - self.deinit() - @property def direction(self): return self.__direction diff --git a/python/mcp/__init__.py b/python/mcp/__init__.py index 2ee42c2..0e52a3c 100644 --- a/python/mcp/__init__.py +++ b/python/mcp/__init__.py @@ -30,3 +30,25 @@ class Enum(object): yield (key, val) +class ContextManaged: + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.deinit() + +def Lockable(ContextManaged): + _locked = False + + def try_lock(self): + if self._locked: + return False + else: + self._locked=True + return True + + def unlock(self): + if self._locked: + self._locked = False + else: + raise ValueError("Not locked")