4 Commits

4 changed files with 100 additions and 6 deletions

View File

@ -86,8 +86,11 @@ void app_main(void)
strcpy(send_message.char_value, "THIS IS A CHAR");
send_message.float_value = 1.234;
send_message.bool_value = false;
printf("Used ESP-NOW version %d.\n", zh_espnow_get_version());
printf("ESP-NOW version %d.\n", zh_espnow_get_version());
printf("ESP-NOW channel %d. \n", zh_espnow_get_channel());
uint8_t node_mac[6] = {0};
zh_espnow_get_mac(node_mac);
printf("ESP-NOW MAC %02X:%02X:%02X:%02X:%02X:%02X.\n", MAC2STR(node_mac));
uint8_t counter = 0;
for (;;)
{

View File

@ -32,7 +32,7 @@ extern "C"
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 attempts; // Maximum number of attempts to send a message. @note It is not recommended to set a value greater than 10.
bool battery_mode; // Battery operation mode. If true, the node does not receive messages.
bool battery_mode; // Battery operation mode. If true the node does not receive messages.
} zh_espnow_init_config_t;
ESP_EVENT_DECLARE_BASE(ZH_ESPNOW);
@ -160,6 +160,45 @@ extern "C"
*/
esp_err_t zh_espnow_set_channel(uint8_t channel);
/**
* @brief Get battery mode.
*
* @return True if battery mode set false otherwise.
*/
bool zh_espnow_get_battery_mode(void);
/**
* @brief Set battery mode.
*
* @param[in] battery_mode True to enable the mode false to disable it.
*
* @note If true the node does not receive messages.
*
* @return ESP_OK if success or an error code otherwise.
*/
esp_err_t zh_espnow_set_battery_mode(bool battery_mode);
/**
* @brief Get MAC address of the node.
*
* @param[out] mac_addr Pointer to a buffer containing an eight-byte MAC.
*
* @return ESP_OK if success or an error code otherwise.
*/
esp_err_t zh_espnow_get_mac(uint8_t *mac_addr);
/**
* @brief Get the number of available places in the queue.
*
* @return Number of available places.
*/
uint8_t zh_espnow_get_queue_space(void);
/**
* @brief Clean up the message queue.
*/
void zh_espnow_clear_queue(void);
#ifdef __cplusplus
}
#endif

View File

@ -1 +1 @@
1.5.0
1.8.0

View File

@ -272,7 +272,7 @@ static esp_err_t _zh_espnow_init_resources(const zh_espnow_init_config_t *config
_event_group_handle = xEventGroupCreate();
ZH_ESPNOW_CHECK(_event_group_handle != NULL, ESP_FAIL, "Event group creation failed.");
_queue_handle = xQueueCreate(config->queue_size, sizeof(_queue_t));
ZH_ESPNOW_CHECK(_queue_handle != 0, ESP_FAIL, "Queue creation failed.");
ZH_ESPNOW_CHECK(_queue_handle != NULL, ESP_FAIL, "Queue creation failed.");
return ESP_OK;
}
@ -530,8 +530,8 @@ uint8_t zh_espnow_get_attempts(void)
esp_err_t zh_espnow_set_attempts(uint8_t attempts)
{
ZH_ESPNOW_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, "ESP-NOW is not initialized.");
ZH_ESPNOW_CHECK(attempts > 0, ESP_ERR_INVALID_ARG, "Invalid number of attempts.");
ZH_ESPNOW_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, "Number of attempts set failed. ESP-NOW is not initialized.");
ZH_ESPNOW_CHECK(attempts > 0, ESP_ERR_INVALID_ARG, "Number of attempts set failed. Invalid number.");
_init_config.attempts = attempts;
ZH_ESPNOW_LOGI("Number of attempts set successfully.");
return ESP_OK;
@ -570,4 +570,56 @@ esp_err_t zh_espnow_set_channel(uint8_t channel)
_init_config.wifi_channel = channel;
ZH_ESPNOW_LOGI("ESP-NOW channel set successfully.");
return err;
}
bool zh_espnow_get_battery_mode(void)
{
return _init_config.battery_mode;
}
esp_err_t zh_espnow_set_battery_mode(bool battery_mode)
{
ZH_ESPNOW_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, "Battery mode set failed. ESP-NOW is not initialized.");
esp_err_t err = esp_now_unregister_send_cb();
ZH_ESPNOW_CHECK(err == ESP_OK, err, "Battery mode set failed. Failed to unregister send callback.");
if (_init_config.battery_mode == false)
{
err = esp_now_unregister_recv_cb();
ZH_ESPNOW_CHECK(err == ESP_OK, err, "Battery mode set failed. Failed to unregister receive callback.");
}
err = _zh_espnow_register_callbacks(battery_mode);
ZH_ESPNOW_CHECK(err == ESP_OK, err, "Battery mode set failed. Failed to register callbacks.");
_init_config.battery_mode = battery_mode;
ZH_ESPNOW_LOGI("Battery mode set successfully.");
return ESP_OK;
}
esp_err_t zh_espnow_get_mac(uint8_t *mac_addr)
{
return esp_wifi_get_mac(_init_config.wifi_interface, mac_addr);
}
uint8_t zh_espnow_get_queue_space(void)
{
if (_queue_handle == NULL)
{
return 0;
}
return uxQueueSpacesAvailable(_queue_handle);
}
void zh_espnow_clear_queue(void)
{
if (_queue_handle != NULL)
{
_queue_t queue = {0};
while (xQueueReceive(_queue_handle, &queue, 0) == pdTRUE)
{
if (queue.data.payload != NULL)
{
heap_caps_free(queue.data.payload);
}
}
ZH_ESPNOW_LOGI("Queue clear completed successfully.");
}
}