Skip to content

Commit c0a9c71

Browse files
committed
replace ESPNowStats with Communicate class and more
1 parent ff95e96 commit c0a9c71

File tree

11 files changed

+264
-214
lines changed

11 files changed

+264
-214
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 MicroDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/objproperty.h"
28+
#include "py/runtime.h"
29+
30+
#include "bindings/espnow/Peer.h"
31+
#include "common-hal/espnow/__init__.h"
32+
33+
// --- Send and Receive ESP-NOW data ---
34+
35+
//| class Communicate:
36+
//| """Provides methods and statistics related to communication
37+
//| with the ESP-NOW peers.
38+
//|
39+
//| Dictionary:
40+
//| * "Send" = "Transmit" = ``TX``
41+
//| * "Recv" = "Receive" = ``RX``
42+
//| """
43+
//|
44+
//| def __init__(self) -> None:
45+
//| """You cannot create an instance of `Communicate`."""
46+
//| ...
47+
48+
//| job: str
49+
//| """Used to decide whether to call `__send` or `__recv`. (read-only)"""
50+
//|
51+
STATIC mp_obj_t espnow_com_get_job(const mp_obj_t self_in) {
52+
espnow_com_obj_t *self = MP_OBJ_TO_PTR(self_in);
53+
return MP_OBJ_NEW_QSTR(self->job);
54+
}
55+
MP_DEFINE_CONST_FUN_OBJ_1(espnow_com_get_job_obj, espnow_com_get_job);
56+
57+
MP_PROPERTY_GETTER(espnow_com_job_obj,
58+
(mp_obj_t)&espnow_com_get_job_obj);
59+
60+
//| success: int
61+
//| """The number of successes. (read-only)
62+
//|
63+
//| * In case of transmit ``TX``:
64+
//| The number of tx packets received by the peer(s) ``ESP_NOW_SEND_SUCCESS``.
65+
//|
66+
//| * In case of receive ``RX``:
67+
//| The number of rx packets captured in the buffer."""
68+
//|
69+
STATIC mp_obj_t espnow_com_get_success(const mp_obj_t self_in) {
70+
espnow_com_obj_t *self = MP_OBJ_TO_PTR(self_in);
71+
return mp_obj_new_int_from_uint(self->success);
72+
}
73+
MP_DEFINE_CONST_FUN_OBJ_1(espnow_com_get_success_obj, espnow_com_get_success);
74+
75+
MP_PROPERTY_GETTER(espnow_com_success_obj,
76+
(mp_obj_t)&espnow_com_get_success_obj);
77+
78+
//| failure: int
79+
//| """The number of failures. (read-only)
80+
//|
81+
//| * In case of transmit ``TX``:
82+
//| The number of failed tx packets ``ESP_NOW_SEND_FAIL``.
83+
//|
84+
//| * In case of receive ``RX``:
85+
//| The number of dropped rx packets due to buffer overflow."""
86+
//|
87+
STATIC mp_obj_t espnow_com_get_failure(const mp_obj_t self_in) {
88+
espnow_com_obj_t *self = MP_OBJ_TO_PTR(self_in);
89+
return mp_obj_new_int_from_uint(self->failure);
90+
}
91+
MP_DEFINE_CONST_FUN_OBJ_1(espnow_com_get_failure_obj, espnow_com_get_failure);
92+
93+
MP_PROPERTY_GETTER(espnow_com_failure_obj,
94+
(mp_obj_t)&espnow_com_get_failure_obj);
95+
96+
//| def __send(
97+
//| self,
98+
//| message: ReadableBuffer,
99+
//| peer: Peer,
100+
//| ) -> bool:
101+
//| """Send a message to the peer's mac address.
102+
//|
103+
//| :param ReadableBuffer message: The message to send (length <= 250 bytes).
104+
//| :param Peer peer: Send message to this peer. If `None`, send to all registered peers.
105+
//| """
106+
//| ...
107+
STATIC mp_obj_t espnow_com___send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
108+
enum { ARG_message, ARG_peer };
109+
static const mp_arg_t allowed_args[] = {
110+
{ MP_QSTR_message, MP_ARG_OBJ | MP_ARG_REQUIRED },
111+
{ MP_QSTR_peer, MP_ARG_OBJ, { .u_obj = mp_const_none } },
112+
};
113+
114+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
115+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
116+
117+
espnow_obj_t *self = pos_args[0];
118+
common_hal_espnow_check_for_deinit(self);
119+
120+
// Get a pointer to the data buffer of the message
121+
mp_buffer_info_t message;
122+
mp_get_buffer_raise(args[ARG_message].u_obj, &message, MP_BUFFER_READ);
123+
124+
const uint8_t *mac = NULL;
125+
if (args[ARG_peer].u_obj != mp_const_none) {
126+
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));
127+
mac = peer->peer_info.peer_addr;
128+
}
129+
130+
return common_hal_espnow_send(self, &message, mac);
131+
}
132+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(espnow_com___send_obj, 2, espnow_com___send);
133+
134+
//| def __recv(self) -> Optional[ESPNowPacket]:
135+
//| """Receive a message from the peer(s).
136+
//|
137+
//| :returns: An `ESPNowPacket` if available in the buffer, otherwise `None`."""
138+
//| ...
139+
STATIC mp_obj_t espnow_com___recv(mp_obj_t self_in) {
140+
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
141+
common_hal_espnow_check_for_deinit(self);
142+
143+
return common_hal_espnow_recv(self);
144+
}
145+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_com___recv_obj, espnow_com___recv);
146+
147+
STATIC const mp_rom_map_elem_t espnow_com_locals_dict_table[] = {
148+
// Config parameters
149+
{ MP_ROM_QSTR(MP_QSTR_job), MP_ROM_PTR(&espnow_com_job_obj) },
150+
151+
// Packet statistics
152+
{ MP_ROM_QSTR(MP_QSTR_success), MP_ROM_PTR(&espnow_com_success_obj) },
153+
{ MP_ROM_QSTR(MP_QSTR_failure), MP_ROM_PTR(&espnow_com_failure_obj) },
154+
155+
// Communication methods
156+
{ MP_ROM_QSTR(MP_QSTR___send), MP_ROM_PTR(&espnow_com___send_obj) },
157+
{ MP_ROM_QSTR(MP_QSTR___recv), MP_ROM_PTR(&espnow_com___recv_obj) },
158+
};
159+
STATIC MP_DEFINE_CONST_DICT(espnow_com_locals_dict, espnow_com_locals_dict_table);
160+
161+
//| def __call__(self, *args: Optional[Any], **kwargs: Optional[Any]) -> Optional[Any]:
162+
//| """Calls the private `__send` or `__recv` methods with the supplied ``args`` and ``kwargs``
163+
//| based on whether the job parameter is set to ``send`` or ``recv``."""
164+
//| ...
165+
//|
166+
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) {
167+
espnow_com_obj_t *self = MP_OBJ_TO_PTR(self_in);
168+
mp_obj_t meth = NULL;
169+
switch (self->job) {
170+
case MP_QSTR_send:
171+
meth = MP_OBJ_FROM_PTR(&espnow_com___send_obj);
172+
break;
173+
case MP_QSTR_recv:
174+
meth = MP_OBJ_FROM_PTR(&espnow_com___recv_obj);
175+
break;
176+
default:
177+
break;
178+
}
179+
return meth ? mp_call_method_self_n_kw(meth, MP_STATE_PORT(espnow_singleton), n_args, n_kw, args) : mp_const_none;
180+
}
181+
182+
espnow_com_obj_t *espnow_com_new(qstr job) {
183+
espnow_com_obj_t *self = m_new_obj(espnow_com_obj_t);
184+
self->base.type = &espnow_com_type;
185+
self->job = job;
186+
return self;
187+
}
188+
189+
const mp_obj_type_t espnow_com_type = {
190+
{ &mp_type_type },
191+
.name = MP_QSTR_Communicate,
192+
.locals_dict = (mp_obj_t)&espnow_com_locals_dict,
193+
.flags = MP_TYPE_FLAG_EXTENDED,
194+
MP_TYPE_EXTENDED_FIELDS(
195+
.call = &espnow_com_call,
196+
),
197+
};

ports/espressif/bindings/espnow/ESPNowStats.h renamed to ports/espressif/bindings/espnow/Communicate.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ typedef struct {
3232
mp_obj_base_t base;
3333
volatile size_t success;
3434
volatile size_t failure;
35-
} espnow_stats_obj_t;
35+
qstr job;
36+
} espnow_com_obj_t;
3637

37-
const mp_obj_type_t espnow_stats_type;
38-
extern espnow_stats_obj_t *espnow_stats_new(void);
38+
const mp_obj_type_t espnow_com_type;
39+
extern espnow_com_obj_t *espnow_com_new(qstr job);

0 commit comments

Comments
 (0)