WIP
This commit is contained in:
@ -60,6 +60,7 @@ void app_main(void)
|
|||||||
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
|
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
|
||||||
#endif
|
#endif
|
||||||
zh_bh1750_init_config_t bh1750_init_config = ZH_BH1750_INIT_CONFIG_DEFAULT();
|
zh_bh1750_init_config_t bh1750_init_config = ZH_BH1750_INIT_CONFIG_DEFAULT();
|
||||||
|
bh1750_init_config.auto_adjust = true; // Just for an example of how to enable auto adjustment the sensor sensitivity.
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
bh1750_init_config.i2c_port = I2C_PORT;
|
bh1750_init_config.i2c_port = I2C_PORT;
|
||||||
#else
|
#else
|
||||||
@ -72,7 +73,7 @@ void app_main(void)
|
|||||||
{
|
{
|
||||||
zh_bh1750_read(&lux);
|
zh_bh1750_read(&lux);
|
||||||
printf("Lux %0.2f\n", lux);
|
printf("Lux %0.2f\n", lux);
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -86,6 +86,8 @@ extern "C"
|
|||||||
/**
|
/**
|
||||||
* @brief Adjust BH1750 sensor sensitivity.
|
* @brief Adjust BH1750 sensor sensitivity.
|
||||||
*
|
*
|
||||||
|
* @attention Can only be used if the automatic adjustment is not switched on.
|
||||||
|
*
|
||||||
* @param[in] value Value of sensitivity.
|
* @param[in] value Value of sensitivity.
|
||||||
*
|
*
|
||||||
* @note Range value from 31 to 254. 69 by default. Can be changed continuously during program work.
|
* @note Range value from 31 to 254. 69 by default. Can be changed continuously during program work.
|
||||||
@ -93,7 +95,7 @@ extern "C"
|
|||||||
* @return
|
* @return
|
||||||
* - ESP_OK if adjust was success
|
* - ESP_OK if adjust was success
|
||||||
* - ESP_ERR_INVALID_ARG if parameter error
|
* - ESP_ERR_INVALID_ARG if parameter error
|
||||||
* - ESP_ERR_NOT_FOUND if BH1750 is not initialized
|
* - ESP_ERR_NOT_FOUND if BH1750 is not initialized or auto adjust is enabled
|
||||||
* - ESP_FAIL if sending command error or slave has not ACK the transfer
|
* - ESP_FAIL if sending command error or slave has not ACK the transfer
|
||||||
* - ESP_ERR_INVALID_STATE if I2C driver not installed or not in master mode
|
* - ESP_ERR_INVALID_STATE if I2C driver not installed or not in master mode
|
||||||
* - ESP_ERR_TIMEOUT if operation timeout because the bus is busy
|
* - ESP_ERR_TIMEOUT if operation timeout because the bus is busy
|
||||||
|
@ -1 +1 @@
|
|||||||
1.0.1
|
1.0.2
|
41
zh_bh1750.c
41
zh_bh1750.c
@ -1,7 +1,7 @@
|
|||||||
#include "zh_bh1750.h"
|
#include "zh_bh1750.h"
|
||||||
|
|
||||||
#define LOW_RESOLUTION_MEASUREMENT_TIME 24 // L-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 max value.
|
#define HIGH_RESOLUTION_MEASUREMENT_TIME 180 // H-Resolution mode measurement time.
|
||||||
|
|
||||||
#define SENSITIVITY_DEFAULT 69
|
#define SENSITIVITY_DEFAULT 69
|
||||||
|
|
||||||
@ -25,6 +25,8 @@ static bool _is_initialized = false;
|
|||||||
static i2c_master_dev_handle_t _bh1750_handle = {0};
|
static i2c_master_dev_handle_t _bh1750_handle = {0};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
esp_err_t _adjust(const uint8_t value);
|
||||||
|
|
||||||
static const char *TAG = "zh_bh1750";
|
static const char *TAG = "zh_bh1750";
|
||||||
|
|
||||||
esp_err_t zh_bh1750_init(const zh_bh1750_init_config_t *config)
|
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.");
|
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
||||||
return esp_err;
|
return esp_err;
|
||||||
}
|
}
|
||||||
vTaskDelay(((_sensivity / SENSITIVITY_DEFAULT) * _time) / portTICK_PERIOD_MS);
|
vTaskDelay(_time / portTICK_PERIOD_MS);
|
||||||
READ:
|
READ:
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
i2c_cmd_handle = i2c_cmd_link_create();
|
i2c_cmd_handle = i2c_cmd_link_create();
|
||||||
@ -134,27 +136,23 @@ READ:
|
|||||||
return esp_err;
|
return esp_err;
|
||||||
}
|
}
|
||||||
uint32_t raw_data = sensor_data[0] << 8 | sensor_data[1];
|
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)
|
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 (raw_data == 65535 && _sensivity > 31)
|
||||||
{
|
{
|
||||||
if (zh_bh1750_adjust(_sensivity - 1) == ESP_OK)
|
if (_adjust(_sensivity - 1) == ESP_OK)
|
||||||
{
|
{
|
||||||
--_sensivity;
|
|
||||||
goto REPEATE;
|
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;
|
goto REPEATE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -172,6 +170,16 @@ READ:
|
|||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_bh1750_adjust(const uint8_t value)
|
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.");
|
ESP_LOGI(TAG, "BH1750 adjust begin.");
|
||||||
if ((value < 31 || value > 254))
|
if ((value < 31 || value > 254))
|
||||||
@ -185,11 +193,12 @@ esp_err_t zh_bh1750_adjust(const uint8_t value)
|
|||||||
return ESP_ERR_NOT_FOUND;
|
return ESP_ERR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
esp_err_t esp_err = ESP_OK;
|
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
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
||||||
i2c_master_start(i2c_cmd_handle);
|
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, _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);
|
i2c_master_stop(i2c_cmd_handle);
|
||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
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_cmd_handle = i2c_cmd_link_create();
|
||||||
i2c_master_start(i2c_cmd_handle);
|
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, _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);
|
i2c_master_stop(i2c_cmd_handle);
|
||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||||
#else
|
#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[0], 1, 1000 / portTICK_PERIOD_MS);
|
||||||
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[1], 1, 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
if (esp_err != ESP_OK)
|
if (esp_err != ESP_OK)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user