@@ -279,19 +279,7 @@ STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
279279 }
280280}
281281
282- /// \method init()
283- STATIC const mp_arg_t pyb_i2c_init_args [] = {
284- { MP_QSTR_mode , MP_ARG_INT , {.u_int = PYBI2C_MASTER } },
285- { MP_QSTR_baudrate , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 100000 } },
286- { MP_QSTR_pins , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
287- };
288- #define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
289-
290- STATIC mp_obj_t pyb_i2c_init_helper (pyb_i2c_obj_t * self , mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
291- // parse args
292- mp_arg_val_t args [PYB_I2C_INIT_NUM_ARGS ];
293- mp_arg_parse_all (n_args , pos_args , kw_args , PYB_I2C_INIT_NUM_ARGS , pyb_i2c_init_args , args );
294-
282+ STATIC mp_obj_t pyb_i2c_init_helper (pyb_i2c_obj_t * self , mp_arg_val_t * args ) {
295283 // verify that mode is master
296284 if (args [0 ].u_int != PYBI2C_MASTER ) {
297285 goto invalid_args ;
@@ -329,32 +317,49 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const
329317 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
330318}
331319
332- /// \classmethod \constructor(bus, ...)
333- STATIC mp_obj_t pyb_i2c_make_new (mp_obj_t type_in , mp_uint_t n_args , mp_uint_t n_kw , const mp_obj_t * args ) {
334- // check arguments
335- mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
336-
337- // setup the object
338- pyb_i2c_obj_t * self = & pyb_i2c_obj ;
339- self -> base .type = & pyb_i2c_type ;
320+ STATIC const mp_arg_t pyb_i2c_init_args [] = {
321+ { MP_QSTR_id , MP_ARG_OBJ , {.u_obj = mp_const_none } },
322+ { MP_QSTR_mode , MP_ARG_INT , {.u_int = PYBI2C_MASTER } },
323+ { MP_QSTR_baudrate , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 100000 } },
324+ { MP_QSTR_pins , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
325+ };
326+ #define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
327+ STATIC mp_obj_t pyb_i2c_make_new (mp_obj_t type_in , mp_uint_t n_args , mp_uint_t n_kw , const mp_obj_t * all_args ) {
328+ // parse args
329+ mp_map_t kw_args ;
330+ mp_map_init_fixed_table (& kw_args , n_kw , all_args + n_args );
331+ mp_arg_val_t args [MP_ARRAY_SIZE (pyb_i2c_init_args )];
332+ mp_arg_parse_all (n_args , all_args , & kw_args , MP_ARRAY_SIZE (args ), pyb_i2c_init_args , args );
333+
334+ // work out the uart id
335+ uint8_t i2c_id ;
336+ if (args [0 ].u_obj == mp_const_none ) {
337+ // default id
338+ i2c_id = 0 ;
339+ } else {
340+ i2c_id = mp_obj_get_int (args [0 ].u_obj );
341+ }
340342
341343 // check the peripheral id
342- if (mp_obj_get_int ( args [ 0 ]) != 0 ) {
344+ if (i2c_id != 0 ) {
343345 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_resource_not_avaliable ));
344346 }
345347
346- if ( n_args > 1 || n_kw > 0 ) {
347- // start the peripheral
348- mp_map_t kw_args ;
349- mp_map_init_fixed_table ( & kw_args , n_kw , args + n_args );
350- pyb_i2c_init_helper ( self , n_args - 1 , args + 1 , & kw_args );
351- }
348+ // setup the object
349+ pyb_i2c_obj_t * self = & pyb_i2c_obj ;
350+ self -> base . type = & pyb_i2c_type ;
351+
352+ // start the peripheral
353+ pyb_i2c_init_helper ( self , & args [ 1 ]);
352354
353355 return (mp_obj_t )self ;
354356}
355357
356- STATIC mp_obj_t pyb_i2c_init (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
357- return pyb_i2c_init_helper (args [0 ], n_args - 1 , args + 1 , kw_args );
358+ STATIC mp_obj_t pyb_i2c_init (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
359+ // parse args
360+ mp_arg_val_t args [MP_ARRAY_SIZE (pyb_i2c_init_args ) - 1 ];
361+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (args ), & pyb_i2c_init_args [1 ], args );
362+ return pyb_i2c_init_helper (pos_args [0 ], args );
358363}
359364STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_i2c_init_obj , 1 , pyb_i2c_init );
360365
0 commit comments