2727 * THE SOFTWARE.
2828 */
2929
30- #include "py/runtime.h"
31- #include "py/objarray.h"
3230#include "py/objproperty.h"
31+ #include "py/runtime.h"
3332#include "py/stream.h"
3433
3534#include "bindings/espnow/ESPNow.h"
@@ -74,19 +73,15 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, size_t
7473 mp_raise_RuntimeError (translate ("Already running" ));
7574 }
7675
76+ // Allocate a new object
7777 self = m_new_obj (espnow_obj_t );
7878 self -> base .type = & espnow_type ;
7979
80- common_hal_espnow_set_buffer_size (self , args [ARG_buffer_size ].u_int );
81- common_hal_espnow_set_phy_rate (self , args [ARG_phy_rate ].u_int );
82-
83- self -> peers = espnow_peers_new ();
80+ // Construct the object
81+ common_hal_espnow_construct (self , args [ARG_buffer_size ].u_int , args [ARG_phy_rate ].u_int );
8482
8583 // Set the global singleton pointer for the espnow protocol.
8684 MP_STATE_PORT (espnow_singleton ) = self ;
87-
88- common_hal_espnow_init (self );
89-
9085 return self ;
9186}
9287
@@ -167,23 +162,29 @@ MP_PROPERTY_GETSET(espnow_phy_rate_obj,
167162 (mp_obj_t )& espnow_get_phy_rate_obj ,
168163 (mp_obj_t )& espnow_set_phy_rate_obj );
169164
170- //| stats: Tuple[int, int, int, int, int]
171- //| """Provide some useful stats in a `tuple` of
172- //| (tx_packets, tx_responses, tx_failures, rx_packets, rx_failures). (read-only)"""
165+ //| tx_stats: ESPNowStats
166+ //| """The ``TX`` packet statistics."""
167+ //|
168+ STATIC mp_obj_t espnow_get_tx_stats (mp_obj_t self_in ) {
169+ espnow_obj_t * self = MP_OBJ_TO_PTR (self_in );
170+ return MP_OBJ_FROM_PTR (self -> tx_stats );
171+ }
172+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (espnow_get_tx_stats_obj , espnow_get_tx_stats );
173+
174+ MP_PROPERTY_GETTER (espnow_tx_stats_obj ,
175+ (mp_obj_t )& espnow_get_tx_stats_obj );
176+
177+ //| rx_stats: ESPNowStats
178+ //| """The ``RX`` packet statistics."""
173179//|
174- STATIC mp_obj_t espnow_get_stats (mp_obj_t self_in ) {
180+ STATIC mp_obj_t espnow_get_rx_stats (mp_obj_t self_in ) {
175181 espnow_obj_t * self = MP_OBJ_TO_PTR (self_in );
176- return MP_OBJ_NEW_TUPLE (
177- mp_obj_new_int (self -> tx_packets ),
178- mp_obj_new_int (self -> tx_responses ),
179- mp_obj_new_int (self -> tx_failures ),
180- mp_obj_new_int (self -> rx_packets ),
181- mp_obj_new_int (self -> rx_failures ));
182+ return MP_OBJ_FROM_PTR (self -> rx_stats );
182183}
183- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (espnow_get_stats_obj , espnow_get_stats );
184+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (espnow_get_rx_stats_obj , espnow_get_rx_stats );
184185
185- MP_PROPERTY_GETTER (espnow_stats_obj ,
186- (mp_obj_t )& espnow_get_stats_obj );
186+ MP_PROPERTY_GETTER (espnow_rx_stats_obj ,
187+ (mp_obj_t )& espnow_get_rx_stats_obj );
187188
188189// --- Send and Receive ESP-NOW data ---
189190
@@ -195,9 +196,8 @@ MP_PROPERTY_GETTER(espnow_stats_obj,
195196//| """Send a message to the peer's mac address.
196197//|
197198//| :param ReadableBuffer message: The message to send (length <= 250 bytes).
198- //| :param ReadableBuffer mac: The peer's address (length = 6 bytes). If `None` or any non-true value, send to all registered peers.
199- //|
200- //| :raises EAGAIN: if the internal espnow buffers are full."""
199+ //| :param ReadableBuffer mac: The peer's address (length = 6 bytes).
200+ //| If `None` or any non-true value, send to all registered peers."""
201201//| ...
202202STATIC mp_obj_t espnow_send (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
203203 enum { ARG_message , ARG_mac , ARG_sync };
@@ -255,14 +255,17 @@ STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = {
255255 { MP_ROM_QSTR (MP_QSTR___enter__ ), MP_ROM_PTR (& mp_identity_obj ) },
256256 { MP_ROM_QSTR (MP_QSTR___exit__ ), MP_ROM_PTR (& espnow___exit___obj ) },
257257
258+ // Deinit the object
258259 { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& espnow_deinit_obj ) },
259260
260261 // Config parameters
261262 { MP_ROM_QSTR (MP_QSTR_set_pmk ), MP_ROM_PTR (& espnow_set_pmk_obj ) },
262263 { MP_ROM_QSTR (MP_QSTR_buffer_size ), MP_ROM_PTR (& espnow_buffer_size_obj ) },
263264 { MP_ROM_QSTR (MP_QSTR_phy_rate ), MP_ROM_PTR (& espnow_phy_rate_obj ) },
264265
265- { MP_ROM_QSTR (MP_QSTR_stats ), MP_ROM_PTR (& espnow_stats_obj ) },
266+ // Packet statistics
267+ { MP_ROM_QSTR (MP_QSTR_tx_stats ), MP_ROM_PTR (& espnow_tx_stats_obj ) },
268+ { MP_ROM_QSTR (MP_QSTR_rx_stats ), MP_ROM_PTR (& espnow_rx_stats_obj ) },
266269
267270 // Send and receive messages
268271 { MP_ROM_QSTR (MP_QSTR_send ), MP_ROM_PTR (& espnow_send_obj ) },
@@ -278,21 +281,25 @@ STATIC MP_DEFINE_CONST_DICT(espnow_locals_dict, espnow_locals_dict_table);
278281
279282// Support ioctl(MP_STREAM_POLL, ) for asyncio
280283STATIC mp_uint_t espnow_stream_ioctl (mp_obj_t self_in , mp_uint_t request , uintptr_t arg , int * errcode ) {
281- if (request != MP_STREAM_POLL ) {
282- * errcode = MP_EINVAL ;
283- return MP_STREAM_ERROR ;
284- }
285-
286284 espnow_obj_t * self = MP_OBJ_TO_PTR (self_in );
287- return (common_hal_espnow_deinited (self )) ? 0 : // If not initialized
288- arg ^ (
289- // If no data in the buffer, unset the Read ready flag
290- ((!ringbuf_num_filled (self -> recv_buffer )) ? MP_STREAM_POLL_RD : 0 ) |
291- // If still waiting for responses, unset the Write ready flag
292- ((self -> tx_responses < self -> tx_packets ) ? MP_STREAM_POLL_WR : 0 ));
285+ check_for_deinit (self );
286+ switch (request ) {
287+ case MP_STREAM_POLL : {
288+ mp_uint_t flags = arg ;
289+ mp_uint_t ret = 0 ;
290+ if ((flags & MP_STREAM_POLL_RD ) && ringbuf_num_filled (self -> recv_buffer ) > 0 ) {
291+ ret |= MP_STREAM_POLL_RD ;
292+ }
293+ return ret ;
294+ }
295+ default :
296+ * errcode = MP_EINVAL ;
297+ return MP_STREAM_ERROR ;
298+ }
293299}
294300
295301STATIC const mp_stream_p_t espnow_stream_p = {
302+ MP_PROTO_IMPLEMENT (MP_QSTR_protocol_stream )
296303 .ioctl = espnow_stream_ioctl ,
297304};
298305
0 commit comments