47
47
#include "py/ringbuf.h"
48
48
49
49
#include "mpconfigport.h"
50
+
51
+ #if MICROPY_PY_ESPNOW
52
+
50
53
#include "mphalport.h"
51
54
#include "modnetwork.h"
52
55
#include "modespnow.h"
53
56
54
- #ifndef MICROPY_ESPNOW_RSSI
57
+ #ifndef MICROPY_PY_ESPNOW_RSSI
55
58
// Include code to track rssi of peers
56
- #define MICROPY_ESPNOW_RSSI 1
59
+ #define MICROPY_PY_ESPNOW_RSSI 1
57
60
#endif
58
- #ifndef MICROPY_ESPNOW_EXTRA_PEER_METHODS
61
+ #ifndef MICROPY_PY_ESPNOW_EXTRA_PEER_METHODS
59
62
// Include mod_peer(),get_peer(),peer_count()
60
- #define MICROPY_ESPNOW_EXTRA_PEER_METHODS 1
63
+ #define MICROPY_PY_ESPNOW_EXTRA_PEER_METHODS 1
61
64
#endif
62
65
63
66
// Relies on gcc Variadic Macros and Statement Expressions
@@ -71,10 +74,10 @@ static const uint8_t ESPNOW_MAGIC = 0x99;
71
74
typedef struct {
72
75
uint8_t magic ; // = ESPNOW_MAGIC
73
76
uint8_t msg_len ; // Length of the message
74
- #if MICROPY_ESPNOW_RSSI
77
+ #if MICROPY_PY_ESPNOW_RSSI
75
78
uint32_t time_ms ; // Timestamp (ms) when packet is received
76
79
int8_t rssi ; // RSSI value (dBm) (-127 to 0)
77
- #endif // MICROPY_ESPNOW_RSSI
80
+ #endif // MICROPY_PY_ESPNOW_RSSI
78
81
} __attribute__((packed )) espnow_hdr_t ;
79
82
80
83
typedef struct {
@@ -117,9 +120,9 @@ typedef struct _esp_espnow_obj_t {
117
120
size_t peer_count ; // Cache the # of peers for send(sync=True)
118
121
mp_obj_t recv_cb ; // Callback when a packet is received
119
122
mp_obj_t recv_cb_arg ; // Argument passed to callback
120
- #if MICROPY_ESPNOW_RSSI
123
+ #if MICROPY_PY_ESPNOW_RSSI
121
124
mp_obj_t peers_table ; // A dictionary of discovered peers
122
- #endif // MICROPY_ESPNOW_RSSI
125
+ #endif // MICROPY_PY_ESPNOW_RSSI
123
126
} esp_espnow_obj_t ;
124
127
125
128
const mp_obj_type_t esp_espnow_type ;
@@ -164,11 +167,11 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args,
164
167
self -> recv_timeout_ms = DEFAULT_RECV_TIMEOUT_MS ;
165
168
self -> recv_buffer = NULL ; // Buffer is allocated in espnow_init()
166
169
self -> recv_cb = mp_const_none ;
167
- #if MICROPY_ESPNOW_RSSI
170
+ #if MICROPY_PY_ESPNOW_RSSI
168
171
self -> peers_table = mp_obj_new_dict (0 );
169
172
// Prevent user code modifying the dict
170
173
mp_obj_dict_get_map (self -> peers_table )-> is_fixed = 1 ;
171
- #endif // MICROPY_ESPNOW_RSSI
174
+ #endif // MICROPY_PY_ESPNOW_RSSI
172
175
173
176
// Set the global singleton pointer for the espnow protocol.
174
177
MP_STATE_PORT (espnow_singleton ) = self ;
@@ -305,7 +308,7 @@ STATIC mp_obj_t espnow_stats(mp_obj_t _) {
305
308
}
306
309
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (espnow_stats_obj , espnow_stats );
307
310
308
- #if MICROPY_ESPNOW_RSSI
311
+ #if MICROPY_PY_ESPNOW_RSSI
309
312
// ### Maintaining the peer table and reading RSSI values
310
313
//
311
314
// We maintain a peers table for several reasons, to:
@@ -345,7 +348,7 @@ static mp_map_elem_t *_update_rssi(const uint8_t *peer, int8_t rssi, uint32_t ti
345
348
list -> items [1 ] = mp_obj_new_int (time_ms );
346
349
return item ;
347
350
}
348
- #endif // MICROPY_ESPNOW_RSSI
351
+ #endif // MICROPY_PY_ESPNOW_RSSI
349
352
350
353
// Return C pointer to byte memory string/bytes/bytearray in obj.
351
354
// Raise ValueError if the length does not match expected len.
@@ -413,11 +416,11 @@ STATIC mp_obj_t espnow_recvinto(size_t n_args, const mp_obj_t *args) {
413
416
msg -> len += msg -> free ; // Make all the space in msg array available
414
417
msg -> free = 0 ;
415
418
}
416
- #if MICROPY_ESPNOW_RSSI
419
+ #if MICROPY_PY_ESPNOW_RSSI
417
420
uint8_t peer_buf [ESP_NOW_ETH_ALEN ];
418
421
#else
419
422
uint8_t * peer_buf = _get_bytes_len_w (list -> items [0 ], ESP_NOW_ETH_ALEN );
420
- #endif // MICROPY_ESPNOW_RSSI
423
+ #endif // MICROPY_PY_ESPNOW_RSSI
421
424
uint8_t * msg_buf = _get_bytes_len_w (msg , ESP_NOW_MAX_DATA_LEN );
422
425
423
426
// Read the packet header from the incoming buffer
@@ -441,15 +444,15 @@ STATIC mp_obj_t espnow_recvinto(size_t n_args, const mp_obj_t *args) {
441
444
msg -> free = size - msg_len ;
442
445
}
443
446
444
- #if MICROPY_ESPNOW_RSSI
447
+ #if MICROPY_PY_ESPNOW_RSSI
445
448
// Update rssi value in the peer device table
446
449
mp_map_elem_t * entry = _update_rssi (peer_buf , hdr .rssi , hdr .time_ms );
447
450
list -> items [0 ] = entry -> key ; // Set first element of list to peer
448
451
if (list -> len >= 4 ) {
449
452
list -> items [2 ] = MP_OBJ_NEW_SMALL_INT (hdr .rssi );
450
453
list -> items [3 ] = mp_obj_new_int (hdr .time_ms );
451
454
}
452
- #endif // MICROPY_ESPNOW_RSSI
455
+ #endif // MICROPY_PY_ESPNOW_RSSI
453
456
454
457
return MP_OBJ_NEW_SMALL_INT (msg_len );
455
458
}
@@ -561,10 +564,10 @@ STATIC void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, in
561
564
espnow_hdr_t header ;
562
565
header .magic = ESPNOW_MAGIC ;
563
566
header .msg_len = msg_len ;
564
- #if MICROPY_ESPNOW_RSSI
567
+ #if MICROPY_PY_ESPNOW_RSSI
565
568
header .rssi = recv_info -> rx_ctrl -> rssi ;
566
569
header .time_ms = mp_hal_ticks_ms ();
567
- #endif // MICROPY_ESPNOW_RSSI
570
+ #endif // MICROPY_PY_ESPNOW_RSSI
568
571
569
572
ringbuf_put_bytes (buf , (uint8_t * )& header , sizeof (header ));
570
573
ringbuf_put_bytes (buf , recv_info -> src_addr , ESP_NOW_ETH_ALEN );
@@ -710,7 +713,7 @@ STATIC mp_obj_t espnow_get_peers(mp_obj_t _) {
710
713
}
711
714
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (espnow_get_peers_obj , espnow_get_peers );
712
715
713
- #if MICROPY_ESPNOW_EXTRA_PEER_METHODS
716
+ #if MICROPY_PY_ESPNOW_EXTRA_PEER_METHODS
714
717
// ESPNow.get_peer(peer_mac): Get the peer info for peer_mac as a tuple.
715
718
// Raise OSError if ESPNow.init() has not been called.
716
719
// Raise ValueError if mac or LMK are not bytes-like objects or wrong length.
@@ -777,11 +780,11 @@ STATIC const mp_rom_map_elem_t esp_espnow_locals_dict_table[] = {
777
780
{ MP_ROM_QSTR (MP_QSTR_add_peer ), MP_ROM_PTR (& espnow_add_peer_obj ) },
778
781
{ MP_ROM_QSTR (MP_QSTR_del_peer ), MP_ROM_PTR (& espnow_del_peer_obj ) },
779
782
{ MP_ROM_QSTR (MP_QSTR_get_peers ), MP_ROM_PTR (& espnow_get_peers_obj ) },
780
- #if MICROPY_ESPNOW_EXTRA_PEER_METHODS
783
+ #if MICROPY_PY_ESPNOW_EXTRA_PEER_METHODS
781
784
{ MP_ROM_QSTR (MP_QSTR_mod_peer ), MP_ROM_PTR (& espnow_mod_peer_obj ) },
782
785
{ MP_ROM_QSTR (MP_QSTR_get_peer ), MP_ROM_PTR (& espnow_get_peer_obj ) },
783
786
{ MP_ROM_QSTR (MP_QSTR_peer_count ), MP_ROM_PTR (& espnow_peer_count_obj ) },
784
- #endif // MICROPY_ESPNOW_EXTRA_PEER_METHODS
787
+ #endif // MICROPY_PY_ESPNOW_EXTRA_PEER_METHODS
785
788
};
786
789
STATIC MP_DEFINE_CONST_DICT (esp_espnow_locals_dict , esp_espnow_locals_dict_table );
787
790
@@ -819,7 +822,7 @@ STATIC const mp_stream_p_t espnow_stream_p = {
819
822
.ioctl = espnow_stream_ioctl ,
820
823
};
821
824
822
- #if MICROPY_ESPNOW_RSSI
825
+ #if MICROPY_PY_ESPNOW_RSSI
823
826
// Return reference to the dictionary of peers we have seen:
824
827
// {peer1: (rssi, time_sec), peer2: (rssi, time_msec), ...}
825
828
// where:
@@ -838,16 +841,16 @@ STATIC void espnow_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
838
841
}
839
842
dest [1 ] = MP_OBJ_SENTINEL ; // Attribute not found
840
843
}
841
- #endif // MICROPY_ESPNOW_RSSI
844
+ #endif // MICROPY_PY_ESPNOW_RSSI
842
845
843
846
MP_DEFINE_CONST_OBJ_TYPE (
844
847
esp_espnow_type ,
845
848
MP_QSTR_ESPNowBase ,
846
849
MP_TYPE_FLAG_NONE ,
847
850
make_new , espnow_make_new ,
848
- #if MICROPY_ESPNOW_RSSI
851
+ #if MICROPY_PY_ESPNOW_RSSI
849
852
attr , espnow_attr ,
850
- #endif // MICROPY_ESPNOW_RSSI
853
+ #endif // MICROPY_PY_ESPNOW_RSSI
851
854
protocol , & espnow_stream_p ,
852
855
locals_dict , & esp_espnow_locals_dict
853
856
);
@@ -859,3 +862,5 @@ const mp_obj_module_t mp_module_espnow = {
859
862
860
863
MP_REGISTER_MODULE (MP_QSTR__espnow , mp_module_espnow );
861
864
MP_REGISTER_ROOT_POINTER (struct _esp_espnow_obj_t * espnow_singleton );
865
+
866
+ #endif // MICROPY_PY_ESPNOW
0 commit comments