From ff95e9616099e2b7f14b46c69a556714d41359ed Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 4 Feb 2023 01:03:33 +0530 Subject: [PATCH] add `ESPNowStats` class and more --- ports/espressif/bindings/espnow/ESPNow.c | 81 +++++++++-------- .../espressif/bindings/espnow/ESPNowPacket.c | 4 +- ports/espressif/bindings/espnow/ESPNowStats.c | 87 +++++++++++++++++++ ports/espressif/bindings/espnow/ESPNowStats.h | 38 ++++++++ ports/espressif/bindings/espnow/Peers.c | 2 + ports/espressif/bindings/espnow/__init__.c | 4 +- ports/espressif/common-hal/espnow/ESPNow.c | 77 ++++++++-------- ports/espressif/common-hal/espnow/ESPNow.h | 11 ++- 8 files changed, 220 insertions(+), 84 deletions(-) create mode 100644 ports/espressif/bindings/espnow/ESPNowStats.c create mode 100644 ports/espressif/bindings/espnow/ESPNowStats.h diff --git a/ports/espressif/bindings/espnow/ESPNow.c b/ports/espressif/bindings/espnow/ESPNow.c index 39eb7a5019..33bb882873 100644 --- a/ports/espressif/bindings/espnow/ESPNow.c +++ b/ports/espressif/bindings/espnow/ESPNow.c @@ -27,9 +27,8 @@ * THE SOFTWARE. */ -#include "py/runtime.h" -#include "py/objarray.h" #include "py/objproperty.h" +#include "py/runtime.h" #include "py/stream.h" #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 mp_raise_RuntimeError(translate("Already running")); } + // Allocate a new object self = m_new_obj(espnow_obj_t); self->base.type = &espnow_type; - common_hal_espnow_set_buffer_size(self, args[ARG_buffer_size].u_int); - common_hal_espnow_set_phy_rate(self, args[ARG_phy_rate].u_int); - - self->peers = espnow_peers_new(); + // Construct the object + common_hal_espnow_construct(self, args[ARG_buffer_size].u_int, args[ARG_phy_rate].u_int); // Set the global singleton pointer for the espnow protocol. MP_STATE_PORT(espnow_singleton) = self; - - common_hal_espnow_init(self); - return self; } @@ -167,23 +162,29 @@ MP_PROPERTY_GETSET(espnow_phy_rate_obj, (mp_obj_t)&espnow_get_phy_rate_obj, (mp_obj_t)&espnow_set_phy_rate_obj); -//| stats: Tuple[int, int, int, int, int] -//| """Provide some useful stats in a `tuple` of -//| (tx_packets, tx_responses, tx_failures, rx_packets, rx_failures). (read-only)""" +//| tx_stats: ESPNowStats +//| """The ``TX`` packet statistics.""" //| -STATIC mp_obj_t espnow_get_stats(mp_obj_t self_in) { +STATIC mp_obj_t espnow_get_tx_stats(mp_obj_t self_in) { espnow_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_TUPLE( - mp_obj_new_int(self->tx_packets), - mp_obj_new_int(self->tx_responses), - mp_obj_new_int(self->tx_failures), - mp_obj_new_int(self->rx_packets), - mp_obj_new_int(self->rx_failures)); + return MP_OBJ_FROM_PTR(self->tx_stats); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_stats_obj, espnow_get_stats); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_tx_stats_obj, espnow_get_tx_stats); -MP_PROPERTY_GETTER(espnow_stats_obj, - (mp_obj_t)&espnow_get_stats_obj); +MP_PROPERTY_GETTER(espnow_tx_stats_obj, + (mp_obj_t)&espnow_get_tx_stats_obj); + +//| rx_stats: ESPNowStats +//| """The ``RX`` packet statistics.""" +//| +STATIC mp_obj_t espnow_get_rx_stats(mp_obj_t self_in) { + espnow_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_FROM_PTR(self->rx_stats); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_rx_stats_obj, espnow_get_rx_stats); + +MP_PROPERTY_GETTER(espnow_rx_stats_obj, + (mp_obj_t)&espnow_get_rx_stats_obj); // --- Send and Receive ESP-NOW data --- @@ -195,9 +196,8 @@ MP_PROPERTY_GETTER(espnow_stats_obj, //| """Send a message to the peer's mac address. //| //| :param ReadableBuffer message: The message to send (length <= 250 bytes). -//| :param ReadableBuffer mac: The peer's address (length = 6 bytes). If `None` or any non-true value, send to all registered peers. -//| -//| :raises EAGAIN: if the internal espnow buffers are full.""" +//| :param ReadableBuffer mac: The peer's address (length = 6 bytes). +//| If `None` or any non-true value, send to all registered peers.""" //| ... STATIC mp_obj_t espnow_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_message, ARG_mac, ARG_sync }; @@ -255,6 +255,7 @@ STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&espnow___exit___obj) }, + // Deinit the object { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&espnow_deinit_obj) }, // Config parameters @@ -262,7 +263,9 @@ STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_buffer_size), MP_ROM_PTR(&espnow_buffer_size_obj) }, { MP_ROM_QSTR(MP_QSTR_phy_rate), MP_ROM_PTR(&espnow_phy_rate_obj) }, - { MP_ROM_QSTR(MP_QSTR_stats), MP_ROM_PTR(&espnow_stats_obj) }, + // Packet statistics + { MP_ROM_QSTR(MP_QSTR_tx_stats), MP_ROM_PTR(&espnow_tx_stats_obj) }, + { MP_ROM_QSTR(MP_QSTR_rx_stats), MP_ROM_PTR(&espnow_rx_stats_obj) }, // Send and receive messages { 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); // Support ioctl(MP_STREAM_POLL, ) for asyncio STATIC mp_uint_t espnow_stream_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { - if (request != MP_STREAM_POLL) { - *errcode = MP_EINVAL; - return MP_STREAM_ERROR; - } - espnow_obj_t *self = MP_OBJ_TO_PTR(self_in); - return (common_hal_espnow_deinited(self)) ? 0 : // If not initialized - arg ^ ( - // If no data in the buffer, unset the Read ready flag - ((!ringbuf_num_filled(self->recv_buffer)) ? MP_STREAM_POLL_RD : 0) | - // If still waiting for responses, unset the Write ready flag - ((self->tx_responses < self->tx_packets) ? MP_STREAM_POLL_WR : 0)); + check_for_deinit(self); + switch (request) { + case MP_STREAM_POLL: { + mp_uint_t flags = arg; + mp_uint_t ret = 0; + if ((flags & MP_STREAM_POLL_RD) && ringbuf_num_filled(self->recv_buffer) > 0) { + ret |= MP_STREAM_POLL_RD; + } + return ret; + } + default: + *errcode = MP_EINVAL; + return MP_STREAM_ERROR; + } } STATIC const mp_stream_p_t espnow_stream_p = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream) .ioctl = espnow_stream_ioctl, }; diff --git a/ports/espressif/bindings/espnow/ESPNowPacket.c b/ports/espressif/bindings/espnow/ESPNowPacket.c index 9d582a00eb..7cbf2caed0 100644 --- a/ports/espressif/bindings/espnow/ESPNowPacket.c +++ b/ports/espressif/bindings/espnow/ESPNowPacket.c @@ -27,7 +27,7 @@ #include "bindings/espnow/ESPNowPacket.h" //| class ESPNowPacket: -//| """A packet retreived from ESP-NOW communication protocol""" +//| """A packet retrieved from ESP-NOW communication protocol""" //| //| mac: ReadableBuffer //| """The sender's mac address (length = 6 bytes)""" @@ -39,7 +39,7 @@ //| """The received signal strength indication (in dBm from -127 to 0)""" //| //| time: int -//| """The is the time in milliseconds since device last booted""" +//| """The time in milliseconds since the device last booted when the packet was received""" //| const mp_obj_namedtuple_type_t espnow_packet_type_obj = { diff --git a/ports/espressif/bindings/espnow/ESPNowStats.c b/ports/espressif/bindings/espnow/ESPNowStats.c new file mode 100644 index 0000000000..babd8e4d83 --- /dev/null +++ b/ports/espressif/bindings/espnow/ESPNowStats.c @@ -0,0 +1,87 @@ +/* + * This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython + * + * The MIT License (MIT) + * + * Copyright (c) 2023 MicroDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "bindings/espnow/ESPNowStats.h" +#include "py/objproperty.h" + +//| class ESPNowStats: +//| """Provide some useful packet tx/rx statistics.""" +//| + +//| success: int +//| """The number of successes. (read-only) +//| +//| * In case of transmit ``TX``: +//| The number of tx packets received by the peer(s) ``ESP_NOW_SEND_SUCCESS``. +//| +//| * In case of receive ``RX``: +//| The number of rx packets captured in the buffer.""" +//| +STATIC mp_obj_t espnow_stats_get_success(const mp_obj_t self_in) { + espnow_stats_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_int_from_uint(self->success); +} +MP_DEFINE_CONST_FUN_OBJ_1(espnow_stats_get_success_obj, espnow_stats_get_success); + +MP_PROPERTY_GETTER(espnow_stats_success_obj, + (mp_obj_t)&espnow_stats_get_success_obj); + +//| failure: int +//| """The number of failures. (read-only) +//| +//| * In case of transmit ``TX``: +//| The number of failed tx packets ``ESP_NOW_SEND_FAIL``. +//| +//| * In case of receive ``RX``: +//| The number of dropped rx packets due to buffer overflow.""" +//| +STATIC mp_obj_t espnow_stats_get_failure(const mp_obj_t self_in) { + espnow_stats_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_int_from_uint(self->failure); +} +MP_DEFINE_CONST_FUN_OBJ_1(espnow_stats_get_failure_obj, espnow_stats_get_failure); + +MP_PROPERTY_GETTER(espnow_stats_failure_obj, + (mp_obj_t)&espnow_stats_get_failure_obj); + +STATIC const mp_rom_map_elem_t espnow_stats_locals_dict_table[] = { + // Peer parameters + { MP_ROM_QSTR(MP_QSTR_success), MP_ROM_PTR(&espnow_stats_success_obj) }, + { MP_ROM_QSTR(MP_QSTR_failure), MP_ROM_PTR(&espnow_stats_failure_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(espnow_stats_locals_dict, espnow_stats_locals_dict_table); + +espnow_stats_obj_t *espnow_stats_new(void) { + espnow_stats_obj_t *self = m_new_obj(espnow_stats_obj_t); + self->base.type = &espnow_stats_type; + return self; +} + +const mp_obj_type_t espnow_stats_type = { + { &mp_type_type }, + .name = MP_QSTR_ESPNowStats, + .locals_dict = (mp_obj_t)&espnow_stats_locals_dict, +}; diff --git a/ports/espressif/bindings/espnow/ESPNowStats.h b/ports/espressif/bindings/espnow/ESPNowStats.h new file mode 100644 index 0000000000..a9a578a8bf --- /dev/null +++ b/ports/espressif/bindings/espnow/ESPNowStats.h @@ -0,0 +1,38 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2023 MicroDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + volatile size_t success; + volatile size_t failure; +} espnow_stats_obj_t; + +const mp_obj_type_t espnow_stats_type; +extern espnow_stats_obj_t *espnow_stats_new(void); diff --git a/ports/espressif/bindings/espnow/Peers.c b/ports/espressif/bindings/espnow/Peers.c index 13c44c3423..843bff644a 100644 --- a/ports/espressif/bindings/espnow/Peers.c +++ b/ports/espressif/bindings/espnow/Peers.c @@ -35,6 +35,8 @@ #include "esp_now.h" +// TODO: Check for deinit + //| class Peers: //| """A class that provides peer managment functions. Sequence[Peer].""" //| diff --git a/ports/espressif/bindings/espnow/__init__.c b/ports/espressif/bindings/espnow/__init__.c index d92221c691..f64a6433dd 100644 --- a/ports/espressif/bindings/espnow/__init__.c +++ b/ports/espressif/bindings/espnow/__init__.c @@ -29,6 +29,7 @@ #include "bindings/espnow/__init__.h" #include "bindings/espnow/ESPNow.h" #include "bindings/espnow/ESPNowPacket.h" +#include "bindings/espnow/ESPNowStats.h" #include "bindings/espnow/Peer.h" #include "bindings/espnow/Peers.h" @@ -51,7 +52,7 @@ //| //| e.send("Starting...") //| for i in range(100): -//| e.send(peer, str(i)*20, True) +//| e.send(str(i)*20) //| e.send(b'end') //| //| **Receiver** @@ -84,6 +85,7 @@ STATIC const mp_rom_map_elem_t espnow_module_globals_table[] = { // module classes { MP_ROM_QSTR(MP_QSTR_ESPNow), MP_ROM_PTR(&espnow_type) }, { MP_ROM_QSTR(MP_QSTR_ESPNowPacket),MP_ROM_PTR(&espnow_packet_type_obj) }, + { MP_ROM_QSTR(MP_QSTR_ESPNowStats), MP_ROM_PTR(&espnow_stats_type) }, { MP_ROM_QSTR(MP_QSTR_Peer), MP_ROM_PTR(&espnow_peer_type) }, { MP_ROM_QSTR(MP_QSTR_Peers), MP_ROM_PTR(&espnow_peers_type) }, }; diff --git a/ports/espressif/common-hal/espnow/ESPNow.c b/ports/espressif/common-hal/espnow/ESPNow.c index 19c0e4a116..96a7cfdfaa 100644 --- a/ports/espressif/common-hal/espnow/ESPNow.c +++ b/ports/espressif/common-hal/espnow/ESPNow.c @@ -45,14 +45,15 @@ // TODO: deinit wifi? -// The maximum length of an espnow packet (bytes) +// The min/max length of an espnow packet (bytes) +#define MIN_PACKET_LEN (sizeof(espnow_packet_t)) #define MAX_PACKET_LEN (sizeof(espnow_packet_t) + ESP_NOW_MAX_DATA_LEN) // Enough for 2 full-size packets: 2 * (6 + 7 + 250) = 526 bytes // Will allocate an additional 7 bytes for buffer overhead #define DEFAULT_RECV_BUFFER_SIZE (2 * MAX_PACKET_LEN) -// Time to wait (millisec) for responses from sent packets: (2 seconds). +// Time to wait (millisec) for responses from sent packets: (1 seconds). #define DEFAULT_SEND_TIMEOUT_MS (1000) // ESPNow packet format for the receive buffer. @@ -70,27 +71,34 @@ typedef struct { uint8_t msg[0]; // Message is up to 250 bytes } __attribute__((packed)) espnow_packet_t; -// Return a pointer to the ESPNow module singleton -static espnow_obj_t *_get_singleton(void) { - return MP_STATE_PORT(espnow_singleton); -} - // --- The ESP-NOW send and recv callback routines --- // Callback triggered when a sent packet is acknowledged by the peer (or not). // Just count the number of responses and number of failures. // These are used in the send() logic. static void send_cb(const uint8_t *mac, esp_now_send_status_t status) { - espnow_obj_t *self = _get_singleton(); - self->tx_responses++; - if (status != ESP_NOW_SEND_SUCCESS) { - self->tx_failures++; + espnow_obj_t *self = MP_STATE_PORT(espnow_singleton); + if (status == ESP_NOW_SEND_SUCCESS) { + self->tx_stats->success++; + } else { + self->tx_stats->failure++; } } -// Get the RSSI value from the wifi packet header -static inline int8_t _get_rssi_from_wifi_packet(const uint8_t *msg) { - // Warning: Secret magic to get the rssi from the wifi packet header +// Callback triggered when an ESP-NOW packet is received. +// Write the peer MAC address and the message into the recv_buffer as an ESPNow packet. +// If the buffer is full, drop the message and increment the dropped count. +static void recv_cb(const uint8_t *mac, const uint8_t *msg, int msg_len) { + espnow_obj_t *self = MP_STATE_PORT(espnow_singleton); + ringbuf_t *buf = self->recv_buffer; + + if (sizeof(espnow_packet_t) + msg_len > ringbuf_num_empty(buf)) { + self->rx_stats->failure++; + return; + } + + // Get the RSSI value from the wifi packet header + // Secret magic to get the rssi from the wifi packet header // See espnow.c:espnow_recv_cb() at https://github.com/espressif/esp-now/ // In the wifi packet the msg comes after a wifi_promiscuous_pkt_t // and a espnow_frame_format_t. @@ -101,38 +109,37 @@ static inline int8_t _get_rssi_from_wifi_packet(const uint8_t *msg) { wifi_promiscuous_pkt_t *wifi_packet = (wifi_promiscuous_pkt_t *)( msg - SIZEOF_ESPNOW_FRAME_FORMAT - sizeof(wifi_promiscuous_pkt_t)); #pragma GCC diagnostic pop - return wifi_packet->rx_ctrl.rssi; -} - -// Callback triggered when an ESP-NOW packet is received. -// Write the peer MAC address and the message into the recv_buffer as an ESPNow packet. -// If the buffer is full, drop the message and increment the dropped count. -static void recv_cb(const uint8_t *mac, const uint8_t *msg, int msg_len) { - espnow_obj_t *self = _get_singleton(); - ringbuf_t *buf = self->recv_buffer; - - if (sizeof(espnow_packet_t) + msg_len > ringbuf_num_empty(buf)) { - self->rx_failures++; - return; - } espnow_header_t header; header.magic = ESPNOW_MAGIC; header.msg_len = msg_len; - header.rssi = _get_rssi_from_wifi_packet(msg); + header.rssi = wifi_packet->rx_ctrl.rssi; header.time_ms = mp_hal_ticks_ms(); ringbuf_put_n(buf, (uint8_t *)&header, sizeof(header)); ringbuf_put_n(buf, mac, ESP_NOW_ETH_ALEN); ringbuf_put_n(buf, msg, msg_len); - self->rx_packets++; + self->rx_stats->success++; } bool common_hal_espnow_deinited(espnow_obj_t *self) { return self == NULL || self->recv_buffer == NULL; } +// Construct the ESPNow object +void common_hal_espnow_construct(espnow_obj_t *self, mp_int_t buffer_size, mp_int_t phy_rate) { + common_hal_espnow_set_buffer_size(self, buffer_size); + common_hal_espnow_set_phy_rate(self, phy_rate); + + self->tx_stats = espnow_stats_new(); + self->rx_stats = espnow_stats_new(); + + self->peers = espnow_peers_new(); + + common_hal_espnow_init(self); +} + // Initialize the ESP-NOW software stack, // register callbacks and allocate the recv data buffers. void common_hal_espnow_init(espnow_obj_t *self) { @@ -171,17 +178,15 @@ void common_hal_espnow_deinit(espnow_obj_t *self) { self->recv_buffer->buf = NULL; self->recv_buffer = NULL; - // self->peers_count = 0; // esp_now_deinit() removes all peers. - self->tx_packets = self->tx_responses; } void espnow_reset(void) { - common_hal_espnow_deinit(_get_singleton()); + common_hal_espnow_deinit(MP_STATE_PORT(espnow_singleton)); MP_STATE_PORT(espnow_singleton) = NULL; } void common_hal_espnow_set_buffer_size(espnow_obj_t *self, mp_int_t value) { - self->recv_buffer_size = mp_arg_validate_int_min(value, MAX_PACKET_LEN, MP_QSTR_buffer_size); + self->recv_buffer_size = mp_arg_validate_int_min(value, MIN_PACKET_LEN, MP_QSTR_buffer_size); }; void common_hal_espnow_set_phy_rate(espnow_obj_t *self, mp_int_t value) { @@ -206,10 +211,6 @@ mp_obj_t common_hal_espnow_send(espnow_obj_t *self, const uint8_t *mac, const mp } CHECK_ESP_RESULT(err); - // Increment the sent packet count. - // If mac == NULL msg will be sent to all peers EXCEPT any broadcast or multicast addresses. - self->tx_packets += ((mac == NULL) ? ((mp_obj_list_t *)self->peers->list)->len : 1); - return mp_const_none; } diff --git a/ports/espressif/common-hal/espnow/ESPNow.h b/ports/espressif/common-hal/espnow/ESPNow.h index 02ca90d5c4..87e3fb7168 100644 --- a/ports/espressif/common-hal/espnow/ESPNow.h +++ b/ports/espressif/common-hal/espnow/ESPNow.h @@ -29,6 +29,7 @@ #include "py/obj.h" #include "py/ringbuf.h" +#include "bindings/espnow/ESPNowStats.h" #include "bindings/espnow/Peers.h" #include "esp_wifi.h" @@ -39,16 +40,14 @@ typedef struct _espnow_obj_t { ringbuf_t *recv_buffer; // A buffer for received packets size_t recv_buffer_size; // The size of the recv_buffer wifi_phy_rate_t phy_rate; // The ESP-NOW physical layer rate. - volatile size_t rx_packets; // # of received packets - volatile size_t rx_failures; // # of dropped packets (buffer full) - size_t tx_packets; // # of sent packets - volatile size_t tx_responses; // # of sent packet responses received - volatile size_t tx_failures; // # of sent packet responses failed - espnow_peers_obj_t *peers; // Cache the # of peers for send(sync=True) + espnow_stats_obj_t *tx_stats; // The tx packet stats + espnow_stats_obj_t *rx_stats; // The rx packet stats + espnow_peers_obj_t *peers; // The sequence of peers } espnow_obj_t; extern void espnow_reset(void); +extern void common_hal_espnow_construct(espnow_obj_t *self, mp_int_t buffer_size, mp_int_t phy_rate); extern void common_hal_espnow_init(espnow_obj_t *self); extern void common_hal_espnow_deinit(espnow_obj_t *self); extern bool common_hal_espnow_deinited(espnow_obj_t *self);