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>
113 lines
3.3 KiB
C++
113 lines
3.3 KiB
C++
#include "button.h"
|
||
|
||
#include <esp_log.h>
|
||
|
||
static const char* TAG = "Button";
|
||
#if CONFIG_SOC_ADC_SUPPORTED
|
||
Button::Button(const button_adc_config_t& adc_cfg) {
|
||
button_config_t button_config = {
|
||
.type = BUTTON_TYPE_ADC,
|
||
// .long_press_time = 1000, // 原有长按3秒时的时间
|
||
.long_press_time = 5000, // 长按5秒时间
|
||
.short_press_time = 50,
|
||
.adc_button_config = adc_cfg
|
||
};
|
||
button_handle_ = iot_button_create(&button_config);
|
||
if (button_handle_ == NULL) {
|
||
ESP_LOGE(TAG, "Failed to create button handle");
|
||
return;
|
||
}
|
||
}
|
||
#endif
|
||
|
||
Button::Button(gpio_num_t gpio_num, bool active_high, uint32_t long_press_ms) : gpio_num_(gpio_num) {
|
||
if (gpio_num == GPIO_NUM_NC) {
|
||
return;
|
||
}
|
||
button_config_t button_config = {
|
||
.type = BUTTON_TYPE_GPIO,
|
||
.long_press_time = (uint16_t)long_press_ms,
|
||
.short_press_time = 0, // 0=使用默认值180ms(双击判定窗口)
|
||
.gpio_button_config = {
|
||
.gpio_num = gpio_num,
|
||
.active_level = static_cast<uint8_t>(active_high ? 1 : 0)
|
||
}
|
||
};
|
||
button_handle_ = iot_button_create(&button_config);
|
||
if (button_handle_ == NULL) {
|
||
ESP_LOGE(TAG, "Failed to create button handle");
|
||
return;
|
||
}
|
||
}
|
||
|
||
Button::~Button() {
|
||
if (button_handle_ != NULL) {
|
||
iot_button_delete(button_handle_);
|
||
}
|
||
}
|
||
|
||
void Button::OnPressDown(std::function<void()> callback) {
|
||
if (button_handle_ == nullptr) {
|
||
return;
|
||
}
|
||
on_press_down_ = callback;
|
||
iot_button_register_cb(button_handle_, BUTTON_PRESS_DOWN, [](void* handle, void* usr_data) {
|
||
Button* button = static_cast<Button*>(usr_data);
|
||
if (button->on_press_down_) {
|
||
button->on_press_down_();
|
||
}
|
||
}, this);
|
||
}
|
||
|
||
void Button::OnPressUp(std::function<void()> callback) {
|
||
if (button_handle_ == nullptr) {
|
||
return;
|
||
}
|
||
on_press_up_ = callback;
|
||
iot_button_register_cb(button_handle_, BUTTON_PRESS_UP, [](void* handle, void* usr_data) {
|
||
Button* button = static_cast<Button*>(usr_data);
|
||
if (button->on_press_up_) {
|
||
button->on_press_up_();
|
||
}
|
||
}, this);
|
||
}
|
||
|
||
void Button::OnLongPress(std::function<void()> callback) {
|
||
if (button_handle_ == nullptr) {
|
||
return;
|
||
}
|
||
on_long_press_ = callback;
|
||
iot_button_register_cb(button_handle_, BUTTON_LONG_PRESS_START, [](void* handle, void* usr_data) {
|
||
Button* button = static_cast<Button*>(usr_data);
|
||
if (button->on_long_press_) {
|
||
button->on_long_press_();
|
||
}
|
||
}, this);
|
||
}
|
||
|
||
void Button::OnClick(std::function<void()> callback) {
|
||
if (button_handle_ == nullptr) {
|
||
return;
|
||
}
|
||
on_click_ = callback;
|
||
iot_button_register_cb(button_handle_, BUTTON_SINGLE_CLICK, [](void* handle, void* usr_data) {
|
||
Button* button = static_cast<Button*>(usr_data);
|
||
if (button->on_click_) {
|
||
button->on_click_();
|
||
}
|
||
}, this);
|
||
}
|
||
|
||
void Button::OnDoubleClick(std::function<void()> callback) {
|
||
if (button_handle_ == nullptr) {
|
||
return;
|
||
}
|
||
on_double_click_ = callback;
|
||
iot_button_register_cb(button_handle_, BUTTON_DOUBLE_CLICK, [](void* handle, void* usr_data) {
|
||
Button* button = static_cast<Button*>(usr_data);
|
||
if (button->on_double_click_) {
|
||
button->on_double_click_();
|
||
}
|
||
}, this);
|
||
}
|