bolar_2 backup
This commit is contained in:
109
main/sensor/sensor_service.h
Normal file
109
main/sensor/sensor_service.h
Normal file
@@ -0,0 +1,109 @@
|
||||
#pragma once
|
||||
|
||||
#include "platform/borla_s3/battery_monitor.h"
|
||||
#include "platform/borla_s3/bmi270_sensor.h"
|
||||
#include "platform/borla_s3/capacitive_touch_input.h"
|
||||
#include "ports/battery_status_provider.h"
|
||||
#include "ports/event_publisher.h"
|
||||
#include "ports/sensor_message_sink.h"
|
||||
#include "sensor/imu_processor.h"
|
||||
#include "sensor/touch_channel.h"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
namespace borla {
|
||||
|
||||
struct SensorDiagnostics {
|
||||
uint32_t touch_events_sent = 0;
|
||||
uint32_t imu_batches_sent = 0;
|
||||
uint32_t gravity_events_sent = 0;
|
||||
uint32_t messages_dropped = 0;
|
||||
uint32_t touch_read_errors = 0;
|
||||
uint32_t imu_read_errors = 0;
|
||||
uint32_t battery_read_errors = 0;
|
||||
};
|
||||
|
||||
class SensorService final : public IBatteryStatusProvider {
|
||||
public:
|
||||
SensorService(
|
||||
CapacitiveTouchInput& touch,
|
||||
Bmi270Sensor& imu,
|
||||
BatteryMonitor& battery,
|
||||
ISensorMessageSink& sink);
|
||||
|
||||
bool Initialize();
|
||||
void SetEventPublisher(IEventPublisher* publisher)
|
||||
{
|
||||
publisher_ = publisher;
|
||||
}
|
||||
void Stop();
|
||||
|
||||
BatteryStatus GetBatteryStatus() const override;
|
||||
SensorDiagnostics GetDiagnostics() const;
|
||||
|
||||
private:
|
||||
static constexpr uint32_t kTouchTaskStackDepth = 3072;
|
||||
static constexpr uint32_t kImuTaskStackDepth = 4096;
|
||||
static constexpr uint32_t kBatteryTaskStackDepth = 3072;
|
||||
static constexpr UBaseType_t kTaskPriority = 4;
|
||||
static constexpr EventBits_t kTouchStoppedBit = BIT0;
|
||||
static constexpr EventBits_t kImuStoppedBit = BIT1;
|
||||
static constexpr EventBits_t kBatteryStoppedBit = BIT2;
|
||||
|
||||
static void TouchTaskEntry(void* context);
|
||||
static void ImuTaskEntry(void* context);
|
||||
static void BatteryTaskEntry(void* context);
|
||||
void RunTouch();
|
||||
void RunImu();
|
||||
void RunBattery();
|
||||
bool SendTouch(const TouchEvent& event);
|
||||
bool SendImu(const ImuBatch& batch);
|
||||
bool SendGravity(const GravityEvent& event);
|
||||
|
||||
CapacitiveTouchInput& touch_;
|
||||
Bmi270Sensor& imu_;
|
||||
BatteryMonitor& battery_;
|
||||
ISensorMessageSink& sink_;
|
||||
IEventPublisher* publisher_ = nullptr;
|
||||
std::array<TouchChannel, kTouchZoneCount> touch_channels_{};
|
||||
ImuProcessor imu_processor_;
|
||||
|
||||
std::atomic<bool> running_{false};
|
||||
bool touch_enabled_ = false;
|
||||
bool imu_enabled_ = false;
|
||||
bool battery_enabled_ = false;
|
||||
EventGroupHandle_t stopped_events_ = nullptr;
|
||||
StaticEventGroup_t stopped_events_control_{};
|
||||
|
||||
TaskHandle_t touch_task_ = nullptr;
|
||||
StaticTask_t touch_task_control_{};
|
||||
std::array<StackType_t, kTouchTaskStackDepth>
|
||||
touch_task_stack_{};
|
||||
TaskHandle_t imu_task_ = nullptr;
|
||||
StaticTask_t imu_task_control_{};
|
||||
std::array<StackType_t, kImuTaskStackDepth>
|
||||
imu_task_stack_{};
|
||||
TaskHandle_t battery_task_ = nullptr;
|
||||
StaticTask_t battery_task_control_{};
|
||||
std::array<StackType_t, kBatteryTaskStackDepth>
|
||||
battery_task_stack_{};
|
||||
|
||||
mutable portMUX_TYPE status_lock_ =
|
||||
portMUX_INITIALIZER_UNLOCKED;
|
||||
BatteryStatus battery_status_{};
|
||||
std::atomic<uint32_t> touch_events_sent_{0};
|
||||
std::atomic<uint32_t> imu_batches_sent_{0};
|
||||
std::atomic<uint32_t> gravity_events_sent_{0};
|
||||
std::atomic<uint32_t> messages_dropped_{0};
|
||||
std::atomic<uint32_t> touch_read_errors_{0};
|
||||
std::atomic<uint32_t> imu_read_errors_{0};
|
||||
std::atomic<uint32_t> battery_read_errors_{0};
|
||||
};
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user