You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.1 KiB

  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. # THE SOFTWARE.
  22. """
  23. `adafruit_bus_device.i2c_device` - I2C Bus Device
  24. ====================================================
  25. """
  26. __version__ = "2.2.2"
  27. __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git"
  28. class I2CDevice:
  29. """
  30. Represents a single I2C device and manages locking the bus and the device
  31. address.
  32. :param ~busio.I2C i2c: The I2C bus the device is on
  33. :param int device_address: The 7 bit device address
  34. .. note:: This class is **NOT** built into CircuitPython. See
  35. :ref:`here for install instructions <bus_device_installation>`.
  36. Example:
  37. .. code-block:: python
  38. import busio
  39. from board import *
  40. from adafruit_bus_device.i2c_device import I2CDevice
  41. with busio.I2C(SCL, SDA) as i2c:
  42. device = I2CDevice(i2c, 0x70)
  43. bytes_read = bytearray(4)
  44. with device:
  45. device.readinto(bytes_read)
  46. # A second transaction
  47. with device:
  48. device.write(bytes_read)
  49. """
  50. def __init__(self, i2c, device_address):
  51. # Verify that a deivce with that address exists.
  52. while not i2c.try_lock():
  53. pass
  54. try:
  55. i2c.writeto(device_address, b'')
  56. except OSError:
  57. raise ValueError("No I2C device at address: %x" % device_address)
  58. finally:
  59. i2c.unlock()
  60. self.i2c = i2c
  61. self.device_address = device_address
  62. def readinto(self, buf, **kwargs):
  63. """
  64. Read into ``buf`` from the device. The number of bytes read will be the
  65. length of ``buf``.
  66. If ``start`` or ``end`` is provided, then the buffer will be sliced
  67. as if ``buf[start:end]``. This will not cause an allocation like
  68. ``buf[start:end]`` will so it saves memory.
  69. :param bytearray buffer: buffer to write into
  70. :param int start: Index to start writing at
  71. :param int end: Index to write up to but not include
  72. """
  73. self.i2c.readfrom_into(self.device_address, buf, **kwargs)
  74. def write(self, buf, **kwargs):
  75. """
  76. Write the bytes from ``buffer`` to the device. Transmits a stop bit if
  77. ``stop`` is set.
  78. If ``start`` or ``end`` is provided, then the buffer will be sliced
  79. as if ``buffer[start:end]``. This will not cause an allocation like
  80. ``buffer[start:end]`` will so it saves memory.
  81. :param bytearray buffer: buffer containing the bytes to write
  82. :param int start: Index to start writing from
  83. :param int end: Index to read up to but not include
  84. :param bool stop: If true, output an I2C stop condition after the buffer is written
  85. """
  86. self.i2c.writeto(self.device_address, buf, **kwargs)
  87. def __enter__(self):
  88. while not self.i2c.try_lock():
  89. pass
  90. return self
  91. def __exit__(self, *exc):
  92. self.i2c.unlock()
  93. return False