-
Notifications
You must be signed in to change notification settings - Fork 76
Collapse file tree
Files
Search this repository
/
Copy pathi2c_device.py
More file actions
More file actions
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Latest commit
161 lines (133 loc) · 5.38 KB
/
i2c_device.py
File metadata and controls
161 lines (133 loc) · 5.38 KB
Edit and raw actions
OlderNewer
1
# SPDX-FileCopyrightText: 2016 Scott Shawcroft for Adafruit Industries
2
#
3
# SPDX-License-Identifier: MIT
4
5
"""
6
`adafruit_bus_device.i2c_device` - I2C Bus Device
7
====================================================
8
"""
9
10
__version__ = "0.0.0-auto.0"
11
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git"
12
13
14
class I2CDevice:
15
"""
16
Represents a single I2C device and manages locking the bus and the device
17
address.
18
19
:param ~busio.I2C i2c: The I2C bus the device is on
20
:param int device_address: The 7 bit device address
21
:param bool probe: Probe for the device upon object creation, default is true
22
23
.. note:: This class is **NOT** built into CircuitPython. See
24
:ref:`here for install instructions <bus_device_installation>`.
25
26
Example:
27
28
.. code-block:: python
29
30
import busio
31
from board import *
32
from adafruit_bus_device.i2c_device import I2CDevice
33
34
with busio.I2C(SCL, SDA) as i2c:
35
device = I2CDevice(i2c, 0x70)
36
bytes_read = bytearray(4)
37
with device:
38
device.readinto(bytes_read)
39
# A second transaction
40
with device:
41
device.write(bytes_read)
42
"""
43
44
def __init__(self, i2c, device_address, probe=True):
45
46
self.i2c = i2c
47
self.device_address = device_address
48
49
if probe:
50
self.__probe_for_device()
51
52
def readinto(self, buf, *, start=0, end=None):
53
"""
54
Read into ``buf`` from the device. The number of bytes read will be the
55
length of ``buf``.
56
57
If ``start`` or ``end`` is provided, then the buffer will be sliced
58
as if ``buf[start:end]``. This will not cause an allocation like
59
``buf[start:end]`` will so it saves memory.
60
61
:param bytearray buffer: buffer to write into
62
:param int start: Index to start writing at
63
:param int end: Index to write up to but not include; if None, use ``len(buf)``
64
"""
65
if end is None:
66
end = len(buf)
67
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)
68
69
def write(self, buf, *, start=0, end=None):
70
"""
71
Write the bytes from ``buffer`` to the device, then transmit a stop
72
bit.
73
74
If ``start`` or ``end`` is provided, then the buffer will be sliced
75
as if ``buffer[start:end]``. This will not cause an allocation like
76
``buffer[start:end]`` will so it saves memory.
77
78
:param bytearray buffer: buffer containing the bytes to write
79
:param int start: Index to start writing from
80
:param int end: Index to read up to but not include; if None, use ``len(buf)``
81
"""
82
if end is None:
83
end = len(buf)
84
self.i2c.writeto(self.device_address, buf, start=start, end=end)
85
86
# pylint: disable-msg=too-many-arguments
87
def write_then_readinto(
88
self,
89
out_buffer,
90
in_buffer,
91
*,
92
out_start=0,
93
out_end=None,
94
in_start=0,
95
in_end=None
96
):
97
"""
98
Write the bytes from ``out_buffer`` to the device, then immediately
99
reads into ``in_buffer`` from the device. The number of bytes read
100
will be the length of ``in_buffer``.
101