Skip to content

Commit 9c430d4

Browse files
committed
use truthiness of the object itself instead of any
1 parent 9b98d48 commit 9c430d4

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

ports/espressif/bindings/espnow/ESPNow.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -561,20 +561,6 @@ STATIC mp_obj_t espnow_recv(mp_obj_t self_in, mp_obj_t buffers) {
561561
}
562562
STATIC MP_DEFINE_CONST_FUN_OBJ_2(espnow_recv_obj, espnow_recv);
563563

564-
//| any: bool
565-
//| """`True` if data is available to read from the buffers."""
566-
//|
567-
STATIC mp_obj_t espnow_get_any(const mp_obj_t self_in) {
568-
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
569-
check_for_deinit(self);
570-
571-
return ringbuf_num_filled(self->recv_buffer) ? mp_const_true : mp_const_false;
572-
}
573-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_any_obj, espnow_get_any);
574-
575-
MP_PROPERTY_GETTER(espnow_any_obj,
576-
(mp_obj_t)&espnow_get_any_obj);
577-
578564
// --- Peer Management Functions ---
579565

580566
// Common code for add_peer() and mod_peer() to process the args.
@@ -826,7 +812,6 @@ STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = {
826812
// Send and receive messages
827813
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&espnow_send_obj) },
828814
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&espnow_recv_obj) },
829-
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&espnow_any_obj) },
830815

831816
// Peer management functions
832817
{ MP_ROM_QSTR(MP_QSTR_add_peer), MP_ROM_PTR(&espnow_add_peer_obj) },
@@ -864,6 +849,27 @@ STATIC const mp_stream_p_t espnow_stream_p = {
864849
.ioctl = espnow_stream_ioctl,
865850
};
866851

852+
//| def __bool__(self) -> bool:
853+
//| """``True`` if `len()` is greater than zero.
854+
//| This is an easy way to check if the buffer is empty.
855+
//| """
856+
//| ...
857+
//| def __len__(self) -> int:
858+
//| """Return the number of `bytes` available to read. Used to implement ``len()``."""
859+
//| ...
860+
STATIC mp_obj_t espnow_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
861+
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
862+
size_t len = ringbuf_num_filled(self->recv_buffer);
863+
switch (op) {
864+
case MP_UNARY_OP_BOOL:
865+
return mp_obj_new_bool(len != 0);
866+
case MP_UNARY_OP_LEN:
867+
return mp_obj_new_int_from_uint(len);
868+
default:
869+
return MP_OBJ_NULL; // op not supported
870+
}
871+
}
872+
867873
const mp_obj_type_t espnow_type = {
868874
{ &mp_type_type },
869875
.name = MP_QSTR_ESPNow,
@@ -872,5 +878,6 @@ const mp_obj_type_t espnow_type = {
872878
.flags = MP_TYPE_FLAG_EXTENDED,
873879
MP_TYPE_EXTENDED_FIELDS(
874880
.protocol = &espnow_stream_p,
881+
.unary_op = &espnow_unary_op
875882
),
876883
};

0 commit comments

Comments
 (0)