]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/bitbangio.py
Add versioning to the top-level Python files
[Adafruit_Blinka-hackapet.git] / src / bitbangio.py
index 8570620c21b3e92e0e171e2012d62503a1d88ece..e63168fbbf0484e96b6d99acb1474bc2a6a2dab9 100755 (executable)
@@ -7,16 +7,25 @@ See `CircuitPython:bitbangio` in CircuitPython for more details.
 * Author(s): cefn
 """
 
 * Author(s): cefn
 """
 
-from adafruit_blinka import Lockable, agnostic
+
+__version__ = "0.0.0-auto.0"
+__repo__ = "https://github.com/adafruit/Adafruit_Blinka.git"
+
+
 import adafruit_platformdetect.constants.boards as ap_board
 import adafruit_platformdetect.constants.boards as ap_board
+from adafruit_blinka import Lockable, agnostic
+
+# pylint: disable=import-outside-toplevel,too-many-arguments
 
 
 class I2C(Lockable):
 
 
 class I2C(Lockable):
+    """Bitbang/Software I2C implementation"""
+
     def __init__(self, scl, sda, frequency=400000):
         # TODO: This one is a bit questionable:
         if agnostic.board_id == ap_board.PYBOARD:
             raise NotImplementedError("No software I2C on {}".format(agnostic.board_id))
     def __init__(self, scl, sda, frequency=400000):
         # TODO: This one is a bit questionable:
         if agnostic.board_id == ap_board.PYBOARD:
             raise NotImplementedError("No software I2C on {}".format(agnostic.board_id))
-        elif agnostic.detector.board.any_embedded_linux:
+        if agnostic.detector.board.any_embedded_linux:
             # TODO: Attempt to load this library automatically
             raise NotImplementedError(
                 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
             # TODO: Attempt to load this library automatically
             raise NotImplementedError(
                 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
@@ -24,16 +33,19 @@ class I2C(Lockable):
         self.init(scl, sda, frequency)
 
     def init(self, scl, sda, frequency):
         self.init(scl, sda, frequency)
 
     def init(self, scl, sda, frequency):
+        """Initialization"""
         from machine import Pin
         from machine import I2C as _I2C
 
         self.deinit()
         from machine import Pin
         from machine import I2C as _I2C
 
         self.deinit()
-        id = (
+        id = (  # pylint: disable=redefined-builtin
             -1
             -1
-        )  # force bitbanging implementation - in future introspect platform if SDA/SCL matches hardware I2C
+        )  # force bitbanging implementation - in future
+        # introspect platform if SDA/SCL matches hardware I2C
         self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
 
     def deinit(self):
         self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
 
     def deinit(self):
+        """Deinitialization"""
         try:
             del self._i2c
         except AttributeError:
         try:
             del self._i2c
         except AttributeError:
@@ -46,10 +58,12 @@ class I2C(Lockable):
         self.deinit()
 
     def scan(self):
         self.deinit()
 
     def scan(self):
+        """Scan for attached devices"""
         return self._i2c.scan()
 
     def readfrom_into(self, address, buffer, start=0, end=None):
         return self._i2c.scan()
 
     def readfrom_into(self, address, buffer, start=0, end=None):
-        if start is not 0 or end is not None:
+        """Read from a device at specified address into a buffer"""
+        if start != 0 or end is not None:
             if end is None:
                 end = len(buffer)
             buffer = memoryview(buffer)[start:end]
             if end is None:
                 end = len(buffer)
             buffer = memoryview(buffer)[start:end]
@@ -57,39 +71,45 @@ class I2C(Lockable):
         return self._i2c.readfrom_into(address, buffer, stop)
 
     def writeto(self, address, buffer, start=0, end=None, stop=True):
         return self._i2c.readfrom_into(address, buffer, stop)
 
     def writeto(self, address, buffer, start=0, end=None, stop=True):
-        if start is not 0 or end is not None:
+        """Write to a device at specified address from a buffer"""
+        if start != 0 or end is not None:
             if end is None:
                 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
             if end is None:
                 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
-            else:
-                return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
+            return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
         return self._i2c.writeto(address, buffer, stop)
 
 
         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 :(
+# TODO untested, as actually busio.SPI was on
+# tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
 class SPI(Lockable):
 class SPI(Lockable):
+    """Bitbang/Software SPI implementation"""
+
     def __init__(self, clock, MOSI=None, MISO=None):
         if agnostic.detector.board.any_embedded_linux:
             # TODO: Attempt to load this library automatically
             raise NotImplementedError(
                 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
             )
     def __init__(self, clock, MOSI=None, MISO=None):
         if agnostic.detector.board.any_embedded_linux:
             # TODO: Attempt to load this library automatically
             raise NotImplementedError(
                 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
             )
-        from machine import SPI
+        from machine import SPI as _SPI
 
 
-        self._spi = SPI(-1)
+        self._spi = _SPI(-1)
         self._pins = (clock, MOSI, MISO)
 
     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
         self._pins = (clock, MOSI, MISO)
 
     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
-        from machine import SPI, Pin
+        """Update the configuration"""
+        from machine import Pin
+        from machine import SPI as _SPI
 
         if self._locked:
 
         if self._locked:
-            # TODO verify if _spi obj 'caches' sck, mosi, miso to avoid storing in _attributeIds (duplicated in busio)
+            # 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,
             # i.e. #init ignores MOSI=None rather than unsetting
             self._spi.init(
                 baudrate=baudrate,
                 polarity=polarity,
                 phase=phase,
                 bits=bits,
-                firstbit=SPI.MSB,
+                firstbit=_SPI.MSB,
                 sck=Pin(self._pins[0].id),
                 mosi=Pin(self._pins[1].id),
                 miso=Pin(self._pins[2].id),
                 sck=Pin(self._pins[0].id),
                 mosi=Pin(self._pins[1].id),
                 miso=Pin(self._pins[2].id),
@@ -98,10 +118,13 @@ class SPI(Lockable):
             raise RuntimeError("First call try_lock()")
 
     def write(self, buf):
             raise RuntimeError("First call try_lock()")
 
     def write(self, buf):
+        """Write to the SPI device"""
         return self._spi.write(buf)
 
     def readinto(self, buf):
         return self._spi.write(buf)
 
     def readinto(self, buf):
+        """Read from the SPI device into a buffer"""
         return self.readinto(buf)
 
     def write_readinto(self, buffer_out, buffer_in):
         return self.readinto(buf)
 
     def write_readinto(self, buffer_out, buffer_in):
+        """Write to the SPI device and read from the SPI device into a buffer"""
         return self.write_readinto(buffer_out, buffer_in)
         return self.write_readinto(buffer_out, buffer_in)