feat!: main structure of handle changed to double pointer

This commit is contained in:
aZholtikov committed 2026-09-06 19:49:26 +03:00
1 parent 9749186fc1
commit ffe0c5e376
5 files changed
+144 -130

No files matched your search

+51 -15
View File
@@ -13,44 +13,80 @@ static const char *TAG = "zh_bh1750";
return err; \
}
static const uint8_t _bh1750_read_command = 0x20; // Measurement at 1 lx resolution.
static zh_bh1750_stats_t _stats = {0};
/**
* @brief Handle structure for BH1750 sensor.
*
* Internal structure managing the I2C device connection.
*/
struct _zh_bh1750_handle_t
{
i2c_master_dev_handle_t dev_handle; /*!< Unique I2C device handle */
};
static const uint8_t _bh1750_read_command = 0x20; /*!< One-time H-resolution measurement command (1 lx step) */
static zh_bh1750_stats_t _stats = {0}; /*!< Global error statistics accumulator */
/**
* @brief Validate the initialization configuration parameters.
*
* Checks that the I2C address is valid (0x23 or 0x5C), the frequency
* does not exceed 400000 Hz, and the I2C bus handle is not NULL.
*
* @param config Pointer to the configuration to validate
*
* @return ESP_OK if all parameters are valid
* @return ESP_ERR_INVALID_ARG if any parameter is out of range or NULL
*/
static esp_err_t _zh_bh1750_validate_config(const zh_bh1750_init_config_t *config);
/**
* @brief Register the BH1750 as an I2C device and verify connectivity.
*
* Configures the I2C device parameters, adds the device to the bus,
* and probes the sensor to confirm it is physically connected and responds.
*
* @param config Pointer to the initialization configuration
* @param handle Pointer to the allocated handle to store the device handle
*
* @return ESP_OK on successful setup and probe
* @return ESP_FAIL if device registration fails
* @return ESP_ERR_NOT_FOUND if the sensor does not respond to a probe
*/
static esp_err_t _zh_bh1750_i2c_init(const zh_bh1750_init_config_t *config, zh_bh1750_handle_t *handle);
esp_err_t zh_bh1750_init(const zh_bh1750_init_config_t *config, zh_bh1750_handle_t *handle)
esp_err_t zh_bh1750_init(const zh_bh1750_init_config_t *config, zh_bh1750_handle_t **handle) // -V2008
{
ZH_LOGI("BH1750 initialization begin.");
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "BH1750 initialization failed. Invalid argument.");
ZH_ERROR_CHECK(_zh_bh1750_validate_config(config) == ESP_OK, ESP_FAIL, NULL, "BH1750 initialization failed. Initial configuration check failed.");
ZH_ERROR_CHECK(_zh_bh1750_i2c_init(config, handle) == ESP_OK, ESP_FAIL, NULL, "BH1750 initialization failed. Failed to add I2C device.");
handle->is_initialized = true;
ZH_ERROR_CHECK(*handle == NULL, ESP_ERR_INVALID_STATE, NULL, "BH1750 initialization failed. BH1750 is already initialized.");
*handle = heap_caps_calloc(1, sizeof(zh_bh1750_handle_t), MALLOC_CAP_8BIT);
ZH_ERROR_CHECK(*handle != NULL, ESP_ERR_NO_MEM, NULL, "BH1750 initialization failed. Failed to allocate BH1750 handle.");
ZH_ERROR_CHECK(_zh_bh1750_i2c_init(config, *handle) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "BH1750 initialization failed. Failed to add I2C device.");
ZH_LOGI("BH1750 initialization completed successfully.");
return ESP_OK;
}
esp_err_t zh_bh1750_deinit(zh_bh1750_handle_t *handle)
esp_err_t zh_bh1750_deinit(zh_bh1750_handle_t **handle)
{
ZH_LOGI("BH1750 deinitialization started.");
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "BH1750 deinitialization failed. Invalid argument.");
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "BH1750 deinitialization failed. BH1750 not initialized.");
ZH_ERROR_CHECK(i2c_master_bus_rm_device(handle->dev_handle) == ESP_OK, ESP_FAIL, NULL, "BH1750 deinitialization failed. I2C remove device failed.");
handle->is_initialized = false;
ZH_ERROR_CHECK(handle != NULL && *handle != NULL, ESP_ERR_INVALID_ARG, NULL, "BH1750 deinitialization failed. Invalid argument.");
ZH_ERROR_CHECK(i2c_master_bus_rm_device((*handle)->dev_handle) == ESP_OK, ESP_FAIL, NULL, "BH1750 deinitialization failed. I2C remove device failed.");
heap_caps_free(*handle);
*handle = NULL;
ZH_LOGI("BH1750 deinitialization completed successfully.");
return ESP_OK;
}
esp_err_t zh_bh1750_read(zh_bh1750_handle_t *handle, float *data) // -V2008
esp_err_t zh_bh1750_read(zh_bh1750_handle_t **handle, float *data) // -V2008
{
ZH_LOGI("BH1750 read begin.");
ZH_ERROR_CHECK(data != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "BH1750 read fail. Invalid argument.");
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "BH1750 read fail. BH1750 not initialized.");
ZH_ERROR_CHECK(handle != NULL && *handle != NULL && data != NULL, ESP_ERR_INVALID_ARG, NULL, "BH1750 read fail. Invalid argument.");
uint8_t sensor_data[2] = {0};
ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, &_bh1750_read_command, sizeof(_bh1750_read_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, &_bh1750_read_command, sizeof(_bh1750_read_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
++_stats.i2c_driver_error, "BH1750 read fail. I2C driver error.");
vTaskDelay(180 / portTICK_PERIOD_MS);
ZH_ERROR_CHECK(i2c_master_receive(handle->dev_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
ZH_ERROR_CHECK(i2c_master_receive((*handle)->dev_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL,
++_stats.i2c_driver_error, "BH1750 read fail. I2C driver error.");
*data = (sensor_data[0] << 8 | sensor_data[1]) / 1.2;
ZH_LOGI("BH1750read completed successfully.");