2023-12-02 12:01:23 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// For Filter
|
|
|
|
#include <cstddef>
|
|
|
|
#include "esp_timer.h"
|
|
|
|
#include "SoftFilters.h"
|
|
|
|
// #define ONE_EURO_FCMIN 1e-5f
|
|
|
|
// #define ONE_EURO_BETA 1e-7f
|
|
|
|
// #define ONE_EURO_DCUTOFF 1e-5f
|
|
|
|
|
|
|
|
// From https://github.com/rpatel3001/BleDistance/blob/master/ble_dist.h
|
|
|
|
#define ONE_EURO_FCMIN 0.0001
|
|
|
|
#define ONE_EURO_BETA 0.05
|
|
|
|
#define ONE_EURO_DCUTOFF 1.0
|
|
|
|
|
|
|
|
#define NO_RSSI (-128)
|
|
|
|
#define DEFAULT_TX (-6)
|
|
|
|
|
|
|
|
// For NimbleDistanceSensor
|
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include "esphome/components/nimble_tracker/nimble_tracker.h"
|
|
|
|
|
|
|
|
namespace esphome
|
|
|
|
{
|
2023-12-03 22:19:57 +03:00
|
|
|
namespace nimble_distance_custom
|
2023-12-02 12:01:23 +03:00
|
|
|
{
|
|
|
|
class Filter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Filter(float fcmin, float beta, float dcutoff);
|
|
|
|
bool filter(float rssi);
|
|
|
|
Reading<Differential<float>> output;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
OneEuroFilter<float, unsigned long> one_euro_;
|
|
|
|
DifferentialFilter<float, unsigned long> diff_filter_;
|
|
|
|
};
|
|
|
|
|
2023-12-03 22:19:57 +03:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
NimBLEAddress address;
|
|
|
|
float distance;
|
|
|
|
} NimbleDistanceCustomResult;
|
|
|
|
|
|
|
|
class NimbleDistanceCustomComponent:
|
2023-12-02 12:01:23 +03:00
|
|
|
public Component,
|
|
|
|
public nimble_tracker::NimbleDeviceListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void setup() override;
|
|
|
|
int get_1m_rssi(nimble_tracker::NimbleTrackerEvent *tracker_event);
|
2023-12-06 21:01:11 +03:00
|
|
|
void set_max_distance(float);
|
2023-12-02 12:01:23 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool update_state(nimble_tracker::NimbleTrackerEvent *tracker_event) override;
|
2023-12-03 22:19:57 +03:00
|
|
|
virtual void on_result(NimbleDistanceCustomResult& result);
|
2023-12-02 12:01:23 +03:00
|
|
|
Filter *filter_;
|
|
|
|
int rssi_ = NO_RSSI, newest_ = NO_RSSI, recent_ = NO_RSSI, oldest_ = NO_RSSI;
|
|
|
|
|
2023-12-02 21:31:39 +03:00
|
|
|
int8_t ref_rssi_ = -75;
|
2023-12-02 12:01:23 +03:00
|
|
|
float absorption_ = 3.5f;
|
|
|
|
float last_reported_position_ = 0;
|
|
|
|
int64_t last_reported_micro_seconds_ = 0;
|
2023-12-06 21:01:11 +03:00
|
|
|
|
|
|
|
float max_distance_ = 16.0;
|
2023-12-02 12:01:23 +03:00
|
|
|
};
|
|
|
|
} // namespace nimble_distance
|
|
|
|
} // namespace esphome
|