bolar_2 backup
This commit is contained in:
385
main/platform/borla_s3/button_input.cc
Normal file
385
main/platform/borla_s3/button_input.cc
Normal file
@@ -0,0 +1,385 @@
|
||||
#include "platform/borla_s3/button_input.h"
|
||||
|
||||
#include "platform/borla_s3/board_config.h"
|
||||
|
||||
// #include <button_gpio.h>
|
||||
// #include <driver/gpio.h>
|
||||
// #include <driver/rtc_io.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_log.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "ButtonInput2";
|
||||
|
||||
// KEY(P1.0) 电平语义:扩展芯片内部上拉,松开=高,按下被拉到低(低有效)。
|
||||
// iot_button 的 get_key_level 要求返回 1 表示"按下"。
|
||||
constexpr uint8_t kKeyPressed = 1;
|
||||
constexpr uint8_t kKeyReleased = 0;
|
||||
|
||||
// I2C 连续读失败达到该次数才认为按键已松开,避免一次总线抖动被当成"松开沿"。
|
||||
constexpr uint32_t kKeyReadFailLimit = 20;
|
||||
|
||||
// 初始化阶段等待按键松开:采样间隔与确认松开所需连续采样次数。
|
||||
constexpr uint32_t kReleaseSampleIntervalMs = 20;
|
||||
constexpr uint8_t kReleaseConfirmSamples = 2;
|
||||
|
||||
bool RegisterButtonCallback(
|
||||
button_handle_t button,
|
||||
button_event_t event,
|
||||
button_event_args_t* args,
|
||||
button_cb_t callback,
|
||||
void* context)
|
||||
{
|
||||
const esp_err_t result =
|
||||
iot_button_register_cb(button, event, args, callback, context);
|
||||
if (result == ESP_OK) {
|
||||
return true;
|
||||
}
|
||||
ESP_LOGE(kTag, "failed to register button event=%d: %s",
|
||||
static_cast<int>(event), esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ButtonInput::ButtonInput(
|
||||
IEventPublisher& publisher,
|
||||
//const IRemoteBehaviorConfig& behavior_config)
|
||||
const IRemoteBehaviorConfig& behavior_config,TCA9555& tca9555)
|
||||
: publisher_(publisher),behavior_config_(behavior_config),tca9555_(tca9555)
|
||||
|
||||
{
|
||||
}
|
||||
|
||||
ButtonInput::~ButtonInput()
|
||||
{
|
||||
if (button_ != nullptr) {
|
||||
iot_button_delete(button_);
|
||||
}
|
||||
}
|
||||
|
||||
/*bool ButtonInput::Initialize()
|
||||
{
|
||||
gpio_hold_dis(board_config::kPowerButtonGpio);
|
||||
rtc_gpio_hold_dis(board_config::kPowerButtonGpio);
|
||||
rtc_gpio_deinit(board_config::kPowerButtonGpio);
|
||||
|
||||
gpio_config_t input_config = {};
|
||||
input_config.pin_bit_mask = 1ULL << board_config::kPowerButtonGpio;
|
||||
input_config.mode = GPIO_MODE_INPUT;
|
||||
// input_config.pull_up_en = GPIO_PULLUP_DISABLE;
|
||||
// input_config.pull_down_en = GPIO_PULLDOWN_ENABLE;//2026-7-29 ckkj debug
|
||||
input_config.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
input_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
input_config.intr_type = GPIO_INTR_DISABLE;
|
||||
esp_err_t result = gpio_config(&input_config);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to configure GPIO%d: %s",
|
||||
static_cast<int>(board_config::kPowerButtonGpio),
|
||||
esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
int waited_ms = 0;
|
||||
while (gpio_get_level(board_config::kPowerButtonGpio) == 1 &&
|
||||
waited_ms < board_config::kButtonReleaseWaitMs) {
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
waited_ms += 20;
|
||||
}
|
||||
if (gpio_get_level(board_config::kPowerButtonGpio) == 1) {
|
||||
ESP_LOGW(kTag, "power button remained pressed after %d ms",
|
||||
waited_ms);
|
||||
}
|
||||
|
||||
const button_config_t button_config = {
|
||||
.long_press_time = board_config::kButtonLongPressMs,
|
||||
.short_press_time = board_config::kButtonShortPressMs,
|
||||
};
|
||||
const button_gpio_config_t gpio_config = {
|
||||
.gpio_num = board_config::kPowerButtonGpio,
|
||||
//.active_level = 1,//2026-7-29 ckkj debug
|
||||
.active_level = 0,
|
||||
.enable_power_save = false,
|
||||
.disable_pull = false,
|
||||
};
|
||||
result =
|
||||
iot_button_new_gpio_device(&button_config, &gpio_config, &button_);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to create GPIO button: %s",
|
||||
esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!RegisterCallbacks()) {
|
||||
iot_button_delete(button_);
|
||||
button_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(kTag, "power button initialized on GPIO%d",
|
||||
static_cast<int>(board_config::kPowerButtonGpio));
|
||||
return true;
|
||||
}*/
|
||||
|
||||
uint8_t ButtonInput::TcaGetKeyLevel(button_driver_t* driver)
|
||||
{
|
||||
if (driver == nullptr) {
|
||||
return kKeyReleased;
|
||||
}
|
||||
auto* ctx = reinterpret_cast<TcaButtonCtx*>(driver);
|
||||
if (ctx->tca == nullptr) {
|
||||
return kKeyReleased;
|
||||
}
|
||||
|
||||
uint8_t port1 = 0xFF;
|
||||
if (ctx->tca->read_p1_input(&port1) != ESP_OK) {
|
||||
// 读失败不能当成"松开":一次 I2C 抖动就会制造一个假的释放沿,
|
||||
// 让 iot_button 把一次真实长按拆成多个点击。保持上一次去抖结果,
|
||||
// 只有连续失败足够多才退回"未按下"。
|
||||
if (++ctx->read_fail_count >= kKeyReadFailLimit) {
|
||||
ctx->read_fail_count = 0;
|
||||
ctx->same_count = 0;
|
||||
ctx->stable_level = kKeyReleased;
|
||||
ESP_LOGW(kTag, "P1.0 read failed %u times, treat as released",
|
||||
static_cast<unsigned>(kKeyReadFailLimit));
|
||||
}
|
||||
} else {
|
||||
ctx->read_fail_count = 0;
|
||||
|
||||
// 按下 = P1.0 为低(KEY 内部上拉,按下被拉到低),松开 = 高。
|
||||
const uint8_t raw_pressed =
|
||||
((port1 & (1U << TCA_P1_KEY)) == 0U) ? kKeyPressed : kKeyReleased;
|
||||
const uint32_t now_ms = pdTICKS_TO_MS(xTaskGetTickCount());
|
||||
|
||||
if (raw_pressed == ctx->stable_level) {
|
||||
// 与当前稳定电平一致,清空突变计数
|
||||
ctx->same_count = 0;
|
||||
} else if (++ctx->same_count >= board_config::kButtonDebounceSamples) {
|
||||
// 关键:必须是"连续"多次读到新电平才确认变化。
|
||||
// 这样交替抖动的噪声(高-低-高-低)永远累积不到阈值。
|
||||
ctx->same_count = 0;
|
||||
ctx->stable_level = raw_pressed;
|
||||
if (raw_pressed == kKeyPressed) {
|
||||
ctx->press_start_ms = now_ms;
|
||||
ESP_LOGI(kTag, "P1.0 pressed (port1=0x%02x)", port1);
|
||||
} else {
|
||||
ctx->last_press_ms = now_ms - ctx->press_start_ms;
|
||||
ESP_LOGI(kTag, "P1.0 released (held=%lums)",
|
||||
static_cast<unsigned long>(ctx->last_press_ms));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 多击分组收尾:只有在"松开"状态且超过分组窗口后才发布。
|
||||
// 按下期间窗口不生效,因此按得慢的一击不会把整组多击拆散。
|
||||
if (ctx->owner != nullptr) {
|
||||
ctx->owner->CloseClickGroupIfIdle(
|
||||
ctx->stable_level, pdTICKS_TO_MS(xTaskGetTickCount()));
|
||||
}
|
||||
return ctx->stable_level;
|
||||
}
|
||||
|
||||
esp_err_t ButtonInput::TcaDelDriver(button_driver_t* driver)
|
||||
{
|
||||
// driver 嵌在 ButtonInput 对象里,不需要单独释放
|
||||
(void)driver;
|
||||
return ESP_OK;
|
||||
}
|
||||
bool ButtonInput::ReadTcaButtonRaw(bool* pressed)
|
||||
{
|
||||
uint8_t port1 = 0xFF;
|
||||
if (tca9555_.read_p1_input(&port1) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
// 与 TcaGetKeyLevel 保持同一极性:KEY 内部上拉,低电平=按下。
|
||||
// (之前这里是 != 0,即把"空闲高电平"当成按下,导致启动时白等 5 秒
|
||||
// 并打印 "power button remained pressed" 的误报警告。)
|
||||
*pressed = (port1 & (1U << TCA_P1_KEY)) == 0U;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ButtonInput::WaitTcaButtonReleased(uint32_t timeout_ms)
|
||||
{
|
||||
uint32_t waited_ms = 0;
|
||||
uint8_t released_samples = 0;
|
||||
while (waited_ms < timeout_ms) {
|
||||
bool pressed = false;
|
||||
if (!ReadTcaButtonRaw(&pressed)) {
|
||||
// I2C 读失败:不能当成"松开",保持等待并继续采样
|
||||
released_samples = 0;
|
||||
} else if (!pressed) {
|
||||
// 连续多次读到松开才退出,避免毛刺让这里提前返回
|
||||
if (++released_samples >= kReleaseConfirmSamples) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
released_samples = 0;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(kReleaseSampleIntervalMs));
|
||||
waited_ms += kReleaseSampleIntervalMs;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ButtonInput::Initialize()
|
||||
{
|
||||
bool pressed = false;
|
||||
if (!ReadTcaButtonRaw(&pressed)) {
|
||||
ESP_LOGE(kTag, "failed to read TCA9555 P1.0 during button init");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!WaitTcaButtonReleased(board_config::kButtonReleaseWaitMs)) {
|
||||
ESP_LOGW(kTag, "power button remained pressed after %d ms",board_config::kButtonReleaseWaitMs);
|
||||
}
|
||||
|
||||
// 初始化自定义 driver 上下文(嵌入在本对象内,生命周期一致)
|
||||
driver_ctx_.tca = &tca9555_;
|
||||
driver_ctx_.owner = this;
|
||||
driver_ctx_.base.enable_power_save = false;
|
||||
driver_ctx_.base.get_key_level = &ButtonInput::TcaGetKeyLevel;
|
||||
driver_ctx_.base.enter_power_save = nullptr;
|
||||
driver_ctx_.base.del = &ButtonInput::TcaDelDriver;
|
||||
// 用当前真实电平作为去抖初值,避免开机瞬间凭空多出一个按下沿
|
||||
if (ReadTcaButtonRaw(&pressed)) {
|
||||
driver_ctx_.stable_level = pressed ? kKeyPressed : kKeyReleased;
|
||||
}
|
||||
driver_ctx_.same_count = 0;
|
||||
driver_ctx_.read_fail_count = 0;
|
||||
|
||||
const button_config_t button_config = {
|
||||
.long_press_time = board_config::kButtonLongPressMs,
|
||||
.short_press_time = board_config::kButtonShortPressMs,
|
||||
};
|
||||
const esp_err_t result = iot_button_create(&button_config, &driver_ctx_.base, &button_);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to create TCA9555 custom button: %s",esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!RegisterCallbacks()) {
|
||||
iot_button_delete(button_);
|
||||
button_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(kTag, "power button initialized on TCA9555 P1.0 (bit=%u, active low, debounce=%u samples)",
|
||||
static_cast<unsigned>(TCA_P1_KEY),
|
||||
static_cast<unsigned>(board_config::kButtonDebounceSamples));
|
||||
return true;
|
||||
}//////2026-9-4 ckkj add
|
||||
|
||||
// 边沿事件(iot_button 的按下/松开事件每次物理边沿都会回调一次,
|
||||
// 与组件内部的多击分组无关)——这里只记录时间戳,点击次数自己归组。
|
||||
void ButtonInput::OnPressDown(void*, void* context)
|
||||
{
|
||||
auto* self = static_cast<ButtonInput*>(context);
|
||||
self->press_down_ms_ = pdTICKS_TO_MS(xTaskGetTickCount());
|
||||
}
|
||||
|
||||
void ButtonInput::OnPressUp(void*, void* context)
|
||||
{
|
||||
auto* self = static_cast<ButtonInput*>(context);
|
||||
const uint32_t now_ms = pdTICKS_TO_MS(xTaskGetTickCount());
|
||||
const uint32_t held_ms = now_ms - self->press_down_ms_;
|
||||
|
||||
// 长按由 BUTTON_LONG_PRESS_START 处理,不计入多击分组。
|
||||
// 注意判据是"本次按了多久",与分组窗口无关。
|
||||
if (held_ms >= board_config::kButtonLongPressMs) {
|
||||
ESP_LOGD(kTag, "press released after %lums, treated as long press",
|
||||
static_cast<unsigned long>(held_ms));
|
||||
return;
|
||||
}
|
||||
|
||||
if (self->click_count_ < 0xFFU) {
|
||||
++self->click_count_;
|
||||
}
|
||||
self->last_release_ms_ = now_ms;
|
||||
self->click_group_open_ = true;
|
||||
ESP_LOGD(kTag, "click %u in group (held=%lums)",
|
||||
static_cast<unsigned>(self->click_count_),
|
||||
static_cast<unsigned long>(held_ms));
|
||||
}
|
||||
|
||||
// 松开并保持 kButtonMultiClickWindowMs 之后才发布整组点击次数。
|
||||
// 由 TcaGetKeyLevel 每 5ms 驱动一次,避免额外定时器。
|
||||
void ButtonInput::CloseClickGroupIfIdle(uint8_t stable_level, uint32_t now_ms)
|
||||
{
|
||||
if (!click_group_open_ || stable_level != kKeyReleased) {
|
||||
// 还在按下(或已经发布过)时不收尾:连击间隔再长也会并入同一组
|
||||
return;
|
||||
}
|
||||
if ((now_ms - last_release_ms_) < board_config::kButtonMultiClickWindowMs) {
|
||||
return;
|
||||
}
|
||||
|
||||
click_group_open_ = false;
|
||||
const uint8_t count = click_count_;
|
||||
click_count_ = 0;
|
||||
PublishClick(static_cast<int32_t>(count));
|
||||
}
|
||||
|
||||
void ButtonInput::OnLongPress(void*, void* context)
|
||||
{
|
||||
static_cast<ButtonInput*>(context)->PublishLongPress();
|
||||
}
|
||||
|
||||
void ButtonInput::PublishClick(int32_t count)
|
||||
{
|
||||
static uint64_t last_click_ts = 0;
|
||||
uint32_t now = pdTICKS_TO_MS(xTaskGetTickCount());
|
||||
// 两次click间隔小于120ms,判定为噪声虚假事件,直接丢弃
|
||||
if((now - last_click_ts) < 120U)
|
||||
{
|
||||
ESP_LOGW(kTag,"filter spurious click count=%ld",count);
|
||||
return;
|
||||
}
|
||||
last_click_ts = now;
|
||||
|
||||
ESP_LOGI(kTag, "button clicked count=%ld", static_cast<long>(count));
|
||||
const ButtonAction action =
|
||||
behavior_config_.ResolveButtonAction(
|
||||
static_cast<uint8_t>(count));
|
||||
if (!publisher_.Publish(
|
||||
AppEvent::Make(
|
||||
AppEventType::kButtonClicked,
|
||||
0,
|
||||
count,
|
||||
static_cast<uint32_t>(action)))) {
|
||||
ESP_LOGE(kTag, "failed to publish button click count=%ld",
|
||||
static_cast<long>(count));
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonInput::PublishLongPress()
|
||||
{
|
||||
ESP_LOGI(kTag, "button long-pressed");
|
||||
if (!publisher_.Publish(
|
||||
AppEvent::Make(AppEventType::kButtonLongPressed))) {
|
||||
ESP_LOGE(kTag, "failed to publish long press");
|
||||
}
|
||||
}
|
||||
|
||||
bool ButtonInput::RegisterCallbacks()
|
||||
{
|
||||
// 只注册边沿事件和长按:点击次数由 OnPressUp + CloseClickGroupIfIdle 归组,
|
||||
// 不再使用 iot_button 的 SINGLE/DOUBLE/MULTIPLE_CLICK(其内置分组会把
|
||||
// 按下超过 short_press_time 的那一击当成分组结束并丢弃整组点击)。
|
||||
bool valid = true;
|
||||
valid &= RegisterButtonCallback(
|
||||
button_, BUTTON_PRESS_DOWN, nullptr, &ButtonInput::OnPressDown,
|
||||
this);
|
||||
valid &= RegisterButtonCallback(
|
||||
button_, BUTTON_PRESS_UP, nullptr, &ButtonInput::OnPressUp,
|
||||
this);
|
||||
valid &= RegisterButtonCallback(
|
||||
button_, BUTTON_LONG_PRESS_START, nullptr, &ButtonInput::OnLongPress,
|
||||
this);
|
||||
return valid;
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user