Updated some comments

This commit is contained in:
Alexey Zholtikov 2024-06-18 17:48:20 +03:00
parent 10a5837c8f
commit a5117e1485
3 changed files with 23 additions and 79 deletions

View File

@ -19,8 +19,6 @@
1. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW. 1. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW.
2. All devices on the network must have the same WiFi channel. 2. All devices on the network must have the same WiFi channel.
## [Function description](http://zh-espnow.zh.com.ru)
## Using ## Using
In an existing project, run the following command to install the component: In an existing project, run the following command to install the component:

View File

@ -1,9 +1,3 @@
/**
* @file
* Header file for the zh_espnow component.
*
*/
#pragma once #pragma once
#include "string.h" #include "string.h"
@ -15,16 +9,6 @@
#include "esp_log.h" #include "esp_log.h"
#include "esp_heap_caps.h" #include "esp_heap_caps.h"
/**
* @brief Unique identifier of ESP-NOW interface events base. Used when registering the event handler.
*
*/
#define ESP_EVENT_BASE ZH_ESPNOW
/**
* @brief Default values for zh_espnow_init_config_t structure for initial initialization of ESP-NOW interface.
*
*/
#define ZH_ESPNOW_INIT_CONFIG_DEFAULT() \ #define ZH_ESPNOW_INIT_CONFIG_DEFAULT() \
{ \ { \
.task_priority = 4, \ .task_priority = 4, \
@ -32,76 +16,48 @@
.queue_size = 32, \ .queue_size = 32, \
.wifi_interface = WIFI_IF_STA, \ .wifi_interface = WIFI_IF_STA, \
.wifi_channel = 1, \ .wifi_channel = 1, \
.attempts = 3 \ .attempts = 3}
}
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
/** typedef struct // Structure for initial initialization of ESP-NOW interface.
* @brief Structure for initial initialization of ESP-NOW interface.
*
* @note Before initialize ESP-NOW interface recommend initialize zh_espnow_init_config_t structure with default values.
*
* @code zh_espnow_init_config_t config = ZH_ESPNOW_INIT_CONFIG_DEFAULT() @endcode
*/
typedef struct
{ {
uint8_t task_priority; ///< Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4. uint8_t task_priority; // Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4.
uint16_t stack_size; ///< Stack size for task for the ESP-NOW messages processing. @note The minimum size is 2048 bytes. uint16_t stack_size; // Stack size for task for the ESP-NOW messages processing. @note The minimum size is 2048 bytes.
uint8_t queue_size; ///< Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 16. uint8_t queue_size; // Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 16.
wifi_interface_t wifi_interface; ///< WiFi interface (STA or AP) used for ESP-NOW operation. @note The MAC address of the device depends on the selected WiFi interface. wifi_interface_t wifi_interface; // WiFi interface (STA or AP) used for ESP-NOW operation. @note The MAC address of the device depends on the selected WiFi interface.
uint8_t wifi_channel; ///< Wi-Fi channel uses to send/receive ESP-NOW data. @note Values from 1 to 14. uint8_t wifi_channel; // Wi-Fi channel uses to send/receive ESP-NOW data. @note Values from 1 to 14.
uint8_t attempts; ///< Maximum number of attempts to send a message. @note It is not recommended to set a value greater than 5. uint8_t attempts; // Maximum number of attempts to send a message. @note It is not recommended to set a value greater than 5.
} zh_espnow_init_config_t; } zh_espnow_init_config_t;
/// \cond ESP_EVENT_DECLARE_BASE(ZH_ESPNOW);
ESP_EVENT_DECLARE_BASE(ESP_EVENT_BASE);
/// \endcond
/** typedef enum // Enumeration of possible ESP-NOW events.
* @brief Enumeration of possible ESP-NOW events.
*
*/
typedef enum
{ {
ZH_ESPNOW_ON_RECV_EVENT, ///< The event when the ESP-NOW message was received. ZH_ESPNOW_ON_RECV_EVENT, // The event when the ESP-NOW message was received.
ZH_ESPNOW_ON_SEND_EVENT ///< The event when the ESP-NOW message was sent. ZH_ESPNOW_ON_SEND_EVENT // The event when the ESP-NOW message was sent.
} zh_espnow_event_type_t; } zh_espnow_event_type_t;
/** typedef enum // Enumeration of possible status of sent ESP-NOW message.
* @brief Enumeration of possible status of sent ESP-NOW message.
*
*/
typedef enum
{ {
ZH_ESPNOW_SEND_SUCCESS, ///< If ESP-NOW message was sent success. ZH_ESPNOW_SEND_SUCCESS, // If ESP-NOW message was sent success.
ZH_ESPNOW_SEND_FAIL ///< If ESP-NOW message was sent fail. ZH_ESPNOW_SEND_FAIL // If ESP-NOW message was sent fail.
} zh_espnow_on_send_event_type_t; } zh_espnow_on_send_event_type_t;
/** typedef struct // Structure for sending data to the event handler when an ESP-NOW message was sent. @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_SEND_EVENT event.
* @brief Structure for sending data to the event handler when an ESP-NOW message was sent.
*
* @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_SEND_EVENT event.
*/
typedef struct
{ {
uint8_t mac_addr[6]; ///< MAC address of the device to which the ESP-NOW message was sent. @note uint8_t mac_addr[6]; // MAC address of the device to which the ESP-NOW message was sent.
zh_espnow_on_send_event_type_t status; ///< Status of sent ESP-NOW message. @note zh_espnow_on_send_event_type_t status; // Status of sent ESP-NOW message.
} zh_espnow_event_on_send_t; } zh_espnow_event_on_send_t;
/** typedef struct // Structure for sending data to the event handler when an ESP-NOW message was received. @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_RECV_EVENT event.
* @brief Structure for sending data to the event handler when an ESP-NOW message was received.
*
* @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_RECV_EVENT event.
*/
typedef struct
{ {
uint8_t mac_addr[6]; ///< MAC address of the sender ESP-NOW message. @note uint8_t mac_addr[6]; // MAC address of the sender ESP-NOW message.
uint8_t *data; ///< Pointer to the data of the received ESP-NOW message. @note uint8_t *data; // Pointer to the data of the received ESP-NOW message.
uint8_t data_len; ///< Size of the received ESP-NOW message. @note uint8_t data_len; // Size of the received ESP-NOW message.
} zh_espnow_event_on_recv_t; } zh_espnow_event_on_recv_t;
/** /**

View File

@ -1,16 +1,8 @@
/**
* @file
* The main code of the zh_espnow component.
*
*/
#include "zh_espnow.h" #include "zh_espnow.h"
/// \cond
#define DATA_SEND_SUCCESS BIT0 #define DATA_SEND_SUCCESS BIT0
#define DATA_SEND_FAIL BIT1 #define DATA_SEND_FAIL BIT1
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
/// \endcond
static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status); static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status);
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4 #if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
@ -29,7 +21,6 @@ static zh_espnow_init_config_t _init_config = {0};
static bool _is_initialized = false; static bool _is_initialized = false;
static uint8_t _attempts = 0; static uint8_t _attempts = 0;
/// \cond
typedef struct typedef struct
{ {
enum enum
@ -46,7 +37,6 @@ typedef struct
} _queue_t; } _queue_t;
ESP_EVENT_DEFINE_BASE(ZH_ESPNOW); ESP_EVENT_DEFINE_BASE(ZH_ESPNOW);
/// \endcond
esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config) esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config)
{ {
@ -59,7 +49,7 @@ esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config)
_init_config = *config; _init_config = *config;
if (_init_config.wifi_channel < 1 || _init_config.wifi_channel > 14) if (_init_config.wifi_channel < 1 || _init_config.wifi_channel > 14)
{ {
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel."); ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel incorrect.");
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
esp_err_t err = esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE); esp_err_t err = esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE);