revert `Communicate` class and more

This commit is contained in:
MicroDev 2023-03-09 12:04:50 +05:30
parent 6c54bc9fd9
commit 874ba4ec68
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
8 changed files with 150 additions and 322 deletions

View File

@ -1,201 +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 "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.
//|
//| Terminology:
//| * "Send" = "Transmit" = ``TX``
//| * "Read" = ``RX``
//| """
//|
//| def __init__(self) -> None:
//| """You cannot create an instance of `Communicate`."""
//| ...
//| job: str
//| """Used to decide whether to call `__send` or `__read`. (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,
//| ) -> None:
//| """Send a message to the peer's mac address.
//|
//| This blocks until a timeout of ``2`` seconds if the ESP-NOW internal buffers are full.
//|
//| :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 __read(self) -> Optional[ESPNowPacket]:
//| """Read a packet from the receive buffer.
//|
//| This is non-blocking, the packet is received asynchronously from the peer(s).
//|
//| :returns: An `ESPNowPacket` if available in the buffer, otherwise `None`."""
//| ...
STATIC mp_obj_t espnow_com___read(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_read(self);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_com___read_obj, espnow_com___read);
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___read), MP_ROM_PTR(&espnow_com___read_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 `__read` methods with the supplied ``args`` and ``kwargs``
//| based on whether the job parameter is set to ``send`` or ``read``."""
//| ...
//|
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_read:
meth = MP_OBJ_FROM_PTR(&espnow_com___read_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,
),
};

View File

@ -1,39 +0,0 @@
/*
* 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;
qstr job;
} espnow_com_obj_t;
const mp_obj_type_t espnow_com_type;
extern espnow_com_obj_t *espnow_com_new(qstr job);

View File

@ -31,11 +31,23 @@
#include "py/runtime.h"
#include "py/stream.h"
#include "shared-bindings/util.h"
#include "bindings/espnow/ESPNow.h"
#include "bindings/espnow/Peer.h"
#include "common-hal/espnow/__init__.h"
#include "common-hal/espnow/ESPNow.h"
#include "esp_now.h"
// Raise ValueError if the ESPNow object is deinited
static void espnow_check_for_deinit(espnow_obj_t *self) {
if (common_hal_espnow_deinited(self)) {
raise_deinited_error();
}
}
// --- Initialisation and Config functions ---
//| class ESPNow:
@ -80,7 +92,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);
espnow_check_for_deinit(self);
common_hal_espnow_deinit(self);
return mp_const_none;
}
@ -101,6 +113,111 @@ STATIC mp_obj_t espnow_obj___exit__(size_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow___exit___obj, 4, 4, espnow_obj___exit__);
// --- Send and Read messages ---
//| def send(
//| self,
//| message: ReadableBuffer,
//| peer: Peer,
//| ) -> None:
//| """Send a message to the peer's mac address.
//|
//| This blocks until a timeout of ``2`` seconds if the ESP-NOW internal buffers are full.
//|
//| :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_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];
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_send_obj, 2, espnow_send);
//| def read(self) -> Optional[ESPNowPacket]:
//| """Read a packet from the receive buffer.
//|
//| This is non-blocking, the packet is received asynchronously from the peer(s).
//|
//| :returns: An `ESPNowPacket` if available in the buffer, otherwise `None`."""
//| ...
STATIC mp_obj_t espnow_read(mp_obj_t self_in) {
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
espnow_check_for_deinit(self);
return common_hal_espnow_read(self);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_read_obj, espnow_read);
//| send_success: int
//| """The number of tx packets received by the peer(s) ``ESP_NOW_SEND_SUCCESS``. (read-only)"""
//|
STATIC mp_obj_t espnow_get_send_success(const mp_obj_t self_in) {
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int_from_uint(self->send_success);
}
MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_send_success_obj, espnow_get_send_success);
MP_PROPERTY_GETTER(espnow_send_success_obj,
(mp_obj_t)&espnow_get_send_success_obj);
//| send_failure: int
//| """The number of failed tx packets ``ESP_NOW_SEND_FAIL``. (read-only)"""
//|
STATIC mp_obj_t espnow_send_get_failure(const mp_obj_t self_in) {
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int_from_uint(self->send_failure);
}
MP_DEFINE_CONST_FUN_OBJ_1(espnow_send_get_failure_obj, espnow_send_get_failure);
MP_PROPERTY_GETTER(espnow_send_failure_obj,
(mp_obj_t)&espnow_send_get_failure_obj);
//| read_success: int
//| """The number of rx packets captured in the buffer. (read-only)"""
//|
STATIC mp_obj_t espnow_get_read_success(const mp_obj_t self_in) {
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int_from_uint(self->read_success);
}
MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_read_success_obj, espnow_get_read_success);
MP_PROPERTY_GETTER(espnow_read_success_obj,
(mp_obj_t)&espnow_get_read_success_obj);
//| read_failure: int
//| """The number of dropped rx packets due to buffer overflow. (read-only)"""
//|
STATIC mp_obj_t espnow_read_get_failure(const mp_obj_t self_in) {
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int_from_uint(self->read_failure);
}
MP_DEFINE_CONST_FUN_OBJ_1(espnow_read_get_failure_obj, espnow_read_get_failure);
MP_PROPERTY_GETTER(espnow_read_failure_obj,
(mp_obj_t)&espnow_read_get_failure_obj);
//| def set_pmk(self, pmk: ReadableBuffer) -> None:
//| """Set the ESP-NOW Primary Master Key (pmk) for encrypted communications.
//|
@ -108,32 +225,23 @@ 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);
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;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(espnow_set_pmk_obj, espnow_set_pmk);
//| buffer_size: int
//| """The size of the internal ring buffer."""
//| """The size of the internal ring buffer. (read-only)"""
//|
STATIC mp_obj_t espnow_get_buffer_size(const mp_obj_t self_in) {
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(self->recv_buffer_size);
return mp_obj_new_int_from_uint(self->recv_buffer_size);
}
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;
}
MP_DEFINE_CONST_FUN_OBJ_2(espnow_set_buffer_size_obj, espnow_set_buffer_size);
MP_PROPERTY_GETSET(espnow_buffer_size_obj,
(mp_obj_t)&espnow_get_buffer_size_obj,
(mp_obj_t)&espnow_set_buffer_size_obj);
MP_PROPERTY_GETTER(espnow_buffer_size_obj,
(mp_obj_t)&espnow_get_buffer_size_obj);
//| phy_rate: int
//| """The ESP-NOW physical layer rate."""
@ -146,7 +254,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);
espnow_check_for_deinit(self);
common_hal_espnow_set_phy_rate(self, mp_obj_get_int(value));
return mp_const_none;
}
@ -156,30 +264,6 @@ MP_PROPERTY_GETSET(espnow_phy_rate_obj,
(mp_obj_t)&espnow_get_phy_rate_obj,
(mp_obj_t)&espnow_set_phy_rate_obj);
//| send: Communicate
//| """A `Communicate` object with ``job`` set to ``send``. (read-only)"""
//|
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->send);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_send_obj, espnow_get_send);
MP_PROPERTY_GETTER(espnow_send_obj,
(mp_obj_t)&espnow_get_send_obj);
//| read: Communicate
//| """A `Communicate` object with ``job`` set to ``read``. (read-only)"""
//|
STATIC mp_obj_t espnow_get_read(mp_obj_t self_in) {
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_FROM_PTR(self->read);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_read_obj, espnow_get_read);
MP_PROPERTY_GETTER(espnow_read_obj,
(mp_obj_t)&espnow_get_read_obj);
// --- Peer Related Properties ---
//| peers: Peers
@ -202,15 +286,21 @@ STATIC const mp_rom_map_elem_t espnow_locals_dict_table[] = {
// Deinit the object
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&espnow_deinit_obj) },
// Send messages
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&espnow_send_obj) },
{ MP_ROM_QSTR(MP_QSTR_send_success),MP_ROM_PTR(&espnow_send_success_obj)},
{ MP_ROM_QSTR(MP_QSTR_send_failure),MP_ROM_PTR(&espnow_send_failure_obj)},
// Read messages
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&espnow_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_success),MP_ROM_PTR(&espnow_read_success_obj)},
{ MP_ROM_QSTR(MP_QSTR_read_failure),MP_ROM_PTR(&espnow_read_failure_obj)},
// Config parameters
{ MP_ROM_QSTR(MP_QSTR_set_pmk), MP_ROM_PTR(&espnow_set_pmk_obj) },
{ 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) },
// Send and receive messages
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&espnow_send_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&espnow_read_obj) },
// Peer related properties
{ MP_ROM_QSTR(MP_QSTR_peers), MP_ROM_PTR(&espnow_peers_obj) },
};
@ -222,7 +312,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);
common_hal_espnow_check_for_deinit(self);
espnow_check_for_deinit(self);
switch (request) {
case MP_STREAM_POLL: {
mp_uint_t flags = arg;
@ -254,7 +344,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);
espnow_check_for_deinit(self);
size_t len = ringbuf_num_filled(self->recv_buffer);
switch (op) {
case MP_UNARY_OP_BOOL:

View File

@ -27,7 +27,6 @@
#include "bindings/espnow/__init__.h"
#include "bindings/espnow/ESPNow.h"
#include "bindings/espnow/ESPNowPacket.h"
#include "bindings/espnow/Communicate.h"
#include "bindings/espnow/Peer.h"
#include "bindings/espnow/Peers.h"
@ -83,7 +82,6 @@ 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_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) },
};

View File

@ -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->send->success++;
self->send_success++;
} else {
self->send->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->read->failure++;
self->read_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->read->success++;
self->read_success++;
}
bool common_hal_espnow_deinited(espnow_obj_t *self) {
@ -129,14 +129,9 @@ bool common_hal_espnow_deinited(espnow_obj_t *self) {
// 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->send = espnow_com_new(MP_QSTR_send);
self->read = espnow_com_new(MP_QSTR_read);
self->recv_buffer_size = mp_arg_validate_int_min(buffer_size, MIN_PACKET_LEN, MP_QSTR_buffer_size);
self->peers = espnow_peers_new();
common_hal_espnow_init(self);
}
@ -185,10 +180,6 @@ void espnow_reset(void) {
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, MIN_PACKET_LEN, MP_QSTR_buffer_size);
};
void common_hal_espnow_set_phy_rate(espnow_obj_t *self, mp_int_t value) {
self->phy_rate = mp_arg_validate_int_range(value, 0, WIFI_PHY_RATE_MAX - 1, MP_QSTR_phy_rate);
};

View File

@ -29,20 +29,20 @@
#include "py/obj.h"
#include "py/ringbuf.h"
#include "bindings/espnow/Communicate.h"
#include "bindings/espnow/Peers.h"
#include "esp_wifi.h"
// 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_com_obj_t *send; // For keeping tx stats
espnow_com_obj_t *read; // For keeping rx stats
espnow_peers_obj_t *peers; // The sequence of peers
ringbuf_t *recv_buffer;
size_t recv_buffer_size;
wifi_phy_rate_t phy_rate;
espnow_peers_obj_t *peers;
volatile size_t send_success;
volatile size_t send_failure;
volatile size_t read_success;
volatile size_t read_failure;
} espnow_obj_t;
extern void espnow_reset(void);
@ -52,7 +52,6 @@ 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);
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);

View File

@ -27,14 +27,6 @@
#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.

View File

@ -26,7 +26,5 @@
#pragma once
#include "common-hal/espnow/ESPNow.h"
extern void common_hal_espnow_check_for_deinit(espnow_obj_t *self);
#include "py/obj.h"
extern const uint8_t *common_hal_espnow_get_bytes_len(mp_obj_t obj, size_t len);