feat: 添加 i2c_master_messager 组件,集成 BH1750 光照传感器支持

This commit is contained in:
Wang Beihong
2026-03-05 20:15:55 +08:00
parent 32f3f9980d
commit bde2df4e13
8 changed files with 362 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "."
REQUIRES wifi-connect esp_lvgl_port lvgl_st7735s_use
REQUIRES wifi-connect esp_lvgl_port lvgl_st7735s_use i2c_master_messager
)

View File

@@ -15,3 +15,4 @@ dependencies:
# # All dependencies of `main` are public by default.
# public: true
espressif/esp_lvgl_port: ^2.7.2
espressif/bh1750: ^2.0.0

View File

@@ -1,9 +1,16 @@
#include <stdio.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_check.h"
#include "wifi-connect.h"
#include "lvgl_st7735s_use.h"
#include "i2c_master_messager.h"
#define BOTANY_I2C_PORT I2C_NUM_0
#define BOTANY_I2C_SCL_GPIO GPIO_NUM_5
#define BOTANY_I2C_SDA_GPIO GPIO_NUM_4
#define BOTANY_READ_PERIOD_MS 1000
void app_main(void)
{
@@ -13,8 +20,33 @@ void app_main(void)
// 启动 LVGL 演示程序,显示简单的界面
ESP_ERROR_CHECK(start_lvgl_demo());
i2c_master_messager_config_t i2c_cfg = {
.i2c_port = BOTANY_I2C_PORT,
.scl_io_num = BOTANY_I2C_SCL_GPIO,
.sda_io_num = BOTANY_I2C_SDA_GPIO,
.read_period_ms = BOTANY_READ_PERIOD_MS,
.bh1750_addr = 0x23,
.bh1750_mode = BH1750_CONTINUE_1LX_RES,
};
ESP_ERROR_CHECK(i2c_master_messager_init(&i2c_cfg));
ESP_ERROR_CHECK(i2c_master_messager_start());
for (;;)
{
i2c_master_messager_data_t sensor_data = {0};
if (i2c_master_messager_get_data(&sensor_data) == ESP_OK && sensor_data.bh1750.valid) {
char text[32] = {0};
snprintf(text, sizeof(text), "Light: %.1f lx", sensor_data.bh1750.lux);
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text(text));
printf("[BH1750] lux=%.2f update_ms=%" PRId64 "\n",
sensor_data.bh1750.lux,
sensor_data.bh1750.last_update_ms);
} else {
ESP_ERROR_CHECK(lvgl_st7735s_set_center_text("Light: waiting..."));
printf("[BH1750] waiting data, err=0x%x\n", sensor_data.bh1750.last_error);
}
vTaskDelay(pdMS_TO_TICKS(1000));
}