bolar_2 backup
This commit is contained in:
351
main/sensor/sensor_service.cc
Normal file
351
main/sensor/sensor_service.cc
Normal file
@@ -0,0 +1,351 @@
|
||||
#include "sensor/sensor_service.h"
|
||||
|
||||
#include "sensor/sensor_message_codec.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "SensorService2";
|
||||
constexpr uint32_t kTouchWarmupMs = 500;
|
||||
constexpr uint32_t kTouchCalibrationSamples = 10;
|
||||
constexpr uint32_t kTouchCalibrationIntervalMs = 20;
|
||||
constexpr uint32_t kTouchPollMs = 50;
|
||||
constexpr uint32_t kImuPollMs = 20;
|
||||
constexpr uint32_t kBatteryPollMs = 10000;
|
||||
|
||||
uint64_t NowMs()
|
||||
{
|
||||
return static_cast<uint64_t>(esp_timer_get_time() / 1000);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
SensorService::SensorService(
|
||||
CapacitiveTouchInput& touch,
|
||||
Bmi270Sensor& imu,
|
||||
BatteryMonitor& battery,
|
||||
ISensorMessageSink& sink)
|
||||
: touch_(touch),
|
||||
imu_(imu),
|
||||
battery_(battery),
|
||||
sink_(sink)
|
||||
{
|
||||
}
|
||||
|
||||
bool SensorService::Initialize()
|
||||
{
|
||||
if (running_.load()) {
|
||||
return true;
|
||||
}
|
||||
stopped_events_ =
|
||||
xEventGroupCreateStatic(&stopped_events_control_);
|
||||
if (stopped_events_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
touch_enabled_ = touch_.Initialize();
|
||||
imu_enabled_ = imu_.Initialize();
|
||||
battery_enabled_ = battery_.Initialize();
|
||||
running_.store(true);
|
||||
|
||||
if (touch_enabled_) {
|
||||
touch_task_ = xTaskCreateStaticPinnedToCore(
|
||||
&SensorService::TouchTaskEntry,
|
||||
"touch_sensor2",
|
||||
touch_task_stack_.size(),
|
||||
this,
|
||||
kTaskPriority,
|
||||
touch_task_stack_.data(),
|
||||
&touch_task_control_,
|
||||
0);
|
||||
touch_enabled_ = touch_task_ != nullptr;
|
||||
}
|
||||
if (imu_enabled_) {
|
||||
imu_task_ = xTaskCreateStaticPinnedToCore(
|
||||
&SensorService::ImuTaskEntry,
|
||||
"imu_sensor2",
|
||||
imu_task_stack_.size(),
|
||||
this,
|
||||
kTaskPriority + 1,
|
||||
imu_task_stack_.data(),
|
||||
&imu_task_control_,
|
||||
1);
|
||||
imu_enabled_ = imu_task_ != nullptr;
|
||||
}
|
||||
if (battery_enabled_) {
|
||||
battery_task_ = xTaskCreateStatic(
|
||||
&SensorService::BatteryTaskEntry,
|
||||
"battery_sensor2",
|
||||
battery_task_stack_.size(),
|
||||
this,
|
||||
kTaskPriority,
|
||||
battery_task_stack_.data(),
|
||||
&battery_task_control_);
|
||||
battery_enabled_ = battery_task_ != nullptr;
|
||||
}
|
||||
|
||||
const bool any_enabled =
|
||||
touch_enabled_ || imu_enabled_ || battery_enabled_;
|
||||
if (!any_enabled) {
|
||||
running_.store(false);
|
||||
ESP_LOGE(kTag, "no sensor adapter available");
|
||||
return false;
|
||||
}
|
||||
ESP_LOGI(kTag, "started touch=%d imu=%d battery=%d",
|
||||
touch_enabled_, imu_enabled_, battery_enabled_);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SensorService::Stop()
|
||||
{
|
||||
if (!running_.exchange(false) || stopped_events_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
EventBits_t expected = 0;
|
||||
if (touch_enabled_) {
|
||||
expected |= kTouchStoppedBit;
|
||||
}
|
||||
if (imu_enabled_) {
|
||||
expected |= kImuStoppedBit;
|
||||
}
|
||||
if (battery_enabled_) {
|
||||
expected |= kBatteryStoppedBit;
|
||||
}
|
||||
if (expected != 0) {
|
||||
xEventGroupWaitBits(
|
||||
stopped_events_,
|
||||
expected,
|
||||
pdFALSE,
|
||||
pdTRUE,
|
||||
pdMS_TO_TICKS(12000));
|
||||
}
|
||||
ESP_LOGI(kTag, "stopped");
|
||||
}
|
||||
|
||||
BatteryStatus SensorService::GetBatteryStatus() const
|
||||
{
|
||||
portENTER_CRITICAL(&status_lock_);
|
||||
const BatteryStatus result = battery_status_;
|
||||
portEXIT_CRITICAL(&status_lock_);
|
||||
return result;
|
||||
}
|
||||
|
||||
SensorDiagnostics SensorService::GetDiagnostics() const
|
||||
{
|
||||
return {
|
||||
.touch_events_sent = touch_events_sent_.load(),
|
||||
.imu_batches_sent = imu_batches_sent_.load(),
|
||||
.gravity_events_sent = gravity_events_sent_.load(),
|
||||
.messages_dropped = messages_dropped_.load(),
|
||||
.touch_read_errors = touch_read_errors_.load(),
|
||||
.imu_read_errors = imu_read_errors_.load(),
|
||||
.battery_read_errors = battery_read_errors_.load(),
|
||||
};
|
||||
}
|
||||
|
||||
void SensorService::TouchTaskEntry(void* context)
|
||||
{
|
||||
static_cast<SensorService*>(context)->RunTouch();
|
||||
}
|
||||
|
||||
void SensorService::ImuTaskEntry(void* context)
|
||||
{
|
||||
static_cast<SensorService*>(context)->RunImu();
|
||||
}
|
||||
|
||||
void SensorService::BatteryTaskEntry(void* context)
|
||||
{
|
||||
static_cast<SensorService*>(context)->RunBattery();
|
||||
}
|
||||
|
||||
void SensorService::RunTouch()
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(kTouchWarmupMs));
|
||||
for (TouchChannel& channel : touch_channels_) {
|
||||
channel.ResetCalibration();
|
||||
}
|
||||
for (uint32_t sample_index = 0;
|
||||
sample_index < kTouchCalibrationSamples && running_.load();
|
||||
++sample_index) {
|
||||
for (size_t channel = 0;
|
||||
channel < touch_channels_.size();
|
||||
++channel) {
|
||||
uint32_t raw = 0;
|
||||
if (touch_.Read(channel, &raw)) {
|
||||
touch_channels_[channel].AddCalibrationSample(raw);
|
||||
} else {
|
||||
touch_read_errors_.fetch_add(1);
|
||||
}
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(kTouchCalibrationIntervalMs));
|
||||
}
|
||||
for (size_t channel = 0;
|
||||
channel < touch_channels_.size();
|
||||
++channel) {
|
||||
if (!touch_channels_[channel].FinishCalibration()) {
|
||||
ESP_LOGW(kTag, "touch calibration failed channel=%u",
|
||||
static_cast<unsigned int>(channel));
|
||||
} else {
|
||||
ESP_LOGI(kTag, "touch channel=%u baseline=%.0f threshold=%.0f",
|
||||
static_cast<unsigned int>(channel),
|
||||
static_cast<double>(
|
||||
touch_channels_[channel].baseline()),
|
||||
static_cast<double>(
|
||||
touch_channels_[channel].threshold()));
|
||||
}
|
||||
}
|
||||
|
||||
TickType_t next_tick = xTaskGetTickCount();
|
||||
while (running_.load()) {
|
||||
const uint64_t now_ms = NowMs();
|
||||
for (size_t channel = 0;
|
||||
channel < touch_channels_.size();
|
||||
++channel) {
|
||||
uint32_t raw = 0;
|
||||
if (!touch_.Read(channel, &raw)) {
|
||||
touch_read_errors_.fetch_add(1);
|
||||
continue;
|
||||
}
|
||||
TouchTransition transition{};
|
||||
if (touch_channels_[channel].Process(
|
||||
raw, now_ms, &transition)) {
|
||||
if (transition.action == TouchAction::kDown &&
|
||||
publisher_ != nullptr) {
|
||||
publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kUserActivityDetected,
|
||||
0,
|
||||
static_cast<int32_t>(channel)));
|
||||
}
|
||||
SendTouch({
|
||||
.zone = static_cast<TouchZone>(channel),
|
||||
.action = transition.action,
|
||||
.timestamp_ms = transition.timestamp_ms,
|
||||
.duration_ms = transition.duration_ms,
|
||||
});
|
||||
}
|
||||
}
|
||||
vTaskDelayUntil(
|
||||
&next_tick, pdMS_TO_TICKS(kTouchPollMs));
|
||||
}
|
||||
xEventGroupSetBits(stopped_events_, kTouchStoppedBit);
|
||||
touch_task_ = nullptr;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
void SensorService::RunImu()
|
||||
{
|
||||
TickType_t next_tick = xTaskGetTickCount();
|
||||
while (running_.load()) {
|
||||
ImuSample sample{};
|
||||
if (imu_.Read(&sample)) {
|
||||
const ImuProcessorOutput output =
|
||||
imu_processor_.Process(sample, NowMs());
|
||||
if (output.has_gravity_event) {
|
||||
SendGravity(output.gravity_event);
|
||||
}
|
||||
if (output.has_batch) {
|
||||
SendImu(output.batch);
|
||||
}
|
||||
} else {
|
||||
imu_read_errors_.fetch_add(1);
|
||||
}
|
||||
vTaskDelayUntil(&next_tick, pdMS_TO_TICKS(kImuPollMs));
|
||||
}
|
||||
xEventGroupSetBits(stopped_events_, kImuStoppedBit);
|
||||
imu_task_ = nullptr;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
void SensorService::RunBattery()
|
||||
{
|
||||
while (running_.load()) {
|
||||
BatteryStatus status{};
|
||||
if (battery_.Read(NowMs(), &status)) {
|
||||
portENTER_CRITICAL(&status_lock_);
|
||||
battery_status_ = status;
|
||||
portEXIT_CRITICAL(&status_lock_);
|
||||
// One line per actual change, not one per 10-second poll — the
|
||||
// steady-state repeat was pure noise in the conversation log.
|
||||
// Function-local statics are fine: there is one battery task.
|
||||
static int last_logged_percent = -1;
|
||||
static int last_logged_charging = -1;
|
||||
if (static_cast<int>(status.percent) != last_logged_percent ||
|
||||
static_cast<int>(status.charging) != last_logged_charging) {
|
||||
last_logged_percent = static_cast<int>(status.percent);
|
||||
last_logged_charging = static_cast<int>(status.charging);
|
||||
ESP_LOGI(kTag, "battery=%u%% voltage=%umV charging=%d",
|
||||
status.percent,
|
||||
status.millivolts,
|
||||
status.charging);
|
||||
}
|
||||
} else {
|
||||
battery_read_errors_.fetch_add(1);
|
||||
portENTER_CRITICAL(&status_lock_);
|
||||
battery_status_ = {};
|
||||
portEXIT_CRITICAL(&status_lock_);
|
||||
}
|
||||
for (uint32_t waited_ms = 0;
|
||||
running_.load() && waited_ms < kBatteryPollMs;
|
||||
waited_ms += 100) {
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
}
|
||||
xEventGroupSetBits(stopped_events_, kBatteryStoppedBit);
|
||||
battery_task_ = nullptr;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
bool SensorService::SendTouch(const TouchEvent& event)
|
||||
{
|
||||
std::array<char, 256> message{};
|
||||
size_t size = 0;
|
||||
const bool sent =
|
||||
SensorMessageCodec::BuildTouch(
|
||||
event, message.data(), message.size(), &size) &&
|
||||
sink_.SendSensorMessage(message.data(), size);
|
||||
if (sent) {
|
||||
touch_events_sent_.fetch_add(1);
|
||||
} else {
|
||||
messages_dropped_.fetch_add(1);
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
bool SensorService::SendImu(const ImuBatch& batch)
|
||||
{
|
||||
std::array<char, 768> message{};
|
||||
size_t size = 0;
|
||||
const bool sent =
|
||||
SensorMessageCodec::BuildImu(
|
||||
batch, message.data(), message.size(), &size) &&
|
||||
sink_.SendSensorMessage(message.data(), size);
|
||||
if (sent) {
|
||||
imu_batches_sent_.fetch_add(1);
|
||||
} else {
|
||||
messages_dropped_.fetch_add(1);
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
bool SensorService::SendGravity(const GravityEvent& event)
|
||||
{
|
||||
std::array<char, 256> message{};
|
||||
size_t size = 0;
|
||||
const bool sent =
|
||||
SensorMessageCodec::BuildGravity(
|
||||
event, message.data(), message.size(), &size) &&
|
||||
sink_.SendSensorMessage(message.data(), size);
|
||||
if (sent) {
|
||||
gravity_events_sent_.fetch_add(1);
|
||||
} else {
|
||||
messages_dropped_.fetch_add(1);
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user