WIP
This commit is contained in:
@ -2,11 +2,12 @@
|
|||||||
|
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "driver/i2c.h"
|
#include "driver/i2c_master.h"
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
// #include "driver/i2c.h"
|
||||||
|
// #ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#endif
|
// #endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Default values for zh_bh1750_init_config_t structure for initial initialization of the sensor.
|
* @brief Default values for zh_bh1750_init_config_t structure for initial initialization of the sensor.
|
||||||
@ -51,6 +52,7 @@ extern "C"
|
|||||||
ONE_TIME // One time measurement. Sensor is power down after measurement.
|
ONE_TIME // One time measurement. Sensor is power down after measurement.
|
||||||
} work_mode;
|
} work_mode;
|
||||||
bool i2c_port; // I2C port.
|
bool i2c_port; // I2C port.
|
||||||
|
i2c_master_bus_handle_t i2c_handle;
|
||||||
} zh_bh1750_init_config_t;
|
} zh_bh1750_init_config_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
145
zh_bh1750.c
145
zh_bh1750.c
@ -21,6 +21,7 @@ static bool _is_first_start = false;
|
|||||||
static uint8_t _time = 0;
|
static uint8_t _time = 0;
|
||||||
static uint8_t _sensivity = SENSITIVITY_DEFAULT;
|
static uint8_t _sensivity = SENSITIVITY_DEFAULT;
|
||||||
static bool _is_initialized = false;
|
static bool _is_initialized = false;
|
||||||
|
static i2c_master_dev_handle_t _bh1750_handle;
|
||||||
|
|
||||||
static const char *TAG = "zh_bh1750";
|
static const char *TAG = "zh_bh1750";
|
||||||
|
|
||||||
@ -33,6 +34,13 @@ esp_err_t zh_bh1750_init(zh_bh1750_init_config_t *config)
|
|||||||
return ESP_ERR_INVALID_ARG;
|
return ESP_ERR_INVALID_ARG;
|
||||||
}
|
}
|
||||||
_init_config = *config;
|
_init_config = *config;
|
||||||
|
i2c_device_config_t bh1750_config = {
|
||||||
|
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||||
|
.device_address = _init_config.i2c_address,
|
||||||
|
.scl_speed_hz = 100000,
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(i2c_master_bus_add_device(_init_config.i2c_handle, &bh1750_config, &_bh1750_handle));
|
||||||
|
ESP_ERROR_CHECK(i2c_master_probe(_init_config.i2c_handle, _init_config.i2c_address, -1));
|
||||||
switch (_init_config.operation_mode)
|
switch (_init_config.operation_mode)
|
||||||
{
|
{
|
||||||
case LOW_RESOLUTION:;
|
case LOW_RESOLUTION:;
|
||||||
@ -82,33 +90,36 @@ esp_err_t zh_bh1750_read(float *data)
|
|||||||
_is_first_start = true;
|
_is_first_start = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
ESP_ERROR_CHECK(i2c_master_transmit(_bh1750_handle, &_command, sizeof(_command), 1000 / portTICK_PERIOD_MS));
|
||||||
i2c_master_start(i2c_cmd_handle);
|
// i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
||||||
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
|
// i2c_master_start(i2c_cmd_handle);
|
||||||
i2c_master_write_byte(i2c_cmd_handle, _command, true);
|
// i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
|
||||||
i2c_master_stop(i2c_cmd_handle);
|
// i2c_master_write_byte(i2c_cmd_handle, _command, true);
|
||||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
// i2c_master_stop(i2c_cmd_handle);
|
||||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
// esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||||
if (esp_err != ESP_OK)
|
// i2c_cmd_link_delete(i2c_cmd_handle);
|
||||||
{
|
// if (esp_err != ESP_OK)
|
||||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
// {
|
||||||
return esp_err;
|
// ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
||||||
}
|
// return esp_err;
|
||||||
|
// }
|
||||||
vTaskDelay(((_sensivity / SENSITIVITY_DEFAULT) * _time) / portTICK_PERIOD_MS);
|
vTaskDelay(((_sensivity / SENSITIVITY_DEFAULT) * _time) / portTICK_PERIOD_MS);
|
||||||
READ:
|
READ:
|
||||||
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_READ, true);
|
// i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_READ, true);
|
||||||
i2c_master_read_byte(i2c_cmd_handle, &sensor_data_high, I2C_MASTER_ACK);
|
// i2c_master_read_byte(i2c_cmd_handle, &sensor_data_high, I2C_MASTER_ACK);
|
||||||
i2c_master_read_byte(i2c_cmd_handle, &sensor_data_low, I2C_MASTER_NACK);
|
// i2c_master_read_byte(i2c_cmd_handle, &sensor_data_low, I2C_MASTER_NACK);
|
||||||
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);
|
||||||
if (esp_err != ESP_OK)
|
// if (esp_err != ESP_OK)
|
||||||
{
|
// {
|
||||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
// ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
||||||
return esp_err;
|
// return esp_err;
|
||||||
}
|
// }
|
||||||
|
ESP_ERROR_CHECK(i2c_master_receive(_bh1750_handle, &sensor_data_high, 8, -1));
|
||||||
|
ESP_ERROR_CHECK(i2c_master_receive(_bh1750_handle, &sensor_data_low, 8, -1));
|
||||||
if (_init_config.operation_mode == HIGH_RESOLUTION_2)
|
if (_init_config.operation_mode == HIGH_RESOLUTION_2)
|
||||||
{
|
{
|
||||||
*data = (sensor_data_high << 8 | sensor_data_low) * (1 / 1.2 * (69.0 / _sensivity) / 2);
|
*data = (sensor_data_high << 8 | sensor_data_low) * (1 / 1.2 * (69.0 / _sensivity) / 2);
|
||||||
@ -123,47 +134,47 @@ READ:
|
|||||||
|
|
||||||
esp_err_t zh_bh1750_adjust(uint8_t value)
|
esp_err_t zh_bh1750_adjust(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))
|
||||||
{
|
// {
|
||||||
ESP_LOGE(TAG, "BH1750 adjust fail. Invalid argument.");
|
// ESP_LOGE(TAG, "BH1750 adjust fail. Invalid argument.");
|
||||||
return ESP_ERR_INVALID_ARG;
|
// return ESP_ERR_INVALID_ARG;
|
||||||
}
|
// }
|
||||||
if (_is_initialized == false)
|
// if (_is_initialized == false)
|
||||||
{
|
// {
|
||||||
ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized.");
|
// ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized.");
|
||||||
return ESP_ERR_NOT_FOUND;
|
// return ESP_ERR_NOT_FOUND;
|
||||||
}
|
// }
|
||||||
esp_err_t esp_err = ESP_OK;
|
// esp_err_t esp_err = ESP_OK;
|
||||||
_sensivity = value;
|
// _sensivity = value;
|
||||||
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, value >> 5 | MEASUREMENT_TIME_HIGH_BIT, 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);
|
||||||
if (esp_err != ESP_OK)
|
// if (esp_err != ESP_OK)
|
||||||
{
|
// {
|
||||||
ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error.");
|
// ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error.");
|
||||||
return esp_err;
|
// return esp_err;
|
||||||
}
|
// }
|
||||||
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, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT, 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);
|
||||||
if (esp_err != ESP_OK)
|
// if (esp_err != ESP_OK)
|
||||||
{
|
// {
|
||||||
ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error.");
|
// ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error.");
|
||||||
return esp_err;
|
// return esp_err;
|
||||||
}
|
// }
|
||||||
if (_init_config.work_mode == CONTINUOUSLY)
|
// if (_init_config.work_mode == CONTINUOUSLY)
|
||||||
{
|
// {
|
||||||
_is_first_start = false;
|
// _is_first_start = false;
|
||||||
}
|
// }
|
||||||
ESP_LOGI(TAG, "BH1750 adjust success.");
|
// ESP_LOGI(TAG, "BH1750 adjust success.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
Reference in New Issue
Block a user