@@ -234,9 +234,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_active_obj, 1, 2, espnow_activ
234
234
// Get or set configuration values. Supported config params:
235
235
// buffer: size of buffer for rx packets (default=514 bytes)
236
236
// timeout: Default read timeout (default=300,000 milliseconds)
237
- STATIC mp_obj_t espnow_config (
238
- size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
239
-
237
+ STATIC mp_obj_t espnow_config (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
240
238
esp_espnow_obj_t * self = _get_singleton ();
241
239
enum { ARG_get , ARG_buffer , ARG_timeout_ms , ARG_rate };
242
240
static const mp_arg_t allowed_args [] = {
@@ -258,10 +256,8 @@ STATIC mp_obj_t espnow_config(
258
256
if (args [ARG_rate ].u_int >= 0 ) {
259
257
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (4 , 3 , 0 )
260
258
esp_initialise_wifi (); // Call the wifi init code in network_wlan.c
261
- check_esp_err (esp_wifi_config_espnow_rate (
262
- ESP_IF_WIFI_STA , args [ARG_rate ].u_int ));
263
- check_esp_err (esp_wifi_config_espnow_rate (
264
- ESP_IF_WIFI_AP , args [ARG_rate ].u_int ));
259
+ check_esp_err (esp_wifi_config_espnow_rate (ESP_IF_WIFI_STA , args [ARG_rate ].u_int ));
260
+ check_esp_err (esp_wifi_config_espnow_rate (ESP_IF_WIFI_AP , args [ARG_rate ].u_int ));
265
261
#else
266
262
mp_raise_ValueError (MP_ERROR_TEXT ("rate option not supported" ));
267
263
#endif
@@ -329,8 +325,9 @@ static inline int8_t _get_rssi_from_wifi_pkt(const uint8_t *msg) {
329
325
// and a espnow_frame_format_t.
330
326
// Backtrack to get a pointer to the wifi_promiscuous_pkt_t.
331
327
static const size_t sizeof_espnow_frame_format = 39 ;
332
- wifi_promiscuous_pkt_t * wifi_pkt = (wifi_promiscuous_pkt_t * )(
333
- msg - sizeof_espnow_frame_format - sizeof (wifi_promiscuous_pkt_t ));
328
+ wifi_promiscuous_pkt_t * wifi_pkt =
329
+ (wifi_promiscuous_pkt_t * )(msg - sizeof_espnow_frame_format -
330
+ sizeof (wifi_promiscuous_pkt_t ));
334
331
335
332
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL (4 , 2 , 0 )
336
333
return wifi_pkt -> rx_ctrl .rssi - 100 ; // Offset rssi for IDF 4.0.2
@@ -342,9 +339,7 @@ static inline int8_t _get_rssi_from_wifi_pkt(const uint8_t *msg) {
342
339
// Lookup a peer in the peers table and return a reference to the item in the
343
340
// peers_table. Add peer to the table if it is not found (may alloc memory).
344
341
// Will not return NULL.
345
- static mp_map_elem_t * _lookup_add_peer (
346
- esp_espnow_obj_t * self , const uint8_t * peer ) {
347
-
342
+ static mp_map_elem_t * _lookup_add_peer (esp_espnow_obj_t * self , const uint8_t * peer ) {
348
343
// We do not want to allocate any new memory in the case that the peer
349
344
// already exists in the peers_table (which is almost all the time).
350
345
// So, we use a byte string on the stack and look that up in the dict.
@@ -364,9 +359,7 @@ static mp_map_elem_t *_lookup_add_peer(
364
359
365
360
// Update the peers table with the new rssi value from a received pkt and
366
361
// return a reference to the item in the peers_table.
367
- static mp_map_elem_t * _update_rssi (
368
- const uint8_t * peer , int8_t rssi , uint32_t time_ms ) {
369
-
362
+ static mp_map_elem_t * _update_rssi (const uint8_t * peer , int8_t rssi , uint32_t time_ms ) {
370
363
esp_espnow_obj_t * self = _get_singleton_initialised ();
371
364
// Lookup the peer in the device table
372
365
mp_map_elem_t * item = _lookup_add_peer (self , peer );
@@ -389,8 +382,7 @@ static uint8_t *_get_bytes_len_rw(mp_obj_t obj, size_t len, mp_uint_t rw) {
389
382
mp_buffer_info_t bufinfo ;
390
383
mp_get_buffer_raise (obj , & bufinfo , rw );
391
384
if (bufinfo .len != len ) {
392
- mp_raise_ValueError (
393
- MP_ERROR_TEXT ("ESPNow: bytes or bytearray wrong length" ));
385
+ mp_raise_ValueError (MP_ERROR_TEXT ("ESPNow: bytes or bytearray wrong length" ));
394
386
}
395
387
return (uint8_t * )bufinfo .buf ;
396
388
}
@@ -465,10 +457,10 @@ STATIC mp_obj_t espnow_recvinto(size_t n_args, const mp_obj_t *args) {
465
457
int msg_len = hdr .msg_len ;
466
458
467
459
// Check the message packet header format and read the message data
468
- if (hdr .magic != ESPNOW_MAGIC ||
469
- msg_len > ESP_NOW_MAX_DATA_LEN ||
470
- ringbuf_get_bytes (self -> recv_buffer , peer_buf , ESP_NOW_ETH_ALEN ) < 0 ||
471
- ringbuf_get_bytes (self -> recv_buffer , msg_buf , msg_len ) < 0 ) {
460
+ if (hdr .magic != ESPNOW_MAGIC
461
+ || msg_len > ESP_NOW_MAX_DATA_LEN
462
+ || ringbuf_get_bytes (self -> recv_buffer , peer_buf , ESP_NOW_ETH_ALEN ) < 0
463
+ || ringbuf_get_bytes (self -> recv_buffer , msg_buf , msg_len ) < 0 ) {
472
464
mp_raise_ValueError (MP_ERROR_TEXT ("ESPNow.recv(): buffer error" ));
473
465
}
474
466
if (mp_obj_is_type (msg , & mp_type_bytearray )) {
@@ -550,10 +542,9 @@ STATIC mp_obj_t espnow_send(size_t n_args, const mp_obj_t *args) {
550
542
int saved_failures = self -> tx_failures ;
551
543
// Send the packet - try, try again if internal esp-now buffers are full.
552
544
esp_err_t err ;
553
- int64_t start = mp_hal_ticks_ms ();
554
- while ((ESP_ERR_ESPNOW_NO_MEM ==
555
- (err = esp_now_send (peer , message .buf , message .len ))) &&
556
- (mp_hal_ticks_ms () - start ) <= DEFAULT_SEND_TIMEOUT_MS ) {
545
+ mp_uint_t start = mp_hal_ticks_ms ();
546
+ while ((ESP_ERR_ESPNOW_NO_MEM == (err = esp_now_send (peer , message .buf , message .len )))
547
+ && (mp_uint_t )(mp_hal_ticks_ms () - start ) < (mp_uint_t )DEFAULT_SEND_TIMEOUT_MS ) {
557
548
MICROPY_EVENT_POLL_HOOK ;
558
549
}
559
550
check_esp_err (err ); // Will raise OSError if e != ESP_OK
@@ -641,16 +632,13 @@ STATIC bool _update_peer_info(
641
632
{ MP_QSTR_encrypt , MP_ARG_OBJ , {.u_obj = mp_const_none } },
642
633
};
643
634
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
644
- mp_arg_parse_all (n_args , pos_args , kw_args ,
645
- MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
635
+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
646
636
if (args [ARG_lmk ].u_obj != mp_const_none ) {
647
637
mp_obj_t obj = args [ARG_lmk ].u_obj ;
648
638
peer -> encrypt = mp_obj_is_true (obj );
649
639
if (peer -> encrypt ) {
650
640
// Key must be 16 bytes in length.
651
- memcpy (peer -> lmk ,
652
- _get_bytes_len (obj , ESP_NOW_KEY_LEN ),
653
- ESP_NOW_KEY_LEN );
641
+ memcpy (peer -> lmk , _get_bytes_len (obj , ESP_NOW_KEY_LEN ), ESP_NOW_KEY_LEN );
654
642
}
655
643
}
656
644
if (args [ARG_channel ].u_obj != mp_const_none ) {
@@ -692,9 +680,7 @@ STATIC void _update_peer_count() {
692
680
// Raise ValueError if mac or LMK are not bytes-like objects or wrong length.
693
681
// Raise TypeError if invalid keyword args or too many positional args.
694
682
// Return None.
695
- STATIC mp_obj_t espnow_add_peer (
696
- size_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
697
-
683
+ STATIC mp_obj_t espnow_add_peer (size_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
698
684
esp_now_peer_info_t peer = {0 };
699
685
memcpy (peer .peer_addr , _get_peer (args [1 ]), ESP_NOW_ETH_ALEN );
700
686
_update_peer_info (& peer , n_args - 2 , args + 2 , kw_args );
@@ -776,9 +762,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(espnow_get_peer_obj, espnow_get_peer);
776
762
// Raise ValueError if mac or LMK are not bytes-like objects or wrong length.
777
763
// Raise TypeError if invalid keyword args or too many positional args.
778
764
// Return None.
779
- STATIC mp_obj_t espnow_mod_peer (
780
- size_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
781
-
765
+ STATIC mp_obj_t espnow_mod_peer (size_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
782
766
esp_now_peer_info_t peer = {0 };
783
767
memcpy (peer .peer_addr , _get_peer (args [1 ]), ESP_NOW_ETH_ALEN );
784
768
check_esp_err (esp_now_get_peer (peer .peer_addr , & peer ));
@@ -845,8 +829,8 @@ STATIC MP_DEFINE_CONST_DICT(espnow_globals_dict, espnow_globals_dict_table);
845
829
// ...so asyncio can poll.ipoll() on this device
846
830
847
831
// Support ioctl(MP_STREAM_POLL, ) for asyncio
848
- STATIC mp_uint_t espnow_stream_ioctl (mp_obj_t self_in , mp_uint_t request ,
849
- uintptr_t arg , int * errcode ) {
832
+ STATIC mp_uint_t espnow_stream_ioctl (
833
+ mp_obj_t self_in , mp_uint_t request , uintptr_t arg , int * errcode ) {
850
834
if (request != MP_STREAM_POLL ) {
851
835
* errcode = MP_EINVAL ;
852
836
return MP_STREAM_ERROR ;
0 commit comments