Skip to content

Commit 28ff097

Browse files
ports/rp2: Make I2C bus ID arg optional and use default if not provided
1 parent 1100aa6 commit 28ff097

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

ports/rp2/machine_i2c.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
9797
mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
9898
enum { ARG_id, ARG_freq, ARG_scl, ARG_sda, ARG_timeout };
9999
static const mp_arg_t allowed_args[] = {
100-
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
100+
{ MP_QSTR_id, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
101101
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} },
102102
{ MP_QSTR_scl, MICROPY_I2C_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
103103
{ MP_QSTR_sda, MICROPY_I2C_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
@@ -108,8 +108,20 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
108108
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
109109
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
110110

111-
// Get I2C bus.
112-
int i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
111+
// Bus ID - use the default I2C bus as default
112+
#ifdef PICO_DEFAULT_I2C
113+
int i2c_id = PICO_DEFAULT_I2C;
114+
#else
115+
// invalid value
116+
int i2c_id = -1;
117+
#endif
118+
119+
// User provide a value?
120+
if (args[ARG_id].u_obj != mp_const_none){
121+
i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
122+
}
123+
124+
// Check if the I2C bus is valid
113125
if (i2c_id < 0 || i2c_id >= MP_ARRAY_SIZE(machine_i2c_obj)) {
114126
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
115127
}

0 commit comments

Comments
 (0)