Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
7ed4ba3a0d | |||
23cac1f945 | |||
54c3b16697 |
BIN
ds/BH1750.pdf
Executable file
BIN
ds/BH1750.pdf
Executable file
Binary file not shown.
@ -76,10 +76,8 @@ extern "C"
|
||||
* @return
|
||||
* - ESP_OK if read was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_NOT_FOUND if BH1750 is not initialized
|
||||
* - 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_TIMEOUT if operation timeout because the bus is busy
|
||||
* - ESP_ERR_NOT_FOUND if sensor is not initialized
|
||||
* - ESP_ERR_INVALID_RESPONSE if I2C driver error
|
||||
*/
|
||||
esp_err_t zh_bh1750_read(float *data);
|
||||
|
||||
@ -95,10 +93,8 @@ extern "C"
|
||||
* @return
|
||||
* - ESP_OK if adjust was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - 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_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_NOT_FOUND if sensor is not initialized or auto adjust is enabled
|
||||
* - ESP_ERR_INVALID_RESPONSE if I2C driver error
|
||||
*/
|
||||
esp_err_t zh_bh1750_adjust(const uint8_t value);
|
||||
|
||||
|
@ -1 +1 @@
|
||||
1.0.2
|
||||
1.0.5
|
42
zh_bh1750.c
42
zh_bh1750.c
@ -87,7 +87,7 @@ esp_err_t zh_bh1750_read(float *data)
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
REPEATE:;
|
||||
esp_err_t esp_err = ESP_OK;
|
||||
esp_err_t err = ESP_OK;
|
||||
uint8_t sensor_data[2] = {0};
|
||||
if (_init_config.work_mode == CONTINUOUSLY)
|
||||
{
|
||||
@ -106,15 +106,15 @@ REPEATE:;
|
||||
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, _command, true);
|
||||
i2c_master_stop(i2c_cmd_handle);
|
||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
#else
|
||||
esp_err = i2c_master_transmit(_bh1750_handle, &_command, sizeof(_command), 1000 / portTICK_PERIOD_MS);
|
||||
err = i2c_master_transmit(_bh1750_handle, &_command, sizeof(_command), 1000 / portTICK_PERIOD_MS);
|
||||
#endif
|
||||
if (esp_err != ESP_OK)
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
||||
return esp_err;
|
||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error at line %d.", __LINE__);
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
}
|
||||
vTaskDelay(_time / portTICK_PERIOD_MS);
|
||||
READ:
|
||||
@ -125,15 +125,15 @@ READ:
|
||||
i2c_master_read_byte(i2c_cmd_handle, &sensor_data[0], I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(i2c_cmd_handle, &sensor_data[1], I2C_MASTER_NACK);
|
||||
i2c_master_stop(i2c_cmd_handle);
|
||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
#else
|
||||
esp_err = i2c_master_receive(_bh1750_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS);
|
||||
err = i2c_master_receive(_bh1750_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS);
|
||||
#endif
|
||||
if (esp_err != ESP_OK)
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
||||
return esp_err;
|
||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error at line %d.", __LINE__);
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
}
|
||||
uint32_t raw_data = sensor_data[0] << 8 | sensor_data[1];
|
||||
if (raw_data == 65535 || raw_data == 0)
|
||||
@ -192,7 +192,7 @@ esp_err_t _adjust(const uint8_t value)
|
||||
ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
esp_err_t esp_err = ESP_OK;
|
||||
esp_err_t 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();
|
||||
@ -200,28 +200,28 @@ esp_err_t _adjust(const uint8_t value)
|
||||
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, 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);
|
||||
err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
if (esp_err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error.");
|
||||
return esp_err;
|
||||
ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error at line %d.", __LINE__);
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
}
|
||||
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, 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);
|
||||
err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
#else
|
||||
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);
|
||||
err = i2c_master_transmit(_bh1750_handle, &sensitivity_data[0], 1, 1000 / portTICK_PERIOD_MS);
|
||||
err = i2c_master_transmit(_bh1750_handle, &sensitivity_data[1], 1, 1000 / portTICK_PERIOD_MS);
|
||||
#endif
|
||||
if (esp_err != ESP_OK)
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error.");
|
||||
return esp_err;
|
||||
ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error at line %d.", __LINE__);
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
}
|
||||
if (_init_config.work_mode == CONTINUOUSLY)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user