155 lines
4.3 KiB
C++
155 lines
4.3 KiB
C++
#include "sensor/touch_channel.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace borla {
|
|
|
|
TouchChannel::TouchChannel(const TouchChannelConfig& config)
|
|
: config_(config)
|
|
{
|
|
}
|
|
|
|
void TouchChannel::ResetCalibration()
|
|
{
|
|
calibration_count_ = 0;
|
|
calibration_sum_ = 0.0;
|
|
calibration_square_sum_ = 0.0;
|
|
baseline_ = 0.0F;
|
|
filtered_ = 0.0F;
|
|
noise_deviation_ = 0.0F;
|
|
threshold_ = 0.0F;
|
|
press_count_ = 0;
|
|
release_count_ = 0;
|
|
calibrated_ = false;
|
|
pressed_ = false;
|
|
stuck_lockout_ = false;
|
|
pressed_at_ms_ = 0;
|
|
}
|
|
|
|
void TouchChannel::AddCalibrationSample(uint32_t raw)
|
|
{
|
|
calibration_count_++;
|
|
calibration_sum_ += static_cast<double>(raw);
|
|
calibration_square_sum_ +=
|
|
static_cast<double>(raw) * static_cast<double>(raw);
|
|
}
|
|
|
|
bool TouchChannel::FinishCalibration()
|
|
{
|
|
if (calibration_count_ < 2) {
|
|
return false;
|
|
}
|
|
const double count = static_cast<double>(calibration_count_);
|
|
const double mean = calibration_sum_ / count;
|
|
const double variance = std::max(
|
|
0.0, calibration_square_sum_ / count - mean * mean);
|
|
baseline_ = static_cast<float>(mean);
|
|
filtered_ = baseline_;
|
|
noise_deviation_ = std::max(
|
|
config_.minimum_noise_deviation,
|
|
static_cast<float>(std::sqrt(variance)));
|
|
UpdateThreshold();
|
|
calibrated_ = true;
|
|
return true;
|
|
}
|
|
|
|
bool TouchChannel::Process(
|
|
uint32_t raw,
|
|
uint64_t now_ms,
|
|
TouchTransition* transition)
|
|
{
|
|
if (!calibrated_ || transition == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
filtered_ = (filtered_ * 7.0F + static_cast<float>(raw)) / 8.0F;
|
|
if (pressed_ && static_cast<float>(raw) < threshold_) {
|
|
// A release must not inherit the long EMA tail of a touch.
|
|
filtered_ = static_cast<float>(raw);
|
|
}
|
|
|
|
if (!pressed_) {
|
|
const float deviation = std::fabs(filtered_ - baseline_);
|
|
const float outlier_limit = std::max(
|
|
config_.minimum_threshold_delta,
|
|
config_.outlier_multiplier * noise_deviation_);
|
|
if (deviation < outlier_limit) {
|
|
baseline_ +=
|
|
(filtered_ - baseline_) /
|
|
config_.baseline_follow_divisor;
|
|
noise_deviation_ +=
|
|
(deviation - noise_deviation_) /
|
|
config_.noise_follow_divisor;
|
|
noise_deviation_ = std::max(
|
|
noise_deviation_,
|
|
config_.minimum_noise_deviation);
|
|
UpdateThreshold();
|
|
}
|
|
}
|
|
|
|
const bool touching = filtered_ > threshold_;
|
|
if (stuck_lockout_ && static_cast<float>(raw) < threshold_) {
|
|
stuck_lockout_ = false;
|
|
}
|
|
|
|
if (!pressed_) {
|
|
release_count_ = 0;
|
|
if (touching && !stuck_lockout_) {
|
|
if (press_count_ < std::numeric_limits<uint8_t>::max()) {
|
|
press_count_++;
|
|
}
|
|
if (press_count_ >= config_.press_debounce_samples) {
|
|
press_count_ = 0;
|
|
pressed_ = true;
|
|
pressed_at_ms_ = now_ms;
|
|
transition->action = TouchAction::kDown;
|
|
transition->timestamp_ms = now_ms;
|
|
transition->duration_ms = 0;
|
|
return true;
|
|
}
|
|
} else {
|
|
press_count_ = 0;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
press_count_ = 0;
|
|
if (!touching) {
|
|
if (release_count_ < std::numeric_limits<uint8_t>::max()) {
|
|
release_count_++;
|
|
}
|
|
} else {
|
|
release_count_ = 0;
|
|
}
|
|
|
|
const uint64_t held_ms = now_ms - pressed_at_ms_;
|
|
const bool forced_release =
|
|
held_ms >= config_.maximum_press_ms &&
|
|
static_cast<float>(raw) < threshold_;
|
|
if (release_count_ < config_.release_debounce_samples &&
|
|
!forced_release) {
|
|
return false;
|
|
}
|
|
|
|
release_count_ = 0;
|
|
pressed_ = false;
|
|
stuck_lockout_ = forced_release;
|
|
transition->action = TouchAction::kUp;
|
|
transition->timestamp_ms = now_ms;
|
|
transition->duration_ms = static_cast<uint32_t>(
|
|
std::min<uint64_t>(
|
|
held_ms, std::numeric_limits<uint32_t>::max()));
|
|
return true;
|
|
}
|
|
|
|
void TouchChannel::UpdateThreshold()
|
|
{
|
|
threshold_ = baseline_ + std::max(
|
|
config_.minimum_threshold_delta,
|
|
config_.threshold_noise_multiplier * noise_deviation_);
|
|
}
|
|
|
|
} // namespace borla
|