Skip to content

Files

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Latest commit

Jun 30, 2021
5089bd1 · Jun 30, 2021

History

History
161 lines (133 loc) · 5.38 KB

File metadata and controls

161 lines (133 loc) · 5.38 KB
tannewtladyadaosterwoodpdp7kattnimrmcwethydhalbertevaherradasommersoftphil-shenk
Jan 14, 2021
Jan 14, 2021
1
# SPDX-FileCopyrightText: 2016 Scott Shawcroft for Adafruit Industries
Dec 2, 2016
Dec 2, 2016
2
#
Jan 14, 2021
Jan 14, 2021
3
# SPDX-License-Identifier: MIT
Dec 2, 2016
Dec 2, 2016
4
Dec 8, 2017
Dec 8, 2017
5
"""
Mar 5, 2018
Mar 5, 2018
6
`adafruit_bus_device.i2c_device` - I2C Bus Device
Dec 8, 2017
Dec 8, 2017
7
====================================================
8
"""
9
10
__version__ = "0.0.0-auto.0"
11
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git"
12
Mar 16, 2020
Mar 16, 2020
13
Dec 2, 2016
Dec 2, 2016
14
class I2CDevice:
15
"""
16
Represents a single I2C device and manages locking the bus and the device
17
address.
18
Apr 10, 2017
Apr 10, 2017
19
:param ~busio.I2C i2c: The I2C bus the device is on
Dec 2, 2016
Dec 2, 2016
20
:param int device_address: The 7 bit device address
Nov 26, 2019
Nov 26, 2019
21
:param bool probe: Probe for the device upon object creation, default is true
Dec 2, 2016
Dec 2, 2016
22
Jan 14, 2017
Jan 14, 2017
23
.. note:: This class is **NOT** built into CircuitPython. See
24
:ref:`here for install instructions <bus_device_installation>`.
25
Jan 6, 2017
Jan 6, 2017
26
Example:
27
28
.. code-block:: python
Dec 2, 2016
Dec 2, 2016
29
Apr 10, 2017
Apr 10, 2017
30
import busio
Dec 2, 2016
Dec 2, 2016
31
from board import *
32
from adafruit_bus_device.i2c_device import I2CDevice
33
Apr 10, 2017
Apr 10, 2017
34
with busio.I2C(SCL, SDA) as i2c:
Dec 2, 2016
Dec 2, 2016
35
device = I2CDevice(i2c, 0x70)
36
bytes_read = bytearray(4)
37
with device:
Nov 5, 2017
Nov 5, 2017
38
device.readinto(bytes_read)
Dec 2, 2016
Dec 2, 2016
39
# A second transaction
40
with device:
Apr 10, 2017
Apr 10, 2017
41
device.write(bytes_read)
Dec 2, 2016
Dec 2, 2016
42
"""
Nov 19, 2018
Nov 19, 2018
43
Nov 25, 2019
Nov 25, 2019
44
def __init__(self, i2c, device_address, probe=True):
Nov 26, 2019
Nov 26, 2019
45
Dec 2, 2016
Dec 2, 2016
46
self.i2c = i2c
47
self.device_address = device_address
48
Nov 26, 2019
Nov 26, 2019
49
if probe:
50
self.__probe_for_device()
51
Feb 11, 2020
Feb 11, 2020
52
def readinto(self, buf, *, start=0, end=None):
Dec 2, 2016
Dec 2, 2016
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
Jan 6, 2017
Jan 6, 2017
61
:param bytearray buffer: buffer to write into
62
:param int start: Index to start writing at
Feb 19, 2020
Feb 19, 2020
63
:param int end: Index to write up to but not include; if None, use ``len(buf)``
Dec 2, 2016
Dec 2, 2016
64
"""
Feb 19, 2020
Feb 19, 2020
65
if end is None:
66
end = len(buf)
Feb 11, 2020
Feb 11, 2020
67
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)
Dec 2, 2016
Dec 2, 2016
68
Jul 26, 2020
Jul 26, 2020
69
def write(self, buf, *, start=0, end=None):
Dec 2, 2016
Dec 2, 2016
70
"""
Jul 26, 2020
Jul 26, 2020
71
Write the bytes from ``buffer`` to the device, then transmit a stop
72
bit.
Dec 2, 2016
Dec 2, 2016
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
Jan 6, 2017
Jan 6, 2017
78
:param bytearray buffer: buffer containing the bytes to write
79
:param int start: Index to start writing from
Feb 19, 2020
Feb 19, 2020
80
:param int end: Index to read up to but not include; if None, use ``len(buf)``
Dec 2, 2016
Dec 2, 2016
81
"""
Feb 19, 2020
Feb 19, 2020
82
if end is None:
83
end = len(buf)
Jul 26, 2020
Jul 26, 2020
84
self.i2c.writeto(self.device_address, buf, start=start, end=end)
Dec 2, 2016
Dec 2, 2016
85
Mar 16, 2020
Mar 16, 2020
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,
Jul 27, 2020
Jul 27, 2020
95
in_end=None
Mar 16, 2020
Mar 16, 2020
96
):
Aug 17, 2018
Aug 17, 2018
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