Skip to content

Commit 53f1a3a

Browse files
ports/esp32: Make I2C bus ID arg optional.
Signed-off-by: Malcolm McKellips <[email protected]>
1 parent 7ef4a1f commit 53f1a3a

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Diff for: ports/esp32/machine_i2c.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,11 @@ static void machine_hw_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_p
146146
}
147147

148148
mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
149-
MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args);
150149

151150
// Parse args
152151
enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout };
153152
static const mp_arg_t allowed_args[] = {
154-
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
153+
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
155154
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
156155
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
157156
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} },
@@ -161,7 +160,15 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_
161160
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
162161

163162
// Get I2C bus
164-
mp_int_t i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
163+
// Bus ID - use I2C bus 0 as default
164+
mp_int_t i2c_id = I2C_NUM_0;
165+
166+
// User provided a value?
167+
if (args[ARG_id].u_obj != MP_OBJ_NULL) {
168+
i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
169+
}
170+
171+
// Check if the I2C bus is valid
165172
if (!(I2C_NUM_0 <= i2c_id && i2c_id < I2C_NUM_MAX)) {
166173
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
167174
}

0 commit comments

Comments
 (0)