Skip to content

Commit db7a962

Browse files
authored
Merge pull request #4112 from gamblor21/longint_busdevice_fix
Fixed for boards without longint
2 parents 5414363 + 0cf2df4 commit db7a962

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

shared-bindings/adafruit_bus_device/I2CDevice.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "lib/utils/buffer_helper.h"
3636
#include "lib/utils/context_manager_helpers.h"
3737
#include "py/runtime.h"
38+
#include "py/smallint.h"
3839
#include "supervisor/shared/translate.h"
3940

4041

@@ -132,15 +133,20 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_o
132133
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
133134

134135
mp_obj_t dest[8];
136+
uint8_t num_kws = 1;
137+
135138
mp_load_method(self->i2c, MP_QSTR_readfrom_into, dest);
136-
dest[2] = mp_obj_new_int_from_ull(self->device_address);
139+
dest[2] = MP_OBJ_NEW_SMALL_INT(self->device_address);
137140
dest[3] = args[ARG_buffer].u_obj;
138141
//dest[4] = mp_obj_new_str("start", 5);
139142
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);
143+
dest[5] = MP_OBJ_NEW_SMALL_INT(args[ARG_start].u_int);
144+
if (args[ARG_end].u_int != INT_MAX) {
145+
dest[6] = MP_OBJ_NEW_QSTR(MP_QSTR_end);
146+
dest[7] = MP_OBJ_NEW_SMALL_INT(args[ARG_end].u_int);
147+
num_kws++;
148+
}
149+
mp_call_method_n_kw(2, num_kws, dest);
144150

145151
return mp_const_none;
146152
}
@@ -170,14 +176,20 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_
170176
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
171177

172178
mp_obj_t dest[8];
179+
uint8_t num_kws = 1;
180+
173181
mp_load_method(self->i2c, MP_QSTR_writeto, dest);
174-
dest[2] = mp_obj_new_int_from_ull(self->device_address);
182+
dest[2] = MP_OBJ_NEW_SMALL_INT(self->device_address);
175183
dest[3] = args[ARG_buffer].u_obj;
176184
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);
185+
dest[5] = MP_OBJ_NEW_SMALL_INT(args[ARG_start].u_int);
186+
if (args[ARG_end].u_int != INT_MAX) {
187+
dest[6] = MP_OBJ_NEW_QSTR(MP_QSTR_end);
188+
dest[7] = MP_OBJ_NEW_SMALL_INT(args[ARG_end].u_int);
189+
num_kws++;
190+
}
191+
192+
mp_call_method_n_kw(2, num_kws, dest);
181193

182194
return mp_const_none;
183195
}
@@ -221,20 +233,28 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write_then_readinto(size_t n_args,
221233
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
222234

223235
mp_obj_t dest[13];
236+
uint8_t num_kws = 2;
237+
224238
mp_load_method(self->i2c, MP_QSTR_writeto_then_readfrom, dest);
225-
dest[2] = mp_obj_new_int_from_ull(self->device_address);
239+
dest[2] = MP_OBJ_NEW_SMALL_INT(self->device_address);
226240
dest[3] = args[ARG_out_buffer].u_obj;
227241
dest[4] = args[ARG_in_buffer].u_obj;
228242
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);
243+
dest[6] = MP_OBJ_NEW_SMALL_INT(args[ARG_out_start].u_int);
244+
if (args[ARG_out_end].u_int != INT_MAX) {
245+
dest[7] = MP_OBJ_NEW_QSTR(MP_QSTR_out_end);
246+
dest[8] = MP_OBJ_NEW_SMALL_INT(args[ARG_out_end].u_int);
247+
num_kws++;
248+
}
232249
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);
250+
dest[10] = MP_OBJ_NEW_SMALL_INT(args[ARG_in_start].u_int);
251+
if (args[ARG_in_end].u_int != INT_MAX) {
252+
dest[11] = MP_OBJ_NEW_QSTR(MP_QSTR_in_end);
253+
dest[12] = MP_OBJ_NEW_SMALL_INT(args[ARG_in_end].u_int);
254+
num_kws++;
255+
}
236256

237-
mp_call_method_n_kw(3, 4, dest);
257+
mp_call_method_n_kw(3, num_kws, dest);
238258

239259
return mp_const_none;
240260
}

shared-module/adafruit_bus_device/I2CDevice.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_devi
7171
nlr_buf_t nlr;
7272
if (nlr_push(&nlr) == 0) {
7373
mp_load_method(self->i2c, MP_QSTR_writeto, dest);
74-
dest[2] = mp_obj_new_int_from_ull(self->device_address);
74+
dest[2] = MP_OBJ_NEW_SMALL_INT(self->device_address);
7575
dest[3] = write_buffer;
7676
mp_call_method_n_kw(2, 0, dest);
7777
nlr_pop();

0 commit comments

Comments
 (0)