Skip to content

Commit 1349373

Browse files
committed
canio: CAN.Match: improve how an unspecified mask is implemented
0 should actually indicate a "match everything" filter which you otherwise couldn't indicate with any single Match object, and an all-address-bits-set number should indicate a "match single address" filter. Use an optional/default None argument to do the job.
1 parent 4f7f1e8 commit 1349373

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

shared-bindings/canio/Match.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
//| """Describe CAN bus messages to match"""
3434
//|
3535
//|
36-
//| def __init__(self, id: int, *, mask: int = 0, extended: bool = False):
36+
//| def __init__(self, id: int, *, mask: Optional[int] = None, extended: bool = False):
3737
//| """Construct a Match with the given properties.
3838
//|
39-
//| If mask is nonzero, then the filter is for any id which matches all
39+
//| If mask is not None, then the filter is for any id which matches all
4040
//| the nonzero bits in mask. Otherwise, it matches exactly the given id.
4141
//| If extended is true then only extended ids are matched, otherwise
4242
//| only standard ids are matched."""
@@ -46,7 +46,7 @@ STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, c
4646
enum { ARG_id, ARG_mask, ARG_extended, NUM_ARGS };
4747
static const mp_arg_t allowed_args[] = {
4848
{ MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED },
49-
{ MP_QSTR_mask, MP_ARG_INT, {.u_int = 0} },
49+
{ MP_QSTR_mask, MP_ARG_OBJ, {.u_obj = mp_const_none } },
5050
{ MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
5151
};
5252
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -56,7 +56,7 @@ STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, c
5656

5757
int id_bits = args[ARG_extended].u_bool ? 0x1fffffff : 0x7ff;
5858
int id = args[ARG_id].u_int;
59-
int mask = args[ARG_mask].u_int;
59+
int mask = args[ARG_mask].u_obj == mp_const_none ? id_bits : mp_obj_get_int(args[ARG_mask].u_obj);
6060

6161
if (id & ~id_bits) {
6262
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_id);
@@ -68,7 +68,7 @@ STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, c
6868

6969
canio_match_obj_t *self = m_new_obj(canio_match_obj_t);
7070
self->base.type = &canio_match_type;
71-
common_hal_canio_match_construct(self, args[ARG_id].u_int, args[ARG_mask].u_int, args[ARG_extended].u_bool);
71+
common_hal_canio_match_construct(self, id, mask, args[ARG_extended].u_bool);
7272
return self;
7373
}
7474

0 commit comments

Comments
 (0)