3939
4040
4141//| class I2CDevice:
42- //| """Two wire serial protocol"""
43- //|
44- //| def __init__(self, i2c, device_address, probe=True) -> None:
45- //|
46- //| ...
47- //|
42+ //| """
43+ //| Represents a single I2C device and manages locking the bus and the device
44+ //| address.
45+ //| :param ~busio.I2C i2c: The I2C bus the device is on
46+ //| :param int device_address: The 7 bit device address
47+ //| :param bool probe: Probe for the device upon object creation, default is true
48+ //| .. note:: This class is **NOT** built into CircuitPython. See
49+ //| :ref:`here for install instructions <bus_device_installation>`.
50+ //| Example:
51+ //| .. code-block:: python
52+ //| import busio
53+ //| from board import *
54+ //| from adafruit_bus_device.i2c_device import I2CDevice
55+ //| with busio.I2C(SCL, SDA) as i2c:
56+ //| device = I2CDevice(i2c, 0x70)
57+ //| bytes_read = bytearray(4)
58+ //| with device:
59+ //| device.readinto(bytes_read)
60+ //| # A second transaction
61+ //| with device:
62+ //| device.write(bytes_read)"""
63+ //| ...
64+ //|
4865STATIC mp_obj_t busdevice_i2cdevice_make_new (const mp_obj_type_t * type , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
4966 busdevice_i2cdevice_obj_t * self = m_new_obj (busdevice_i2cdevice_obj_t );
5067 self -> base .type = & busdevice_i2cdevice_type ;
@@ -67,28 +84,30 @@ STATIC mp_obj_t busdevice_i2cdevice_make_new(const mp_obj_type_t *type, size_t n
6784 return (mp_obj_t )self ;
6885}
6986
70- //| def __enter__(self) -> None:
71- //| """Automatically initializes the hardware on context exit. See FIX
72- //| :ref:`lifetime-and-contextmanagers` for more info."""
73- //| ...
74- //|
7587STATIC mp_obj_t busdevice_i2cdevice_obj___enter__ (mp_obj_t self_in ) {
7688 common_hal_busdevice_i2cdevice_lock (self_in );
7789 return self_in ;
7890}
7991STATIC MP_DEFINE_CONST_FUN_OBJ_1 (busdevice_i2cdevice___enter___obj , busdevice_i2cdevice_obj___enter__ );
8092
81- //| def __exit__(self) -> None:
82- //| """Automatically deinitializes the hardware on context exit. See
83- //| :ref:`lifetime-and-contextmanagers` for more info."""
84- //| ...
85- //|
8693STATIC mp_obj_t busdevice_i2cdevice_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
8794 common_hal_busdevice_i2cdevice_unlock (args [0 ]);
8895 return mp_const_none ;
8996}
9097STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (busdevice_i2cdevice___exit___obj , 4 , 4 , busdevice_i2cdevice_obj___exit__ );
9198
99+ //| def readinto(self, buf, *, start=0, end=None):
100+ //| """
101+ //| Read into ``buf`` from the device. The number of bytes read will be the
102+ //| length of ``buf``.
103+ //| If ``start`` or ``end`` is provided, then the buffer will be sliced
104+ //| as if ``buf[start:end]``. This will not cause an allocation like
105+ //| ``buf[start:end]`` will so it saves memory.
106+ //| :param bytearray buffer: buffer to write into
107+ //| :param int start: Index to start writing at
108+ //| :param int end: Index to write up to but not include; if None, use ``len(buf)``"""
109+ //| ...
110+ //|
92111STATIC void readinto (busdevice_i2cdevice_obj_t * self , mp_obj_t buffer , int32_t start , mp_int_t end ) {
93112 mp_buffer_info_t bufinfo ;
94113 mp_get_buffer_raise (buffer , & bufinfo , MP_BUFFER_WRITE );
@@ -123,7 +142,19 @@ STATIC mp_obj_t busdevice_i2cdevice_readinto(size_t n_args, const mp_obj_t *pos_
123142}
124143STATIC MP_DEFINE_CONST_FUN_OBJ_KW (busdevice_i2cdevice_readinto_obj , 2 , busdevice_i2cdevice_readinto );
125144
126-
145+ //| def write(self, buf, *, start=0, end=None):
146+ //| """
147+ //| Write the bytes from ``buffer`` to the device, then transmit a stop
148+ //| bit.
149+ //| If ``start`` or ``end`` is provided, then the buffer will be sliced
150+ //| as if ``buffer[start:end]``. This will not cause an allocation like
151+ //| ``buffer[start:end]`` will so it saves memory.
152+ //| :param bytearray buffer: buffer containing the bytes to write
153+ //| :param int start: Index to start writing from
154+ //| :param int end: Index to read up to but not include; if None, use ``len(buf)``
155+ //| """
156+ //| ...
157+ //|
127158STATIC void write (busdevice_i2cdevice_obj_t * self , mp_obj_t buffer , int32_t start , mp_int_t end ) {
128159 mp_buffer_info_t bufinfo ;
129160 mp_get_buffer_raise (buffer , & bufinfo , MP_BUFFER_READ );
@@ -158,32 +189,28 @@ STATIC mp_obj_t busdevice_i2cdevice_write(size_t n_args, const mp_obj_t *pos_arg
158189MP_DEFINE_CONST_FUN_OBJ_KW (busdevice_i2cdevice_write_obj , 2 , busdevice_i2cdevice_write );
159190
160191
161- /*STATIC void write_then_readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t out_buffer, mp_obj_t in_buffer,
162- int32_t out_start, mp_int_t out_end, int32_t in_start, mp_int_t in_end) {
163- mp_buffer_info_t out_bufinfo;
164- mp_get_buffer_raise(out_buffer, &out_bufinfo, MP_BUFFER_READ);
165-
166- size_t out_length = out_bufinfo.len;
167- normalize_buffer_bounds(&out_start, out_end, &out_length);
168- if (out_length == 0) {
169- mp_raise_ValueError(translate("Buffer must be at least length 1"));
170- }
171-
172- mp_buffer_info_t in_bufinfo;
173- mp_get_buffer_raise(in_buffer, &in_bufinfo, MP_BUFFER_WRITE);
174-
175- size_t in_length = in_bufinfo.len;
176- normalize_buffer_bounds(&in_start, in_end, &in_length);
177- if (in_length == 0) {
178- mp_raise_ValueError(translate("Buffer must be at least length 1"));
179- }
180-
181- uint8_t status = common_hal_busdevice_i2cdevice_write_then_readinto(self, out_buffer, in_buffer, out_length, in_length);
182- if (status != 0) {
183- mp_raise_OSError(status);
184- }
185- }*/
186-
192+ //| def write_then_readinto(self, out_buffer, in_buffer, *, out_start=0, out_end=None, in_start=0, in_end=None):
193+ //| """
194+ //| Write the bytes from ``out_buffer`` to the device, then immediately
195+ //| reads into ``in_buffer`` from the device. The number of bytes read
196+ //| will be the length of ``in_buffer``.
197+ //| If ``out_start`` or ``out_end`` is provided, then the output buffer
198+ //| will be sliced as if ``out_buffer[out_start:out_end]``. This will
199+ //| not cause an allocation like ``buffer[out_start:out_end]`` will so
200+ //| it saves memory.
201+ //| If ``in_start`` or ``in_end`` is provided, then the input buffer
202+ //| will be sliced as if ``in_buffer[in_start:in_end]``. This will not
203+ //| cause an allocation like ``in_buffer[in_start:in_end]`` will so
204+ //| it saves memory.
205+ //| :param bytearray out_buffer: buffer containing the bytes to write
206+ //| :param bytearray in_buffer: buffer containing the bytes to read into
207+ //| :param int out_start: Index to start writing from
208+ //| :param int out_end: Index to read up to but not include; if None, use ``len(out_buffer)``
209+ //| :param int in_start: Index to start writing at
210+ //| :param int in_end: Index to write up to but not include; if None, use ``len(in_buffer)``
211+ //| """
212+ //| ...
213+ //|
187214STATIC mp_obj_t busdevice_i2cdevice_write_then_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
188215 enum { ARG_out_buffer , ARG_in_buffer , ARG_out_start , ARG_out_end , ARG_in_start , ARG_in_end };
189216 static const mp_arg_t allowed_args [] = {
@@ -207,29 +234,17 @@ STATIC mp_obj_t busdevice_i2cdevice_write_then_readinto(size_t n_args, const mp_
207234}
208235MP_DEFINE_CONST_FUN_OBJ_KW (busdevice_i2cdevice_write_then_readinto_obj , 3 , busdevice_i2cdevice_write_then_readinto );
209236
210-
237+ //| def __probe_for_device(self):
238+ //| """
239+ //| Try to read a byte from an address,
240+ //| if you get an OSError it means the device is not there
241+ //| or that the device does not support these means of probing
242+ //| """
243+ //| ...
244+ //|
211245STATIC mp_obj_t busdevice_i2cdevice___probe_for_device (mp_obj_t self_in ) {
212246 busdevice_i2cdevice_obj_t * self = self_in ;
213247 common_hal_busdevice_i2cdevice___probe_for_device (self );
214-
215- /* common_hal_busdevice_i2cdevice_lock(self);
216-
217-
218- //uint8_t buffer;
219- mp_buffer_info_t bufinfo;
220- //mp_obj_t bufobj = MP_OBJ_FROM_PTR(&buffer)
221- mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
222-
223- mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
224-
225- uint8_t status = common_hal_busdevice_i2cdevice_readinto(self_in, (uint8_t*)bufinfo.buf, 1);
226- if (status != 0) {
227- common_hal_busdevice_i2cdevice_unlock(self_in);
228- mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
229- }
230-
231- common_hal_busdevice_i2cdevice_unlock(self);
232- */
233248 return mp_const_none ;
234249}
235250MP_DEFINE_CONST_FUN_OBJ_1 (busdevice_i2cdevice___probe_for_device_obj , busdevice_i2cdevice___probe_for_device );
0 commit comments