Skip to content

Commit 5354aea

Browse files
committed
bleio: Allow using len() on UUID
1 parent 13dd27a commit 5354aea

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

shared-bindings/bleio/UUID.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
//| :param int/str uuid: The uuid to encapsulate
5050
//|
5151

52+
//| .. method:: __len__()
53+
//|
54+
//| Returns the uuid length in bits
55+
//|
56+
//| This allows you to:
57+
//|
58+
//| uuid = bleio.UUID(0x1801)
59+
//| print(len(uuid))
60+
//|
61+
5262
//| .. attribute:: type
5363
//|
5464
//| The UUID type. One of:
@@ -79,6 +89,18 @@ STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, si
7989
return MP_OBJ_FROM_PTR(self);
8090
}
8191

92+
STATIC mp_obj_t bleio_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
93+
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
94+
95+
const bleio_uuid_type_t type = common_hal_bleio_uuid_get_type(self);
96+
const uint8_t len = (type == UUID_TYPE_16BIT) ? 16 : 128;
97+
switch (op) {
98+
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0);
99+
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
100+
default: return MP_OBJ_NULL; // op not supported
101+
}
102+
}
103+
82104
STATIC void bleio_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
83105
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
84106

@@ -119,5 +141,6 @@ const mp_obj_type_t bleio_uuid_type = {
119141
.name = MP_QSTR_UUID,
120142
.print = bleio_uuid_print,
121143
.make_new = bleio_uuid_make_new,
144+
.unary_op = bleio_uuid_unary_op,
122145
.locals_dict = (mp_obj_dict_t*)&bleio_uuid_locals_dict
123146
};

0 commit comments

Comments
 (0)