|
49 | 49 | //| :param int/str uuid: The uuid to encapsulate
|
50 | 50 | //|
|
51 | 51 |
|
| 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 | + |
52 | 62 | //| .. attribute:: type
|
53 | 63 | //|
|
54 | 64 | //| 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
|
79 | 89 | return MP_OBJ_FROM_PTR(self);
|
80 | 90 | }
|
81 | 91 |
|
| 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 | + |
82 | 104 | STATIC void bleio_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
83 | 105 | bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
84 | 106 |
|
@@ -119,5 +141,6 @@ const mp_obj_type_t bleio_uuid_type = {
|
119 | 141 | .name = MP_QSTR_UUID,
|
120 | 142 | .print = bleio_uuid_print,
|
121 | 143 | .make_new = bleio_uuid_make_new,
|
| 144 | + .unary_op = bleio_uuid_unary_op, |
122 | 145 | .locals_dict = (mp_obj_dict_t*)&bleio_uuid_locals_dict
|
123 | 146 | };
|
0 commit comments