replace ESPNowStats
with Communicate
class and more
This commit is contained in:
parent
ff95e96160
commit
c0a9c71057
197
ports/espressif/bindings/espnow/Communicate.c
Normal file
197
ports/espressif/bindings/espnow/Communicate.c
Normal file
@ -0,0 +1,197 @@
|
||||
/*
|
||||
* 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 "py/objproperty.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "bindings/espnow/Peer.h"
|
||||
#include "common-hal/espnow/__init__.h"
|
||||
|
||||
// --- Send and Receive ESP-NOW data ---
|
||||
|
||||
//| class Communicate:
|
||||
//| """Provides methods and statistics related to communication
|
||||
//| with the ESP-NOW peers.
|
||||
//|
|
||||
//| Dictionary:
|
||||
//| * "Send" = "Transmit" = ``TX``
|
||||
//| * "Recv" = "Receive" = ``RX``
|
||||
//| """
|
||||
//|
|
||||
//| def __init__(self) -> None:
|
||||
//| """You cannot create an instance of `Communicate`."""
|
||||
//| ...
|
||||
|
||||
//| job: str
|
||||
//| """Used to decide whether to call `__send` or `__recv`. (read-only)"""
|
||||
//|
|
||||
STATIC mp_obj_t espnow_com_get_job(const mp_obj_t self_in) {
|
||||
espnow_com_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
return MP_OBJ_NEW_QSTR(self->job);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(espnow_com_get_job_obj, espnow_com_get_job);
|
||||
|
||||
MP_PROPERTY_GETTER(espnow_com_job_obj,
|
||||
(mp_obj_t)&espnow_com_get_job_obj);
|
||||
|
||||
//| 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_com_get_success(const mp_obj_t self_in) {
|
||||
espnow_com_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_com_get_success_obj, espnow_com_get_success);
|
||||
|
||||
MP_PROPERTY_GETTER(espnow_com_success_obj,
|
||||
(mp_obj_t)&espnow_com_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_com_get_failure(const mp_obj_t self_in) {
|
||||
espnow_com_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_com_get_failure_obj, espnow_com_get_failure);
|
||||
|
||||
MP_PROPERTY_GETTER(espnow_com_failure_obj,
|
||||
(mp_obj_t)&espnow_com_get_failure_obj);
|
||||
|
||||
//| def __send(
|
||||
//| self,
|
||||
//| message: ReadableBuffer,
|
||||
//| peer: Peer,
|
||||
//| ) -> bool:
|
||||
//| """Send a message to the peer's mac address.
|
||||
//|
|
||||
//| :param ReadableBuffer message: The message to send (length <= 250 bytes).
|
||||
//| :param Peer peer: Send message to this peer. If `None`, send to all registered peers.
|
||||
//| """
|
||||
//| ...
|
||||
STATIC mp_obj_t espnow_com___send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_message, ARG_peer };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_message, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
||||
{ MP_QSTR_peer, MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
||||
};
|
||||
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
espnow_obj_t *self = pos_args[0];
|
||||
common_hal_espnow_check_for_deinit(self);
|
||||
|
||||
// Get a pointer to the data buffer of the message
|
||||
mp_buffer_info_t message;
|
||||
mp_get_buffer_raise(args[ARG_message].u_obj, &message, MP_BUFFER_READ);
|
||||
|
||||
const uint8_t *mac = NULL;
|
||||
if (args[ARG_peer].u_obj != mp_const_none) {
|
||||
const espnow_peer_obj_t *peer = MP_OBJ_FROM_PTR(mp_arg_validate_type_or_none(args[ARG_peer].u_obj, &espnow_peer_type, MP_QSTR_peer));
|
||||
mac = peer->peer_info.peer_addr;
|
||||
}
|
||||
|
||||
return common_hal_espnow_send(self, &message, mac);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(espnow_com___send_obj, 2, espnow_com___send);
|
||||
|
||||
//| def __recv(self) -> Optional[ESPNowPacket]:
|
||||
//| """Receive a message from the peer(s).
|
||||
//|
|
||||
//| :returns: An `ESPNowPacket` if available in the buffer, otherwise `None`."""
|
||||
//| ...
|
||||
STATIC mp_obj_t espnow_com___recv(mp_obj_t self_in) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
common_hal_espnow_check_for_deinit(self);
|
||||
|
||||
return common_hal_espnow_recv(self);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_com___recv_obj, espnow_com___recv);
|
||||
|
||||
STATIC const mp_rom_map_elem_t espnow_com_locals_dict_table[] = {
|
||||
// Config parameters
|
||||
{ MP_ROM_QSTR(MP_QSTR_job), MP_ROM_PTR(&espnow_com_job_obj) },
|
||||
|
||||
// Packet statistics
|
||||
{ MP_ROM_QSTR(MP_QSTR_success), MP_ROM_PTR(&espnow_com_success_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_failure), MP_ROM_PTR(&espnow_com_failure_obj) },
|
||||
|
||||
// Communication methods
|
||||
{ MP_ROM_QSTR(MP_QSTR___send), MP_ROM_PTR(&espnow_com___send_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___recv), MP_ROM_PTR(&espnow_com___recv_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(espnow_com_locals_dict, espnow_com_locals_dict_table);
|
||||
|
||||
//| def __call__(self, *args: Optional[Any], **kwargs: Optional[Any]) -> Optional[Any]:
|
||||
//| """Calls the private `__send` or `__recv` methods with the supplied ``args`` and ``kwargs``
|
||||
//| based on whether the job parameter is set to ``send`` or ``recv``."""
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t espnow_com_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||
espnow_com_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_obj_t meth = NULL;
|
||||
switch (self->job) {
|
||||
case MP_QSTR_send:
|
||||
meth = MP_OBJ_FROM_PTR(&espnow_com___send_obj);
|
||||
break;
|
||||
case MP_QSTR_recv:
|
||||
meth = MP_OBJ_FROM_PTR(&espnow_com___recv_obj);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return meth ? mp_call_method_self_n_kw(meth, MP_STATE_PORT(espnow_singleton), n_args, n_kw, args) : mp_const_none;
|
||||
}
|
||||
|
||||
espnow_com_obj_t *espnow_com_new(qstr job) {
|
||||
espnow_com_obj_t *self = m_new_obj(espnow_com_obj_t);
|
||||
self->base.type = &espnow_com_type;
|
||||
self->job = job;
|
||||
return self;
|
||||
}
|
||||
|
||||
const mp_obj_type_t espnow_com_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Communicate,
|
||||
.locals_dict = (mp_obj_t)&espnow_com_locals_dict,
|
||||
.flags = MP_TYPE_FLAG_EXTENDED,
|
||||
MP_TYPE_EXTENDED_FIELDS(
|
||||
.call = &espnow_com_call,
|
||||
),
|
||||
};
|
@ -32,7 +32,8 @@ typedef struct {
|
||||
mp_obj_base_t base;
|
||||
volatile size_t success;
|
||||
volatile size_t failure;
|
||||
} espnow_stats_obj_t;
|
||||
qstr job;
|
||||
} espnow_com_obj_t;
|
||||
|
||||
const mp_obj_type_t espnow_stats_type;
|
||||
extern espnow_stats_obj_t *espnow_stats_new(void);
|
||||
const mp_obj_type_t espnow_com_type;
|
||||
extern espnow_com_obj_t *espnow_com_new(qstr job);
|
@ -32,22 +32,12 @@
|
||||
#include "py/stream.h"
|
||||
|
||||
#include "bindings/espnow/ESPNow.h"
|
||||
|
||||
#include "shared-bindings/util.h"
|
||||
|
||||
#include "common-hal/espnow/__init__.h"
|
||||
#include "common-hal/espnow/ESPNow.h"
|
||||
|
||||
#include "esp_now.h"
|
||||
|
||||
// --- Initialisation and Config functions ---
|
||||
|
||||
static void check_for_deinit(espnow_obj_t *self) {
|
||||
if (common_hal_espnow_deinited(self)) {
|
||||
raise_deinited_error();
|
||||
}
|
||||
}
|
||||
|
||||
//| class ESPNow:
|
||||
//| """Provides access to the ESP-NOW protocol."""
|
||||
//|
|
||||
@ -82,7 +72,7 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, size_t
|
||||
|
||||
// Set the global singleton pointer for the espnow protocol.
|
||||
MP_STATE_PORT(espnow_singleton) = self;
|
||||
return self;
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
//| def deinit(self) -> None:
|
||||
@ -90,6 +80,7 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, size_t
|
||||
//| ...
|
||||
STATIC mp_obj_t espnow_deinit(mp_obj_t self_in) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
common_hal_espnow_check_for_deinit(self);
|
||||
common_hal_espnow_deinit(self);
|
||||
return mp_const_none;
|
||||
}
|
||||
@ -117,6 +108,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow___exit___obj, 4, 4, espnow_obj
|
||||
//| ...
|
||||
STATIC mp_obj_t espnow_set_pmk(mp_obj_t self_in, mp_obj_t key) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
common_hal_espnow_check_for_deinit(self);
|
||||
common_hal_espnow_set_pmk(self, common_hal_espnow_get_bytes_len(key, ESP_NOW_KEY_LEN));
|
||||
return mp_const_none;
|
||||
}
|
||||
@ -133,6 +125,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_buffer_size_obj, espnow_get_buffer_size);
|
||||
|
||||
STATIC mp_obj_t espnow_set_buffer_size(const mp_obj_t self_in, const mp_obj_t value) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
common_hal_espnow_check_for_deinit(self);
|
||||
common_hal_espnow_set_buffer_size(self, mp_obj_get_int(value));
|
||||
return mp_const_none;
|
||||
}
|
||||
@ -153,6 +146,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_phy_rate_obj, espnow_get_phy_rate);
|
||||
|
||||
STATIC mp_obj_t espnow_set_phy_rate(const mp_obj_t self_in, const mp_obj_t value) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
common_hal_espnow_check_for_deinit(self);
|
||||
common_hal_espnow_set_phy_rate(self, mp_obj_get_int(value));
|
||||
return mp_const_none;
|
||||
}
|
||||
@ -162,78 +156,29 @@ MP_PROPERTY_GETSET(espnow_phy_rate_obj,
|
||||
(mp_obj_t)&espnow_get_phy_rate_obj,
|
||||
(mp_obj_t)&espnow_set_phy_rate_obj);
|
||||
|
||||
//| tx_stats: ESPNowStats
|
||||
//| """The ``TX`` packet statistics."""
|
||||
//| send: Communicate
|
||||
//| """A `Communicate` object with ``job`` set to ``send``. (read-only)"""
|
||||
//|
|
||||
STATIC mp_obj_t espnow_get_tx_stats(mp_obj_t self_in) {
|
||||
STATIC mp_obj_t espnow_get_send(mp_obj_t self_in) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
return MP_OBJ_FROM_PTR(self->tx_stats);
|
||||
return MP_OBJ_FROM_PTR(self->send);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_tx_stats_obj, espnow_get_tx_stats);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_send_obj, espnow_get_send);
|
||||
|
||||
MP_PROPERTY_GETTER(espnow_tx_stats_obj,
|
||||
(mp_obj_t)&espnow_get_tx_stats_obj);
|
||||
MP_PROPERTY_GETTER(espnow_send_call_obj,
|
||||
(mp_obj_t)&espnow_get_send_obj);
|
||||
|
||||
//| rx_stats: ESPNowStats
|
||||
//| """The ``RX`` packet statistics."""
|
||||
//| recv: Communicate
|
||||
//| """A `Communicate` object with ``job`` set to ``recv``. (read-only)"""
|
||||
//|
|
||||
STATIC mp_obj_t espnow_get_rx_stats(mp_obj_t self_in) {
|
||||
STATIC mp_obj_t espnow_get_recv(mp_obj_t self_in) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
return MP_OBJ_FROM_PTR(self->rx_stats);
|
||||
return MP_OBJ_FROM_PTR(self->recv);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_rx_stats_obj, espnow_get_rx_stats);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_recv_obj, espnow_get_recv);
|
||||
|
||||
MP_PROPERTY_GETTER(espnow_rx_stats_obj,
|
||||
(mp_obj_t)&espnow_get_rx_stats_obj);
|
||||
|
||||
// --- Send and Receive ESP-NOW data ---
|
||||
|
||||
//| def send(
|
||||
//| self,
|
||||
//| message: ReadableBuffer,
|
||||
//| mac: Optional[ReadableBuffer],
|
||||
//| ) -> bool:
|
||||
//| """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."""
|
||||
//| ...
|
||||
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 };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_message, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
||||
{ MP_QSTR_mac, MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
||||
};
|
||||
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
espnow_obj_t *self = pos_args[0];
|
||||
check_for_deinit(self);
|
||||
|
||||
const uint8_t *peer_addr = common_hal_espnow_get_bytes_len(args[ARG_mac].u_obj, ESP_NOW_ETH_ALEN);
|
||||
|
||||
// Get a pointer to the data buffer of the message
|
||||
mp_buffer_info_t message;
|
||||
mp_get_buffer_raise(args[ARG_message].u_obj, &message, MP_BUFFER_READ);
|
||||
|
||||
return common_hal_espnow_send(self, peer_addr, &message);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(espnow_send_obj, 2, espnow_send);
|
||||
|
||||
//| def recv(self) -> Optional[ESPNowPacket]:
|
||||
//| """Receive a message from the peer(s).
|
||||
//|
|
||||
//| :returns: An `ESPNowPacket` if available in the buffer, otherwise `None`."""
|
||||
//| ...
|
||||
STATIC mp_obj_t espnow_recv(mp_obj_t self_in) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
check_for_deinit(self);
|
||||
|
||||
return common_hal_espnow_recv(self);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_recv_obj, espnow_recv);
|
||||
MP_PROPERTY_GETTER(espnow_recv_call_obj,
|
||||
(mp_obj_t)&espnow_get_recv_obj);
|
||||
|
||||
// --- Peer Related Properties ---
|
||||
|
||||
@ -242,7 +187,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_recv_obj, espnow_recv);
|
||||
//|
|
||||
STATIC mp_obj_t espnow_get_peers(mp_obj_t self_in) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
check_for_deinit(self);
|
||||
return MP_OBJ_FROM_PTR(self->peers);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_peers_obj, espnow_get_peers);
|
||||
@ -263,13 +207,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) },
|
||||
|
||||
// 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) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&espnow_recv_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&espnow_send_call_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&espnow_recv_call_obj) },
|
||||
|
||||
// Peer related properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_peers), MP_ROM_PTR(&espnow_peers_obj) },
|
||||
@ -282,7 +222,7 @@ 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) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
check_for_deinit(self);
|
||||
common_hal_espnow_check_for_deinit(self);
|
||||
switch (request) {
|
||||
case MP_STREAM_POLL: {
|
||||
mp_uint_t flags = arg;
|
||||
@ -314,6 +254,7 @@ STATIC const mp_stream_p_t espnow_stream_p = {
|
||||
//|
|
||||
STATIC mp_obj_t espnow_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
common_hal_espnow_check_for_deinit(self);
|
||||
size_t len = ringbuf_num_filled(self->recv_buffer);
|
||||
switch (op) {
|
||||
case MP_UNARY_OP_BOOL:
|
||||
|
@ -27,19 +27,19 @@
|
||||
#include "bindings/espnow/ESPNowPacket.h"
|
||||
|
||||
//| class ESPNowPacket:
|
||||
//| """A packet retrieved from ESP-NOW communication protocol"""
|
||||
//| """A packet retrieved from ESP-NOW communication protocol. A namedtuple."""
|
||||
//|
|
||||
//| mac: ReadableBuffer
|
||||
//| """The sender's mac address (length = 6 bytes)"""
|
||||
//| """The sender's mac address (length = 6 bytes)."""
|
||||
//|
|
||||
//| msg: ReadableBuffer
|
||||
//| """The message sent by the peer (length <= 250 bytes)"""
|
||||
//| """The message sent by the peer (length <= 250 bytes)."""
|
||||
//|
|
||||
//| rssi: int
|
||||
//| """The received signal strength indication (in dBm from -127 to 0)"""
|
||||
//| """The received signal strength indication (in dBm from -127 to 0)."""
|
||||
//|
|
||||
//| time: int
|
||||
//| """The time in milliseconds since the device last booted when the packet was received"""
|
||||
//| """The time in milliseconds since the device last booted when the packet was received."""
|
||||
//|
|
||||
|
||||
const mp_obj_namedtuple_type_t espnow_packet_type_obj = {
|
||||
|
@ -1,87 +0,0 @@
|
||||
/*
|
||||
* 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,
|
||||
};
|
@ -40,6 +40,9 @@
|
||||
//| class Peers:
|
||||
//| """A class that provides peer managment functions. Sequence[Peer]."""
|
||||
//|
|
||||
//| def __init__(self) -> None:
|
||||
//| """You cannot create an instance of `Peers`."""
|
||||
//| ...
|
||||
|
||||
//| def append(self, peer: Peer) -> None:
|
||||
//| """Append peer.
|
||||
@ -195,7 +198,6 @@ const mp_obj_type_t espnow_peers_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Peers,
|
||||
.print = espnow_peers_print,
|
||||
// .make_new = espnow_peers_make_new,
|
||||
.locals_dict = (mp_obj_t)&espnow_peers_locals_dict,
|
||||
.flags = MP_TYPE_FLAG_EXTENDED,
|
||||
MP_TYPE_EXTENDED_FIELDS(
|
||||
@ -204,16 +206,3 @@ const mp_obj_type_t espnow_peers_type = {
|
||||
.getiter = espnow_peers_getiter,
|
||||
),
|
||||
};
|
||||
|
||||
/*
|
||||
STATIC mp_obj_t espnow_peers_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||
(void)type_in;
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
|
||||
espnow_peers_obj_t *self = m_new_obj(espnow_peers_obj_t);
|
||||
self->base.type = &espnow_peers_type;
|
||||
self->list = mp_obj_new_list_from_iter(args[0]);
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
*/
|
||||
|
@ -24,12 +24,10 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "shared-bindings/util.h"
|
||||
|
||||
#include "bindings/espnow/__init__.h"
|
||||
#include "bindings/espnow/ESPNow.h"
|
||||
#include "bindings/espnow/ESPNowPacket.h"
|
||||
#include "bindings/espnow/ESPNowStats.h"
|
||||
#include "bindings/espnow/Communicate.h"
|
||||
#include "bindings/espnow/Peer.h"
|
||||
#include "bindings/espnow/Peers.h"
|
||||
|
||||
@ -51,7 +49,7 @@
|
||||
//| e.peers.append(peer)
|
||||
//|
|
||||
//| e.send("Starting...")
|
||||
//| for i in range(100):
|
||||
//| for i in range(10):
|
||||
//| e.send(str(i)*20)
|
||||
//| e.send(b'end')
|
||||
//|
|
||||
@ -85,7 +83,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_Communicate), MP_ROM_PTR(&espnow_com_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) },
|
||||
};
|
||||
|
@ -53,8 +53,8 @@
|
||||
// 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: (1 seconds).
|
||||
#define DEFAULT_SEND_TIMEOUT_MS (1000)
|
||||
// Time to wait (millisec) for responses from sent packets: (2 seconds).
|
||||
#define DEFAULT_SEND_TIMEOUT_MS (2000)
|
||||
|
||||
// ESPNow packet format for the receive buffer.
|
||||
// Use this for peeking at the header of the next packet in the buffer.
|
||||
@ -79,9 +79,9 @@ typedef struct {
|
||||
static void send_cb(const uint8_t *mac, esp_now_send_status_t status) {
|
||||
espnow_obj_t *self = MP_STATE_PORT(espnow_singleton);
|
||||
if (status == ESP_NOW_SEND_SUCCESS) {
|
||||
self->tx_stats->success++;
|
||||
self->send->success++;
|
||||
} else {
|
||||
self->tx_stats->failure++;
|
||||
self->send->failure++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,7 +93,7 @@ static void recv_cb(const uint8_t *mac, const uint8_t *msg, int msg_len) {
|
||||
ringbuf_t *buf = self->recv_buffer;
|
||||
|
||||
if (sizeof(espnow_packet_t) + msg_len > ringbuf_num_empty(buf)) {
|
||||
self->rx_stats->failure++;
|
||||
self->recv->failure++;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -120,7 +120,7 @@ static void recv_cb(const uint8_t *mac, const uint8_t *msg, int msg_len) {
|
||||
ringbuf_put_n(buf, mac, ESP_NOW_ETH_ALEN);
|
||||
ringbuf_put_n(buf, msg, msg_len);
|
||||
|
||||
self->rx_stats->success++;
|
||||
self->recv->success++;
|
||||
}
|
||||
|
||||
bool common_hal_espnow_deinited(espnow_obj_t *self) {
|
||||
@ -132,8 +132,8 @@ void common_hal_espnow_construct(espnow_obj_t *self, mp_int_t buffer_size, mp_in
|
||||
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->send = espnow_com_new(MP_QSTR_send);
|
||||
self->recv = espnow_com_new(MP_QSTR_recv);
|
||||
|
||||
self->peers = espnow_peers_new();
|
||||
|
||||
@ -200,7 +200,7 @@ void common_hal_espnow_set_pmk(espnow_obj_t *self, const uint8_t *key) {
|
||||
// --- Send and Receive ESP-NOW data ---
|
||||
|
||||
|
||||
mp_obj_t common_hal_espnow_send(espnow_obj_t *self, const uint8_t *mac, const mp_buffer_info_t *message) {
|
||||
mp_obj_t common_hal_espnow_send(espnow_obj_t *self, const mp_buffer_info_t *message, const uint8_t *mac) {
|
||||
// Send the packet - keep trying until timeout if the internal esp-now buffers are full.
|
||||
esp_err_t err;
|
||||
mp_uint_t start = mp_hal_ticks_ms();
|
||||
|
@ -29,7 +29,7 @@
|
||||
#include "py/obj.h"
|
||||
#include "py/ringbuf.h"
|
||||
|
||||
#include "bindings/espnow/ESPNowStats.h"
|
||||
#include "bindings/espnow/Communicate.h"
|
||||
#include "bindings/espnow/Peers.h"
|
||||
|
||||
#include "esp_wifi.h"
|
||||
@ -37,12 +37,12 @@
|
||||
// The data structure for the espnow_singleton.
|
||||
typedef struct _espnow_obj_t {
|
||||
mp_obj_base_t base;
|
||||
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.
|
||||
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
|
||||
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.
|
||||
espnow_com_obj_t *send; // The tx packet stats
|
||||
espnow_com_obj_t *recv; // The rx packet stats
|
||||
espnow_peers_obj_t *peers; // The sequence of peers
|
||||
} espnow_obj_t;
|
||||
|
||||
extern void espnow_reset(void);
|
||||
@ -56,5 +56,5 @@ extern void common_hal_espnow_set_buffer_size(espnow_obj_t *self, mp_int_t value
|
||||
extern void common_hal_espnow_set_phy_rate(espnow_obj_t *self, mp_int_t value);
|
||||
extern void common_hal_espnow_set_pmk(espnow_obj_t *self, const uint8_t *key);
|
||||
|
||||
extern mp_obj_t common_hal_espnow_send(espnow_obj_t *self, const uint8_t *mac, const mp_buffer_info_t *message);
|
||||
extern mp_obj_t common_hal_espnow_send(espnow_obj_t *self, const mp_buffer_info_t *message, const uint8_t *mac);
|
||||
extern mp_obj_t common_hal_espnow_recv(espnow_obj_t *self);
|
||||
|
@ -25,7 +25,16 @@
|
||||
*/
|
||||
|
||||
#include "common-hal/espnow/__init__.h"
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/util.h"
|
||||
|
||||
// Raise ValueError if the ESPNow object is deinited
|
||||
void common_hal_espnow_check_for_deinit(espnow_obj_t *self) {
|
||||
if (common_hal_espnow_deinited(self)) {
|
||||
raise_deinited_error();
|
||||
}
|
||||
}
|
||||
|
||||
// Return C pointer to byte memory string/bytes/bytearray in obj.
|
||||
// Raise ValueError if the length does not match expected len.
|
||||
|
@ -26,5 +26,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "common-hal/espnow/ESPNow.h"
|
||||
|
||||
extern void common_hal_espnow_check_for_deinit(espnow_obj_t *self);
|
||||
extern const uint8_t *common_hal_espnow_get_bytes_len(mp_obj_t obj, size_t len);
|
||||
|
Loading…
Reference in New Issue
Block a user