diff --git a/include/zh_espnow.h b/include/zh_espnow.h index 58699de..32c1aba 100644 --- a/include/zh_espnow.h +++ b/include/zh_espnow.h @@ -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,24 @@ 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); + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/version.txt b/version.txt index 3e1ad72..ce6a70b 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.5.0 \ No newline at end of file +1.6.0 \ No newline at end of file diff --git a/zh_espnow.c b/zh_espnow.c index e8b1b7b..d17030a 100755 --- a/zh_espnow.c +++ b/zh_espnow.c @@ -570,4 +570,26 @@ 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; } \ No newline at end of file