ESP32-S3 吊坠设备固件,集成火山引擎 RTC 语音助手、蓝牙配网、 VEML7700 环境光传感器驱动及石头同频匹配交友功能。 VEML7700 驱动: - 基于 ESP-IDF i2c_master API 实现,复用项目 I2cDevice 基类 - 支持 ALS + White 双通道、自动量程、Vishay 非线性校正 - 3 次采样取中位数过滤偶发异常 石头同频匹配算法(双维度): - 维度1:光谱比值 ALS/White(石头固有光学特征,不随光照强度变化) - 维度2:亮度等级(5级对数划分,排除极端环境差异) - 比值阈值 15%,实测同石头姿势变化波动 1.6%~9.6%,安全余量充足 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
57 lines
1.9 KiB
C++
57 lines
1.9 KiB
C++
#include "i2c_device.h"
|
|
|
|
#include <esp_log.h>
|
|
#include <cstring>
|
|
|
|
#define TAG "I2cDevice"
|
|
|
|
|
|
I2cDevice::I2cDevice(i2c_master_bus_handle_t i2c_bus, uint8_t addr) {
|
|
i2c_device_config_t i2c_device_cfg = {
|
|
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
|
.device_address = addr,
|
|
.scl_speed_hz = 400 * 1000,
|
|
.scl_wait_us = 0,
|
|
.flags = {
|
|
.disable_ack_check = 0,
|
|
},
|
|
};
|
|
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus, &i2c_device_cfg, &i2c_device_));
|
|
assert(i2c_device_ != NULL);
|
|
}
|
|
|
|
void I2cDevice::WriteReg(uint8_t reg, uint8_t value) {
|
|
uint8_t buffer[2] = {reg, value};
|
|
esp_err_t ret = i2c_master_transmit(i2c_device_, buffer, 2, 100);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to write register 0x%02X with value 0x%02X: %s", reg, value, esp_err_to_name(ret));
|
|
}
|
|
}
|
|
|
|
esp_err_t I2cDevice::WriteRegWithError(uint8_t reg, uint8_t value) {
|
|
uint8_t buffer[2] = {reg, value};
|
|
esp_err_t ret = i2c_master_transmit(i2c_device_, buffer, 2, 100);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to write register 0x%02X with value 0x%02X: %s", reg, value, esp_err_to_name(ret));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
uint8_t I2cDevice::ReadReg(uint8_t reg) {
|
|
uint8_t buffer[1];
|
|
esp_err_t ret = i2c_master_transmit_receive(i2c_device_, ®, 1, buffer, 1, 100);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to read register 0x%02X: %s", reg, esp_err_to_name(ret));
|
|
return 0xFF; // 返回错误值
|
|
}
|
|
return buffer[0];
|
|
}
|
|
|
|
void I2cDevice::ReadRegs(uint8_t reg, uint8_t* buffer, size_t length) {
|
|
esp_err_t ret = i2c_master_transmit_receive(i2c_device_, ®, 1, buffer, length, 100);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to read %zu bytes from register 0x%02X: %s", length, reg, esp_err_to_name(ret));
|
|
// 清零缓冲区以避免使用未初始化的数据
|
|
memset(buffer, 0, length);
|
|
}
|
|
} |