diff options
| author | arturo182 <arturo182@tlen.pl> | 2018-07-26 00:14:23 +0200 |
|---|---|---|
| committer | arturo182 <arturo182@tlen.pl> | 2018-10-21 16:00:12 +0200 |
| commit | 5354aeab4c2aaee53c07caa6242701cbac069d2d (patch) | |
| tree | 044ccd7a1980b15df9dfdf5cb5f4c677d4ff0aae | |
| parent | 13dd27a04760867a7ccb6d59fd339536ce588e25 (diff) | |
bleio: Allow using len() on UUID
| -rw-r--r-- | shared-bindings/bleio/UUID.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/shared-bindings/bleio/UUID.c b/shared-bindings/bleio/UUID.c index 26e3a84c8..7cef9a26f 100644 --- a/shared-bindings/bleio/UUID.c +++ b/shared-bindings/bleio/UUID.c @@ -49,6 +49,16 @@ //| :param int/str uuid: The uuid to encapsulate //| +//| .. method:: __len__() +//| +//| Returns the uuid length in bits +//| +//| This allows you to: +//| +//| uuid = bleio.UUID(0x1801) +//| print(len(uuid)) +//| + //| .. attribute:: type //| //| 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 return MP_OBJ_FROM_PTR(self); } +STATIC mp_obj_t bleio_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) { + bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in); + + const bleio_uuid_type_t type = common_hal_bleio_uuid_get_type(self); + const uint8_t len = (type == UUID_TYPE_16BIT) ? 16 : 128; + switch (op) { + case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); + case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len); + default: return MP_OBJ_NULL; // op not supported + } +} + STATIC void bleio_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -119,5 +141,6 @@ const mp_obj_type_t bleio_uuid_type = { .name = MP_QSTR_UUID, .print = bleio_uuid_print, .make_new = bleio_uuid_make_new, + .unary_op = bleio_uuid_unary_op, .locals_dict = (mp_obj_dict_t*)&bleio_uuid_locals_dict }; |
