@@ -79,6 +79,7 @@ static const size_t BUSY_WAIT_MS = 25; // milliseconds
79
79
typedef struct _esp_espnow_obj_t {
80
80
mp_obj_base_t base ;
81
81
82
+ int initialised ;
82
83
buffer_t recv_buffer ; // A buffer for received packets
83
84
size_t recv_buffer_size ;
84
85
size_t sent_packets ; // Count of sent packets
@@ -124,18 +125,13 @@ static int _buf_get_data_from_packet(const uint8_t *buf, size_t size,
124
125
const uint8_t * * peer_addr ,
125
126
const uint8_t * * msg , size_t * msg_len );
126
127
// These functions help cleanup boiler plate code for processing args.
127
- static const uint8_t * _get_str (mp_obj_t obj );
128
- static size_t _get_len (mp_obj_t obj );
129
128
static bool _get_bool (mp_obj_t obj );
130
- static const uint8_t * _get_bytes (mp_obj_t obj );
131
129
static const uint8_t * _get_bytes_len (mp_obj_t obj , size_t len );
132
130
static const uint8_t * _get_peer (mp_obj_t obj );
133
131
134
132
// ### Initialisation and Config functions
135
133
//
136
134
137
- static int initialized = 0 ;
138
-
139
135
// Allocate and initialise the ESPNow module singleton
140
136
// Returns the initialise singleton.
141
137
STATIC mp_obj_t espnow_make_new (const mp_obj_type_t * type , size_t n_args ,
@@ -145,14 +141,17 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args,
145
141
return espnow_singleton ;
146
142
}
147
143
esp_espnow_obj_t * self = m_malloc0 (sizeof (esp_espnow_obj_t ));
144
+ self -> initialised = 0 ;
148
145
self -> base .type = & esp_espnow_type ;
149
146
self -> recv_buffer_size = DEFAULT_RECV_BUFFER_SIZE ;
150
147
self -> recv_timeout = DEFAULT_RECV_TIMEOUT ;
151
148
152
149
// Allocate and initialise the "callee-owned" tuple for irecv().
153
150
uint8_t msg_tmp [ESP_NOW_MAX_DATA_LEN ], peer_tmp [ESP_NOW_ETH_ALEN ];
154
- self -> irecv_peer = MP_OBJ_TO_PTR (mp_obj_new_bytearray (ESP_NOW_ETH_ALEN , peer_tmp ));
155
- self -> irecv_msg = MP_OBJ_TO_PTR (mp_obj_new_bytearray (ESP_NOW_MAX_DATA_LEN , msg_tmp ));
151
+ self -> irecv_peer = MP_OBJ_TO_PTR (
152
+ mp_obj_new_bytearray (ESP_NOW_ETH_ALEN , peer_tmp ));
153
+ self -> irecv_msg = MP_OBJ_TO_PTR (
154
+ mp_obj_new_bytearray (ESP_NOW_MAX_DATA_LEN , msg_tmp ));
156
155
self -> irecv_tuple = mp_obj_new_tuple (2 , NULL );
157
156
self -> irecv_tuple -> items [0 ] = MP_OBJ_FROM_PTR (self -> irecv_peer );
158
157
self -> irecv_tuple -> items [1 ] = MP_OBJ_FROM_PTR (self -> irecv_msg );
@@ -175,14 +174,14 @@ recv_cb(const uint8_t *mac_addr, const uint8_t *data, int len);
175
174
// allocate the recv data buffers.
176
175
STATIC mp_obj_t espnow_init (mp_obj_t _ ) {
177
176
esp_espnow_obj_t * self = espnow_singleton ;
178
- if (initialized ) {
177
+ if (self -> initialised ) {
179
178
return mp_const_none ;
180
179
}
181
180
self -> recv_buffer = buffer_init (self -> recv_buffer_size );
182
181
self -> recv_buffer_size = buffer_size (self -> recv_buffer );
183
182
buffer_print ("Recv buffer" , self -> recv_buffer );
184
183
185
- initialized = 1 ;
184
+ self -> initialised = 1 ;
186
185
esp_initialise_wifi ();
187
186
check_esp_err (esp_now_init ());
188
187
check_esp_err (esp_now_register_recv_cb (recv_cb ));
@@ -197,14 +196,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_init_obj, espnow_init);
197
196
// deallocate the recv data buffers.
198
197
STATIC mp_obj_t espnow_deinit (mp_obj_t _ ) {
199
198
esp_espnow_obj_t * self = espnow_singleton ;
200
- if (initialized ) {
201
- check_esp_err (esp_now_unregister_recv_cb ());
202
- check_esp_err (esp_now_unregister_send_cb ());
203
- check_esp_err (esp_now_deinit ());
204
- initialized = 0 ;
205
- buffer_release (self -> recv_buffer );
206
- self -> peer_count = 0 ; // esp_now_deinit() removes all peers.
199
+ if (!self -> initialised ) {
200
+ return mp_const_none ;
207
201
}
202
+ check_esp_err (esp_now_unregister_recv_cb ());
203
+ check_esp_err (esp_now_unregister_send_cb ());
204
+ check_esp_err (esp_now_deinit ());
205
+ self -> initialised = 0 ;
206
+ buffer_release (self -> recv_buffer );
207
+ self -> peer_count = 0 ; // esp_now_deinit() removes all peers.
208
+
208
209
return mp_const_none ;
209
210
}
210
211
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (espnow_deinit_obj , espnow_deinit );
@@ -241,8 +242,8 @@ STATIC mp_obj_t espnow_config(
241
242
uintptr_t name = (uintptr_t )args [ARG_get ].u_obj ;
242
243
if (name == QS (MP_QSTR_rxbuf )) {
243
244
return mp_obj_new_int (
244
- (initialized ? buffer_size (self -> recv_buffer )
245
- : self -> recv_buffer_size ));
245
+ (self -> initialised ? buffer_size (self -> recv_buffer )
246
+ : self -> recv_buffer_size ));
246
247
} else if (name == QS (MP_QSTR_timeout )) {
247
248
return mp_obj_new_int (self -> recv_timeout );
248
249
} else {
@@ -335,7 +336,7 @@ STATIC void IRAM_ATTR recv_cb(
335
336
// Timeout is set in args or by default.
336
337
static int _wait_for_recv_packet (size_t n_args , const mp_obj_t * args ) {
337
338
esp_espnow_obj_t * self = espnow_singleton ;
338
- if (!initialized ) {
339
+ if (!self -> initialised ) {
339
340
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("not initialised" ));
340
341
}
341
342
// Setup the timeout and wait for a packet to arrive
@@ -487,17 +488,22 @@ static int _do_espnow_send(
487
488
// False if sync==True and message is not received by any recipients
488
489
// Raises: EAGAIN if the internal espnow buffers are full.
489
490
STATIC mp_obj_t espnow_send (size_t n_args , const mp_obj_t * args ) {
490
- if (!initialized ) {
491
+ esp_espnow_obj_t * self = espnow_singleton ;
492
+ if (!self -> initialised ) {
491
493
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("not initialised" ));
492
494
}
493
495
// Check the various combinations of input arguments
494
496
mp_obj_t peer = (n_args > 2 ) ? args [1 ] : mp_const_none ;
495
497
mp_obj_t msg = (n_args > 2 ) ? args [2 ] : (n_args == 2 ) ? args [1 ] : MP_OBJ_NULL ;
496
498
mp_obj_t sync = (n_args > 3 ) ? args [3 ] : mp_const_true ;
497
499
500
+ // Get a pointer to the buffer of obj
501
+ mp_buffer_info_t bufinfo ;
502
+ mp_get_buffer_raise (msg , & bufinfo , MP_BUFFER_READ );
503
+
498
504
int failed_responses =
499
505
_do_espnow_send (
500
- _get_peer (peer ), _get_str ( msg ), _get_len ( msg ) , _get_bool (sync ));
506
+ _get_peer (peer ), bufinfo . buf , bufinfo . len , _get_bool (sync ));
501
507
if (failed_responses < 0 ) {
502
508
mp_raise_OSError (MP_EAGAIN );
503
509
}
@@ -531,7 +537,7 @@ STATIC bool _update_peer_info(
531
537
// Key can be <= 16 bytes - padded with '\0'.
532
538
memcpy (peer -> lmk ,
533
539
_get_bytes_len (obj , ESP_NOW_KEY_LEN ),
534
- _get_len ( obj ) );
540
+ ESP_NOW_KEY_LEN );
535
541
}
536
542
}
537
543
if (args [ARG_channel ].u_int != -1 ) {
@@ -681,7 +687,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_peer_count_obj, espnow_peer_count);
681
687
// Read an ESPNow packet into a stream buffer
682
688
STATIC mp_uint_t espnow_stream_read (mp_obj_t self_in , void * buf_in ,
683
689
mp_uint_t size , int * errcode ) {
684
- if (!initialized ) {
690
+ esp_espnow_obj_t * self = espnow_singleton ;
691
+ if (!self -> initialised ) {
685
692
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("not initialised" ));
686
693
}
687
694
@@ -770,7 +777,8 @@ STATIC mp_uint_t espnow_stream_ioctl(mp_obj_t self_in, mp_uint_t request,
770
777
771
778
// Iterating over ESPNow returns tuples of (peer_addr, message)...
772
779
STATIC mp_obj_t espnow_iternext (mp_obj_t self_in ) {
773
- if (!initialized ) {
780
+ esp_espnow_obj_t * self = espnow_singleton ;
781
+ if (!self -> initialised ) {
774
782
return MP_OBJ_STOP_ITERATION ;
775
783
}
776
784
return espnow_irecv (1 , & self_in );
@@ -846,38 +854,20 @@ const mp_obj_module_t mp_module_esp_espnow = {
846
854
};
847
855
848
856
// These functions help cleanup boiler plate code for processing args.
849
- static const uint8_t * _get_str (mp_obj_t obj ) {
850
- size_t len ;
851
- return (const uint8_t * )mp_obj_str_get_data (obj , & len );
852
- }
853
-
854
- static size_t _get_len (mp_obj_t obj ) {
855
- size_t len ;
856
- (void )mp_obj_str_get_data (obj , & len );
857
- return len ;
858
- }
859
-
860
857
static bool _get_bool (mp_obj_t obj ) {
861
858
if (obj == mp_const_true || obj == mp_const_false ) {
862
859
return obj == mp_const_true ;
863
860
}
864
861
mp_raise_TypeError (MP_ERROR_TEXT ("Expected True or False" ));
865
862
}
866
863
867
- static const uint8_t * _get_bytes (mp_obj_t obj ) {
868
- if (!mp_obj_is_type (obj , & mp_type_bytes )) {
869
- mp_raise_TypeError (MP_ERROR_TEXT ("not bytes" ));
870
- }
871
- size_t len ;
872
<
10000
/td>
- return (const uint8_t * )mp_obj_str_get_data (obj , & len );
873
- }
874
-
875
864
static const uint8_t * _get_bytes_len (mp_obj_t obj , size_t len ) {
876
- const uint8_t * p = _get_bytes (obj );
877
- if (_get_len (obj ) != len ) {
865
+ mp_buffer_info_t bufinfo ;
866
+ mp_get_buffer_raise (obj , & bufinfo , MP_BUFFER_READ );
867
+ if (bufinfo .len != len ) {
878
868
mp_raise_ValueError (MP_ERROR_TEXT ("wrong length" ));
879
869
}
880
- return p ;
870
+ return ( const uint8_t * ) bufinfo . buf ;
881
871
}
882
872
883
873
// Check obj is a byte string and return ptr to mac address.
0 commit comments