Skip to content

Commit a2ac2da

Browse files
authored
Merge pull request #3936 from gamblor21/busdevice_fixes
Changing adafruit_bus_device to duck typing
2 parents c075e89 + 41b9196 commit a2ac2da

File tree

24 files changed

+80
-78
lines changed

24 files changed

+80
-78
lines changed

ports/atmel-samd/boards/8086_commander/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ CIRCUITPY_GAMEPAD = 1
2222
CIRCUITPY_BUSDEVICE = 1
2323

2424
# Include these Python libraries in firmware.
25-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
2625
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_HID
2726
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD
2827
#FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_ADXL34x

ports/atmel-samd/boards/circuitbrains_basic_m0/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CIRCUITPY_COUNTIO = 0
1616
CIRCUITPY_FREQUENCYIO = 0
1717
CIRCUITPY_I2CPERIPHERAL = 0
1818
CIRCUITPY_VECTORIO = 0
19+
CIRCUITPY_BUSDEVICE = 0
1920
MICROPY_PY_ASYNC_AWAIT = 0
2021

2122
SUPEROPT_GC = 0

ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ SUPEROPT_GC = 0
2222
CFLAGS_INLINE_LIMIT = 55
2323

2424
# Include these Python libraries in firmware.
25-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
2625
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
2726
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_HID
2827
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH

ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ CFLAGS_INLINE_LIMIT = 40
2626

2727

2828
# Include these Python libraries in firmware.
29-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
3029
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
3130
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit
3231
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH

ports/atmel-samd/boards/circuitplayground_express_displayio/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ SUPEROPT_GC = 0
2727
CFLAGS_INLINE_LIMIT = 55
2828

2929
# Include these Python libraries in firmware.
30-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
3130
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
3231
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
3332
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ CFLAGS_INLINE_LIMIT = 50
2121
CIRCUITPY_MSGPACK = 0
2222

2323
# Include these Python libraries in firmware.
24-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
2524
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit
2625
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor
2726
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ CFLAGS_INLINE_LIMIT = 35
2828
SUPEROPT_GC = 0
2929

3030
# Include these Python libraries in firmware.
31-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
3231
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM69

ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@ CFLAGS_INLINE_LIMIT = 35
2929
SUPEROPT_GC = 0
3030

3131
# Include these Python libraries in firmware.
32-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
3332
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM9x

ports/atmel-samd/boards/hallowing_m0_express/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ CFLAGS_INLINE_LIMIT = 55
2727
SUPEROPT_GC = 0
2828

2929
# Include these Python libraries in firmware.
30-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
3130
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
3231
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
3332

ports/atmel-samd/boards/pycubed/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ CIRCUITPY_GAMEPAD = 0
2121
CIRCUITPY_RGBMATRIX = 0
2222
CIRCUITPY_PS2IO = 0
2323

24-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
2524
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
2625
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Register
2726
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD

ports/atmel-samd/boards/pycubed_mram/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ CIRCUITPY_GAMEPAD = 0
2121
CIRCUITPY_RGBMATRIX = 0
2222
CIRCUITPY_PS2IO = 0
2323

24-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
2524
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
2625
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Register
2726
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD

ports/atmel-samd/boards/sam32/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ LONGINT_IMPL = MPZ
1313
CIRCUITPY_AUDIOBUSIO = 0
1414
CIRCUITPY_USTACK = 1
1515

16-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
1716
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

ports/atmel-samd/boards/serpente/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LONGINT_IMPL = NONE
1414
CIRCUITPY_AUDIOBUSIO = 0
1515
CIRCUITPY_FREQUENCYIO = 0
1616
CIRCUITPY_GAMEPAD = 0
17+
CIRCUITPY_BUSDEVICE = 0
1718

1819
SUPEROPT_GC = 0
1920

ports/atmel-samd/boards/sparkfun_lumidrive/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LONGINT_IMPL = MPZ
1414
CIRCUITPY_AUDIOIO = 0
1515
CIRCUITPY_AUDIOBUSIO = 0
1616
CIRCUITPY_VECTORIO = 0
17+
CIRCUITPY_BUSDEVICE = 0
1718

1819
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_DotStar
1920

ports/atmel-samd/boards/ugame10/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CIRCUITPY_RTC = 0
2828
CIRCUITPY_TOUCHIO = 0
2929
CIRCUITPY_USB_HID = 0
3030
CIRCUITPY_USB_MIDI = 0
31+
CIRCUITPY_BUSDEVICE = 0
3132

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

ports/atmel-samd/boards/xinabox_cc03/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ CIRCUITPY_TOUCHIO=0
2424
CIRCUITPY_BUSDEVICE=1
2525

2626
# Include these Python libraries in firmware.
27-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice

ports/atmel-samd/boards/xinabox_cs11/mpconfigboard.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,4 @@ CIRCUITPY_COUNTIO=0
2727
CIRCUITPY_BUSDEVICE=1
2828

2929
# Include these Python libraries in firmware.
30-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
3130
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD

ports/nrf/boards/pca10100/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CIRCUITPY_RTC = 1
2525
CIRCUITPY_SDCARDIO = 0
2626
CIRCUITPY_TOUCHIO = 0
2727
CIRCUITPY_ULAB = 0
28+
CIRCUITPY_BUSDEVICE = 0
2829
MICROPY_PY_ASYNC_AWAIT = 0
2930

3031
SUPEROPT_GC = 0

ports/stm/boards/espruino_pico/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ LD_FILE = boards/STM32F401xd_fs.ld
2020
# lto for this port, and if other stuff hasn't been added in the
2121
# meantime
2222
CIRCUITPY_ULAB = 0
23+
CIRCUITPY_BUSDEVICE = 0
2324

2425
SUPEROPT_GC = 0

py/circuitpy_mpconfig.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ CFLAGS += -DCIRCUITPY_BLEIO=$(CIRCUITPY_BLEIO)
100100
CIRCUITPY_BOARD ?= 1
101101
CFLAGS += -DCIRCUITPY_BOARD=$(CIRCUITPY_BOARD)
102102

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

106106
CIRCUITPY_BUILTINS_POW3 ?= $(CIRCUITPY_FULL_BUILD)

shared-bindings/adafruit_bus_device/I2CDevice.c

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_make_new(const mp_obj_type_t *type
7676
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
7777
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
7878

79-
busio_i2c_obj_t* i2c = args[ARG_i2c].u_obj;
79+
mp_obj_t* i2c = args[ARG_i2c].u_obj;
8080

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

110-
//| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: int = 0) -> None:
110+
//| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
111111
//| """Read into ``buf`` from the device. The number of bytes read will be the
112112
//| length of ``buf``.
113113
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
@@ -118,22 +118,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_i2cdevice___exit_
118118
//| :param int end: Index to write up to but not include; if None, use ``len(buf)``"""
119119
//| ...
120120
//|
121-
STATIC void readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t start, mp_int_t end) {
122-
mp_buffer_info_t bufinfo;
123-
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
124-
125-
size_t length = bufinfo.len;
126-
normalize_buffer_bounds(&start, end, &length);
127-
if (length == 0) {
128-
mp_raise_ValueError(translate("Buffer must be at least length 1"));
129-
}
130-
131-
uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length);
132-
if (status != 0) {
133-
mp_raise_OSError(status);
134-
}
135-
}
136-
137121
STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
138122
enum { ARG_buffer, ARG_start, ARG_end };
139123
static const mp_arg_t allowed_args[] = {
@@ -147,12 +131,22 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_o
147131
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
148132
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
149133

150-
readinto(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int);
134+
mp_obj_t dest[8];
135+
mp_load_method(self->i2c, MP_QSTR_readfrom_into, dest);
136+
dest[2] = mp_obj_new_int_from_ull(self->device_address);
137+
dest[3] = args[ARG_buffer].u_obj;
138+
//dest[4] = mp_obj_new_str("start", 5);
139+
dest[4] = MP_OBJ_NEW_QSTR(MP_QSTR_start);
140+
dest[5] = mp_obj_new_int(args[ARG_start].u_int);
141+
dest[6] = MP_OBJ_NEW_QSTR(MP_QSTR_end);
142+
dest[7] = mp_obj_new_int(args[ARG_end].u_int);
143+
mp_call_method_n_kw(2, 2, dest);
144+
151145
return mp_const_none;
152146
}
153147
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2, adafruit_bus_device_i2cdevice_readinto);
154148

155-
//| def write(self, buf: ReadableBuffer, *, start: int = 0, end: int = 0) -> None:
149+
//| def write(self, buf: ReadableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
156150
//| """Write the bytes from ``buffer`` to the device, then transmit a stop bit.
157151
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
158152
//| as if ``buffer[start:end]``. This will not cause an allocation like
@@ -163,22 +157,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2,
163157
//| """
164158
//| ...
165159
//|
166-
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) {
167-
mp_buffer_info_t bufinfo;
168-
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
169-
170-
size_t length = bufinfo.len;
171-
normalize_buffer_bounds(&start, end, &length);
172-
if (length == 0) {
173-
mp_raise_ValueError(translate("Buffer must be at least length 1"));
174-
}
175-
176-
uint8_t status = common_hal_adafruit_bus_device_i2cdevice_write(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length, transmit_stop_bit);
177-
if (status != 0) {
178-
mp_raise_OSError(status);
179-
}
180-
}
181-
182160
STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
183161
enum { ARG_buffer, ARG_start, ARG_end };
184162
static const mp_arg_t allowed_args[] = {
@@ -191,13 +169,22 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_
191169
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
192170
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
193171

194-
write(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int, true);
172+
mp_obj_t dest[8];
173+
mp_load_method(self->i2c, MP_QSTR_writeto, dest);
174+
dest[2] = mp_obj_new_int_from_ull(self->device_address);
175+
dest[3] = args[ARG_buffer].u_obj;
176+
dest[4] = MP_OBJ_NEW_QSTR(MP_QSTR_start);
177+
dest[5] = mp_obj_new_int(args[ARG_start].u_int);
178+
dest[6] = MP_OBJ_NEW_QSTR(MP_QSTR_end);
179+
dest[7] = mp_obj_new_int(args[ARG_end].u_int);
180+
mp_call_method_n_kw(2, 2, dest);
181+
195182
return mp_const_none;
196183
}
197184
MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_write_obj, 2, adafruit_bus_device_i2cdevice_write);
198185

199186

200-
//| 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:
187+
//| 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:
201188
//| """Write the bytes from ``out_buffer`` to the device, then immediately
202189
//| reads into ``in_buffer`` from the device. The number of bytes read
203190
//| will be the length of ``in_buffer``.
@@ -233,9 +220,21 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write_then_readinto(size_t n_args,
233220
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
234221
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
235222

236-
write(self, args[ARG_out_buffer].u_obj, args[ARG_out_start].u_int, args[ARG_out_end].u_int, false);
237-
238-
readinto(self, args[ARG_in_buffer].u_obj, args[ARG_in_start].u_int, args[ARG_in_end].u_int);
223+
mp_obj_t dest[13];
224+
mp_load_method(self->i2c, MP_QSTR_writeto_then_readfrom, dest);
225+
dest[2] = mp_obj_new_int_from_ull(self->device_address);
226+
dest[3] = args[ARG_out_buffer].u_obj;
227+
dest[4] = args[ARG_in_buffer].u_obj;
228+
dest[5] = MP_OBJ_NEW_QSTR(MP_QSTR_out_start);
229+
dest[6] = mp_obj_new_int(args[ARG_out_start].u_int);
230+
dest[7] = MP_OBJ_NEW_QSTR(MP_QSTR_out_end);
231+
dest[8] = mp_obj_new_int(args[ARG_out_end].u_int);
232+
dest[9] = MP_OBJ_NEW_QSTR(MP_QSTR_in_start);
233+
dest[10] = mp_obj_new_int(args[ARG_in_start].u_int);
234+
dest[11] = MP_OBJ_NEW_QSTR(MP_QSTR_in_end);
235+
dest[12] = mp_obj_new_int(args[ARG_in_end].u_int);
236+
237+
mp_call_method_n_kw(3, 4, dest);
239238

240239
return mp_const_none;
241240
}

shared-bindings/adafruit_bus_device/I2CDevice.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
extern const mp_obj_type_t adafruit_bus_device_i2cdevice_type;
4444

4545
// Initializes the hardware peripheral.
46-
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);
47-
extern uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
48-
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);
46+
extern void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t *i2c, uint8_t device_address);
4947
extern void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self);
5048
extern void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self);
5149
extern void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self);

shared-module/adafruit_bus_device/I2CDevice.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,60 @@
3131
#include "py/runtime.h"
3232
#include "lib/utils/interrupt_char.h"
3333

34-
void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address) {
34+
void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t *i2c, uint8_t device_address) {
3535
self->i2c = i2c;
3636
self->device_address = device_address;
3737
}
3838

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

42-
while (!success) {
43+
mp_obj_t success = mp_call_method_n_kw(0, 0, dest);
44+
45+
while (!mp_obj_is_true(success)) {
4346
RUN_BACKGROUND_TASKS;
4447
if (mp_hal_is_interrupted()) {
4548
break;
4649
}
4750

48-
success = common_hal_busio_i2c_try_lock(self->i2c);
51+
success = mp_call_method_n_kw(0, 0, dest);
4952
}
5053
}
5154

5255
void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self) {
53-
common_hal_busio_i2c_unlock(self->i2c);
54-
}
55-
56-
uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length) {
57-
return common_hal_busio_i2c_read(self->i2c, self->device_address, buffer, length);
58-
}
59-
60-
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) {
61-
return common_hal_busio_i2c_write(self->i2c, self->device_address, buffer, length, transmit_stop_bit);
56+
mp_obj_t dest[2];
57+
mp_load_method(self->i2c, MP_QSTR_unlock, dest);
58+
mp_call_method_n_kw(0, 0, dest);
6259
}
6360

6461
void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self) {
6562
common_hal_adafruit_bus_device_i2cdevice_lock(self);
6663

67-
mp_buffer_info_t bufinfo;
68-
mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
64+
mp_buffer_info_t write_bufinfo;
65+
mp_obj_t write_buffer = mp_obj_new_bytearray_of_zeros(0);
66+
mp_get_buffer_raise(write_buffer, &write_bufinfo, MP_BUFFER_READ);
6967

70-
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
68+
mp_obj_t dest[4];
7169

72-
uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(self, (uint8_t*)bufinfo.buf, 1);
73-
if (status != 0) {
70+
/* catch exceptions that may be thrown while probing for the device */
71+
nlr_buf_t nlr;
72+
if (nlr_push(&nlr) == 0) {
73+
mp_load_method(self->i2c, MP_QSTR_writeto, dest);
74+
dest[2] = mp_obj_new_int_from_ull(self->device_address);
75+
dest[3] = write_buffer;
76+
mp_call_method_n_kw(2, 0, dest);
77+
nlr_pop();
78+
} else {
7479
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
75-
mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
80+
81+
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))) {
82+
mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
83+
}
84+
else {
85+
/* In case we receive an unrelated exception pass it up */
86+
nlr_raise(MP_OBJ_FROM_PTR(nlr.ret_val));
87+
}
7688
}
7789

7890
common_hal_adafruit_bus_device_i2cdevice_unlock(self);

shared-module/adafruit_bus_device/I2CDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
typedef struct {
3434
mp_obj_base_t base;
35-
busio_i2c_obj_t *i2c;
35+
mp_obj_t *i2c;
3636
uint8_t device_address;
3737
} adafruit_bus_device_i2cdevice_obj_t;
3838

0 commit comments

Comments
 (0)