Fixes from draft PR

This commit is contained in:
gamblor21 2021-01-09 10:07:08 -06:00
parent b609bc0124
commit d3995eaf97
2 changed files with 18 additions and 32 deletions

View File

@ -135,9 +135,10 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_o
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_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_str("end", 3);
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);
@ -172,9 +173,9 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_
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_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_str("end", 3);
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);
@ -224,13 +225,13 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write_then_readinto(size_t n_args,
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_str("out_start", 9);
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_str("out_end", 7);
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_str("in_start", 8);
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_str("in_end", 6);
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);

View File

@ -68,37 +68,22 @@ void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_devi
mp_obj_t dest[4];
/* catch exceptions that may be thrown while probing for the device */
nlr_buf_t write_nlr;
if (nlr_push(&write_nlr) == 0) {
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 {
/* some OS's don't like writing an empty bytestring... retry by reading a byte */
mp_buffer_info_t read_bufinfo;
mp_obj_t read_buffer = mp_obj_new_bytearray_of_zeros(1);
mp_get_buffer_raise(read_buffer, &read_bufinfo, MP_BUFFER_WRITE);
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
mp_load_method(self->i2c, MP_QSTR_readfrom_into, dest);
dest[2] = mp_obj_new_int_from_ull(self->device_address);
dest[3] = read_buffer;
nlr_buf_t read_nlr;
if (nlr_push(&read_nlr) == 0) {
mp_call_method_n_kw(2, 0, dest);
nlr_pop();
} else {
/* At this point we tried two methods and only got exceptions */
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t*)read_nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
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(read_nlr.ret_val));
}
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));
}
}