27
27
* THE SOFTWARE.
28
28
*/
29
29
30
- #include "py/runtime.h"
31
- #include "py/objarray.h"
32
30
#include "py/objproperty.h"
31
+ #include "py/runtime.h"
33
32
#include "py/stream.h"
34
33
35
34
#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
74
73
mp_raise_RuntimeError (translate ("Already running" ));
75
74
}
76
75
76
+ // Allocate a new object
77
77
self = m_new_obj (espnow_obj_t );
78
78
self -> base .type = & espnow_type ;
79
79
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 );
84
82
85
83
// Set the global singleton pointer for the espnow protocol.
86
84
MP_STATE_PORT (espnow_singleton ) = self ;
87
-
88
- common_hal_espnow_init (self );
89
-
90
85
return self ;
91
86
}
92
87
@@ -167,23 +162,29 @@ MP_PROPERTY_GETSET(espnow_phy_rate_obj,
167
162
(mp_obj_t )& espnow_get_phy_rate_obj ,
168
163
(mp_obj_t )& espnow_set_phy_rate_obj );
169
164
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."""
173
179
//|
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 ) {
175
181
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 );
182
183
}
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 );
184
185
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 );
187
188
188
189
// --- Send and Receive ESP-NOW data ---
189
190
@@ -195,9 +196,8 @@ MP_PROPERTY_GETTER(espnow_stats_obj,
195
196
//| """Send a message to the peer's mac address.
196
197
//|
197
198
//| :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."""
201
201
//| ...
202
202
STATIC mp_obj_t espnow_send (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
203
203
enum { ARG_message , ARG_mac , ARG_sync };
@@ -255,14 +255,17 @@ STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = {
255
255
{ MP_ROM_QSTR (MP_QSTR___enter__ ), MP_ROM_PTR (& mp_identity_obj ) },
256
256
{ MP_ROM_QSTR (MP_QSTR___exit__ ), MP_ROM_PTR (& espnow___exit___obj ) },
257
257
258
+ // Deinit the object
258
259
{ MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& espnow_deinit_obj ) },
259
260
260
261
// Config parameters
261
262
{ MP_ROM_QSTR (MP_QSTR_set_pmk ), MP_ROM_PTR (& espnow_set_pmk_obj ) },
262
263
{ MP_ROM_QSTR (MP_QSTR_buffer_size ), MP_ROM_PTR (& espnow_buffer_size_obj ) },
263
264
{ MP_ROM_QSTR (MP_QSTR_phy_rate ), MP_ROM_PTR (& espnow_phy_rate_obj ) },
264
265
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 ) },
266
269
267
270
// Send and receive messages
268
271
{ 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);
278
281
279
282
// Support ioctl(MP_STREAM_POLL, ) for asyncio
280
283
STATIC 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
-
286
284
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
+ }
293
299
}
294
300
295
301
STATIC const mp_stream_p_t espnow_stream_p = {
302
+ MP_PROTO_IMPLEMENT (MP_QSTR_protocol_stream )
296
303
.ioctl = espnow_stream_ioctl ,
297
304
};
298
305
0 commit comments