Skip to content

Changing adafruit_bus_device to duck typing #3936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/8086_commander/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ CIRCUITPY_GAMEPAD = 1
CIRCUITPY_BUSDEVICE = 1

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_HID
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD
#FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_ADXL34x
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CIRCUITPY_COUNTIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
MICROPY_PY_ASYNC_AWAIT = 0

SUPEROPT_GC = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ SUPEROPT_GC = 0
CFLAGS_INLINE_LIMIT = 55

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_HID
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ CFLAGS_INLINE_LIMIT = 50


# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ SUPEROPT_GC = 0
CFLAGS_INLINE_LIMIT = 55

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ CFLAGS_INLINE_LIMIT = 50
CIRCUITPY_MSGPACK = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
Expand Down
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ CFLAGS_INLINE_LIMIT = 35
SUPEROPT_GC = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM69
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ CFLAGS_INLINE_LIMIT = 35
SUPEROPT_GC = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM9x
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ CFLAGS_INLINE_LIMIT = 55
SUPEROPT_GC = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

Expand Down
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/pycubed/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ CIRCUITPY_GAMEPAD = 0
CIRCUITPY_RGBMATRIX = 0
CIRCUITPY_PS2IO = 0

FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Register
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/pycubed_mram/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ CIRCUITPY_GAMEPAD = 0
CIRCUITPY_RGBMATRIX = 0
CIRCUITPY_PS2IO = 0

FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Register
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/sam32/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ LONGINT_IMPL = MPZ
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_USTACK = 1

FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/serpente/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LONGINT_IMPL = NONE
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_BUSDEVICE = 0

SUPEROPT_GC = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LONGINT_IMPL = MPZ
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0

FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_DotStar

Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/ugame10/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CIRCUITPY_RTC = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_USB_HID = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_BUSDEVICE = 0

FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/ugame10

Expand Down
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/xinabox_cc03/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ CIRCUITPY_TOUCHIO=0
CIRCUITPY_BUSDEVICE=1

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/xinabox_cs11/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ CIRCUITPY_COUNTIO=0
CIRCUITPY_BUSDEVICE=1

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD
1 change: 1 addition & 0 deletions ports/nrf/boards/pca10100/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CIRCUITPY_RTC = 1
CIRCUITPY_SDCARDIO = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_BUSDEVICE = 0
MICROPY_PY_ASYNC_AWAIT = 0

SUPEROPT_GC = 0
Expand Down
1 change: 1 addition & 0 deletions ports/stm/boards/espruino_pico/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ LD_FILE = boards/STM32F401xd_fs.ld
# lto for this port, and if other stuff hasn't been added in the
# meantime
CIRCUITPY_ULAB = 0
CIRCUITPY_BUSDEVICE = 0

SUPEROPT_GC = 0
2 changes: 1 addition & 1 deletion py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ CFLAGS += -DCIRCUITPY_BLEIO=$(CIRCUITPY_BLEIO)
CIRCUITPY_BOARD ?= 1
CFLAGS += -DCIRCUITPY_BOARD=$(CIRCUITPY_BOARD)

CIRCUITPY_BUSDEVICE = 0
CIRCUITPY_BUSDEVICE ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_BUSDEVICE=$(CIRCUITPY_BUSDEVICE)

CIRCUITPY_BUILTINS_POW3 ?= $(CIRCUITPY_FULL_BUILD)
Expand Down
81 changes: 40 additions & 41 deletions shared-bindings/adafruit_bus_device/I2CDevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_make_new(const mp_obj_type_t *type
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

busio_i2c_obj_t* i2c = args[ARG_i2c].u_obj;
mp_obj_t* i2c = args[ARG_i2c].u_obj;

common_hal_adafruit_bus_device_i2cdevice_construct(MP_OBJ_TO_PTR(self), i2c, args[ARG_device_address].u_int);
if (args[ARG_probe].u_bool == true) {
Expand Down Expand Up @@ -107,7 +107,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_obj___exit__(size_t n_args, const
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_i2cdevice___exit___obj, 4, 4, adafruit_bus_device_i2cdevice_obj___exit__);

//| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: int = 0) -> None:
//| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
//| """Read into ``buf`` from the device. The number of bytes read will be the
//| length of ``buf``.
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
Expand All @@ -118,22 +118,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_i2cdevice___exit_
//| :param int end: Index to write up to but not include; if None, use ``len(buf)``"""
//| ...
//|
STATIC void readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t start, mp_int_t end) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);

size_t length = bufinfo.len;
normalize_buffer_bounds(&start, end, &length);
if (length == 0) {
mp_raise_ValueError(translate("Buffer must be at least length 1"));
}

uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length);
if (status != 0) {
mp_raise_OSError(status);
}
}

STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
Expand All @@ -147,12 +131,22 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_o
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

readinto(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int);
mp_obj_t dest[8];
mp_load_method(self->i2c, MP_QSTR_readfrom_into, dest);
dest[2] = mp_obj_new_int_from_ull(self->device_address);
dest[3] = args[ARG_buffer].u_obj;
//dest[4] = mp_obj_new_str("start", 5);
dest[4] = MP_OBJ_NEW_QSTR(MP_QSTR_start);
dest[5] = mp_obj_new_int(args[ARG_start].u_int);
dest[6] = MP_OBJ_NEW_QSTR(MP_QSTR_end);
dest[7] = mp_obj_new_int(args[ARG_end].u_int);
mp_call_method_n_kw(2, 2, dest);

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2, adafruit_bus_device_i2cdevice_readinto);

//| def write(self, buf: ReadableBuffer, *, start: int = 0, end: int = 0) -> None:
//| def write(self, buf: ReadableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
//| """Write the bytes from ``buffer`` to the device, then transmit a stop bit.
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
//| as if ``buffer[start:end]``. This will not cause an allocation like
Expand All @@ -163,22 +157,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2,
//| """
//| ...
//|
STATIC void write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t start, mp_int_t end, bool transmit_stop_bit) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);

size_t length = bufinfo.len;
normalize_buffer_bounds(&start, end, &length);
if (length == 0) {
mp_raise_ValueError(translate("Buffer must be at least length 1"));
}

uint8_t status = common_hal_adafruit_bus_device_i2cdevice_write(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length, transmit_stop_bit);
if (status != 0) {
mp_raise_OSError(status);
}
}

STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
Expand All @@ -191,13 +169,22 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

write(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int, true);
mp_obj_t dest[8];
mp_load_method(self->i2c, MP_QSTR_writeto, dest);
dest[2] = mp_obj_new_int_from_ull(self->device_address);
dest[3] = args[ARG_buffer].u_obj;
dest[4] = MP_OBJ_NEW_QSTR(MP_QSTR_start);
dest[5] = mp_obj_new_int(args[ARG_start].u_int);
dest[6] = MP_OBJ_NEW_QSTR(MP_QSTR_end);
dest[7] = mp_obj_new_int(args[ARG_end].u_int);
mp_call_method_n_kw(2, 2, dest);

return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_write_obj, 2, adafruit_bus_device_i2cdevice_write);


//| def write_then_readinto(self, out_buffer: WriteableBuffer, in_buffer: ReadableBuffer, *, out_start: int = 0, out_end: int = 0, in_start: int = 0, in_end: int = 0) -> None:
//| def write_then_readinto(self, out_buffer: WriteableBuffer, in_buffer: ReadableBuffer, *, out_start: int = 0, out_end: Optional[int] = None, in_start: int = 0, in_end: Optional[int] = None) -> None:
//| """Write the bytes from ``out_buffer`` to the device, then immediately
//| reads into ``in_buffer`` from the device. The number of bytes read
//| will be the length of ``in_buffer``.
Expand Down Expand Up @@ -233,9 +220,21 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write_then_readinto(size_t n_args,
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

write(self, args[ARG_out_buffer].u_obj, args[ARG_out_start].u_int, args[ARG_out_end].u_int, false);

readinto(self, args[ARG_in_buffer].u_obj, args[ARG_in_start].u_int, args[ARG_in_end].u_int);
mp_obj_t dest[13];
mp_load_method(self->i2c, MP_QSTR_writeto_then_readfrom, dest);
dest[2] = mp_obj_new_int_from_ull(self->device_address);
dest[3] = args[ARG_out_buffer].u_obj;
dest[4] = args[ARG_in_buffer].u_obj;
dest[5] = MP_OBJ_NEW_QSTR(MP_QSTR_out_start);
dest[6] = mp_obj_new_int(args[ARG_out_start].u_int);
dest[7] = MP_OBJ_NEW_QSTR(MP_QSTR_out_end);
dest[8] = mp_obj_new_int(args[ARG_out_end].u_int);
dest[9] = MP_OBJ_NEW_QSTR(MP_QSTR_in_start);
dest[10] = mp_obj_new_int(args[ARG_in_start].u_int);
dest[11] = MP_OBJ_NEW_QSTR(MP_QSTR_in_end);
dest[12] = mp_obj_new_int(args[ARG_in_end].u_int);

mp_call_method_n_kw(3, 4, dest);

return mp_const_none;
}
Expand Down
4 changes: 1 addition & 3 deletions shared-bindings/adafruit_bus_device/I2CDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
extern const mp_obj_type_t adafruit_bus_device_i2cdevice_type;

// Initializes the hardware peripheral.
extern void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address);
extern uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
extern uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length, bool transmit_stop_bit);
extern void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t *i2c, uint8_t device_address);
extern void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self);
extern void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self);
extern void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self);
Expand Down
50 changes: 31 additions & 19 deletions shared-module/adafruit_bus_device/I2CDevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,60 @@
#include "py/runtime.h"
#include "lib/utils/interrupt_char.h"

void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address) {
void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t *i2c, uint8_t device_address) {
self->i2c = i2c;
self->device_address = device_address;
}

void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self) {
bool success = common_hal_busio_i2c_try_lock(self->i2c);
mp_obj_t dest[2];
mp_load_method(self->i2c, MP_QSTR_try_lock, dest);

while (!success) {
mp_obj_t success = mp_call_method_n_kw(0, 0, dest);

while (!mp_obj_is_true(success)) {
RUN_BACKGROUND_TASKS;
if (mp_hal_is_interrupted()) {
break;
}

success = common_hal_busio_i2c_try_lock(self->i2c);
success = mp_call_method_n_kw(0, 0, dest);
}
}

void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self) {
common_hal_busio_i2c_unlock(self->i2c);
}

uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length) {
return common_hal_busio_i2c_read(self->i2c, self->device_address, buffer, length);
}

uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length, bool transmit_stop_bit) {
return common_hal_busio_i2c_write(self->i2c, self->device_address, buffer, length, transmit_stop_bit);
mp_obj_t dest[2];
mp_load_method(self->i2c, MP_QSTR_unlock, dest);
mp_call_method_n_kw(0, 0, dest);
}

void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self) {
common_hal_adafruit_bus_device_i2cdevice_lock(self);

mp_buffer_info_t bufinfo;
mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
mp_buffer_info_t write_bufinfo;
mp_obj_t write_buffer = mp_obj_new_bytearray_of_zeros(0);
mp_get_buffer_raise(write_buffer, &write_bufinfo, MP_BUFFER_READ);

mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
mp_obj_t dest[4];

uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(self, (uint8_t*)bufinfo.buf, 1);
if (status != 0) {
/* catch exceptions that may be thrown while probing for the device */
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
mp_load_method(self->i2c, MP_QSTR_writeto, dest);
dest[2] = mp_obj_new_int_from_ull(self->device_address);
dest[3] = write_buffer;
mp_call_method_n_kw(2, 0, dest);
nlr_pop();
} else {
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);

if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t*)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
}
else {
/* In case we receive an unrelated exception pass it up */
nlr_raise(MP_OBJ_FROM_PTR(nlr.ret_val));
}
}

common_hal_adafruit_bus_device_i2cdevice_unlock(self);
Expand Down
2 changes: 1 addition & 1 deletion shared-module/adafruit_bus_device/I2CDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

typedef struct {
mp_obj_base_t base;
busio_i2c_obj_t *i2c;
mp_obj_t *i2c;
uint8_t device_address;
} adafruit_bus_device_i2cdevice_obj_t;

Expand Down