This commit is contained in:
2024-07-03 10:31:03 +03:00
parent 4a216e773b
commit 618b4f4b4d
4 changed files with 31 additions and 19 deletions

View File

@ -1,7 +1,7 @@
#include "zh_bh1750.h"
#define LOW_RESOLUTION_MEASUREMENT_TIME 24 // L-Resolution mode measurement time max value.
#define HIGH_RESOLUTION_MEASUREMENT_TIME 180 // H-Resolution mode measurement time max value.
#define LOW_RESOLUTION_MEASUREMENT_TIME 24 // L-Resolution mode measurement time.
#define HIGH_RESOLUTION_MEASUREMENT_TIME 180 // H-Resolution mode measurement time.
#define SENSITIVITY_DEFAULT 69
@ -25,6 +25,8 @@ static bool _is_initialized = false;
static i2c_master_dev_handle_t _bh1750_handle = {0};
#endif
esp_err_t _adjust(const uint8_t value);
static const char *TAG = "zh_bh1750";
esp_err_t zh_bh1750_init(const zh_bh1750_init_config_t *config)
@ -114,7 +116,7 @@ REPEATE:
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
return esp_err;
}
vTaskDelay(((_sensivity / SENSITIVITY_DEFAULT) * _time) / portTICK_PERIOD_MS);
vTaskDelay(_time / portTICK_PERIOD_MS);
READ:
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_cmd_handle = i2c_cmd_link_create();
@ -134,27 +136,23 @@ READ:
return esp_err;
}
uint32_t raw_data = sensor_data[0] << 8 | sensor_data[1];
if (raw_data == 65536 || raw_data == 0)
if (raw_data == 65535 || raw_data == 0)
{
ESP_LOGW(TAG, "BH1750 read error. Sensitivity adjustment required. Current sensivity level %d", _sensivity);
ESP_LOGW(TAG, "BH1750 read error. Sensitivity adjustment required. Current sensivity level %d.", _sensivity);
}
if (_init_config.auto_adjust == true)
{
printf("RAW data %ld\n", raw_data);
printf("Sensivity %d\n", _sensivity);
if (raw_data == 65535 && _sensivity > 31)
{
if (zh_bh1750_adjust(_sensivity - 1) == ESP_OK)
if (_adjust(_sensivity - 1) == ESP_OK)
{
--_sensivity;
goto REPEATE;
}
}
if (raw_data == 1 && _sensivity < 254)
if (raw_data == 0 && _sensivity < 254)
{
if (zh_bh1750_adjust(_sensivity + 1) == ESP_OK)
if (_adjust(_sensivity + 1) == ESP_OK)
{
++_sensivity;
goto REPEATE;
}
}
@ -172,6 +170,16 @@ READ:
}
esp_err_t zh_bh1750_adjust(const uint8_t value)
{
if (_init_config.auto_adjust == true)
{
ESP_LOGE(TAG, "BH1750 adjust fail. Auto adjust is enabled.");
return ESP_ERR_NOT_FOUND;
}
return _adjust(value);
}
esp_err_t _adjust(const uint8_t value)
{
ESP_LOGI(TAG, "BH1750 adjust begin.");
if ((value < 31 || value > 254))
@ -185,11 +193,12 @@ esp_err_t zh_bh1750_adjust(const uint8_t value)
return ESP_ERR_NOT_FOUND;
}
esp_err_t esp_err = ESP_OK;
uint8_t sensitivity_data[2] = {value >> 5 | MEASUREMENT_TIME_HIGH_BIT, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT};
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
i2c_master_start(i2c_cmd_handle);
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
i2c_master_write_byte(i2c_cmd_handle, value >> 5 | MEASUREMENT_TIME_HIGH_BIT, true);
i2c_master_write_byte(i2c_cmd_handle, &sensitivity_data[0], true);
i2c_master_stop(i2c_cmd_handle);
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(i2c_cmd_handle);
@ -201,13 +210,13 @@ esp_err_t zh_bh1750_adjust(const uint8_t value)
i2c_cmd_handle = i2c_cmd_link_create();
i2c_master_start(i2c_cmd_handle);
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
i2c_master_write_byte(i2c_cmd_handle, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT, true);
i2c_master_write_byte(i2c_cmd_handle, &sensitivity_data[1], true);
i2c_master_stop(i2c_cmd_handle);
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(i2c_cmd_handle);
#else
uint8_t sensitivity_data[2] = {value >> 5 | MEASUREMENT_TIME_HIGH_BIT, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT};
esp_err = i2c_master_transmit(_bh1750_handle, sensitivity_data, sizeof(sensitivity_data), 1000 / portTICK_PERIOD_MS);
esp_err = i2c_master_transmit(_bh1750_handle, &sensitivity_data[0], 1, 1000 / portTICK_PERIOD_MS);
esp_err = i2c_master_transmit(_bh1750_handle, &sensitivity_data[1], 1, 1000 / portTICK_PERIOD_MS);
#endif
if (esp_err != ESP_OK)
{