M5Unit-DISTANCE 0.3.1 git rev:f8cfad3
Loading...
Searching...
No Matches
unit_RCWL9620.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_DISTANCE_UNIT_RCWL9620_HPP
11#define M5_UNIT_DISTANCE_UNIT_RCWL9620_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <limits> // NaN
16#include <cmath>
17#include <array>
18
19namespace m5 {
20namespace unit {
21
26namespace rcwl9620 {
27
32struct Data {
33 std::array<uint8_t, 3> raw{};
34
35 static constexpr float MAX_DISTANCE{4500.f};
36 static constexpr float MIN_DISTANCE{20.f};
37
40 inline float distance() const
41 {
42 const float fd{raw_distance() / 1000.f};
43 return std::fmax(std::fmin(fd, MAX_DISTANCE), MIN_DISTANCE);
44 }
47 inline uint32_t raw_distance() const
48 {
49 return (static_cast<uint32_t>(raw[0]) << 16) | (static_cast<uint32_t>(raw[1]) << 8) |
50 static_cast<uint32_t>(raw[2]);
51 }
52};
53
54} // namespace rcwl9620
55
62class UnitRCWL9620 : public Component, public PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data> {
63 M5_UNIT_COMPONENT_HPP_BUILDER(UnitRCWL9620, 0x57);
64
65public:
70 struct config_t {
72 bool start_periodic{true};
74 uint32_t interval_ms{150};
75 };
76
80 explicit UnitRCWL9620(const uint8_t addr = DEFAULT_ADDRESS)
81 : Component(addr), _data{new m5::container::CircularBuffer<rcwl9620::Data>(1)}
82 {
83 auto ccfg = component_config();
84 // At 100 kHz the sensor can drive SDA too late for the next SCL rising edge, and reads fail
85 // intermittently (more often through a PaHub). 400 kHz fails (NACK)
86 ccfg.clock = 80 * 1000U;
87 component_config(ccfg);
88 }
90 virtual ~UnitRCWL9620()
91 {
92 }
93
96 virtual bool begin() override;
99 virtual void update(const bool force = false) override;
100
103
105 inline config_t config() const
106 {
107 return _cfg;
108 }
111 inline void config(const config_t& cfg)
112 {
113 _cfg = cfg;
114 }
116
121 float distance() const
122 {
123 return !empty() ? oldest().distance() : std::numeric_limits<float>::quiet_NaN();
124 }
126
129
137 inline bool startPeriodicMeasurement(const uint32_t interval)
138 {
139 return PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data>::startPeriodicMeasurement(interval);
140 }
146 {
147 return PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data>::stopPeriodicMeasurement();
148 }
150
153
162
164 // Class that abstracts the interaction between classes and adapters
165 // The owning unit is passed on each call instead of being stored, so the unit stays safe to move
166 class Interface {
167 public:
168 virtual ~Interface()
169 {
170 }
171 virtual bool read_measurement(UnitRCWL9620& u, rcwl9620::Data& d, bool& timed_out) = 0;
172 virtual bool request_measurement(UnitRCWL9620& u) = 0;
173 };
175
176protected:
177 bool request_measurement();
178 bool read_measurement(rcwl9620::Data& d, bool& timed_out);
179
180 bool start_periodic_measurement(const uint32_t interval);
181 bool stop_periodic_measurement();
182
183 inline Interface* interface() const
184 {
185 return _interface.get();
186 }
187
188 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitRCWL9620, rcwl9620::Data);
189
190 std::unique_ptr<m5::container::CircularBuffer<rcwl9620::Data>> _data{};
191
192 inline virtual uint32_t minimum_interval() const
193 {
194 // The datasheet gives 100 ms as the maximum ranging time after writing MEASURE_DISTANCE (0x01).
195 // Reading after 100 ms often fails with ESP_ERR_TIMEOUT (263), so 150 ms is used.
196 return 150; // for I2C
197 }
198
199private:
200 std::unique_ptr<Interface> _interface{};
201 config_t _cfg{};
202 uint8_t _request_failures{}; // Consecutive failures to request the next periodic measurement
203};
204
206namespace rcwl9620 {
207namespace command {
208constexpr uint8_t MEASURE_DISTANCE{0x01};
209} // namespace command
210} // namespace rcwl9620
212
213} // namespace unit
214} // namespace m5
215#endif
virtual bool begin() override
Begin communication and optionally start periodic measurement.
Definition unit_RCWL9620.cpp:107
virtual ~UnitRCWL9620()
Destructor.
Definition unit_RCWL9620.hpp:90
bool startPeriodicMeasurement(const uint32_t interval)
Start periodic measurement.
Definition unit_RCWL9620.hpp:137
UnitRCWL9620(const uint8_t addr=DEFAULT_ADDRESS)
Constructor.
Definition unit_RCWL9620.hpp:80
bool measureSingleshot(rcwl9620::Data &data)
Measurement single shot.
Definition unit_RCWL9620.cpp:183
config_t config() const
Gets the configuration.
Definition unit_RCWL9620.hpp:105
float distance() const
Oldest distance (mm)
Definition unit_RCWL9620.hpp:121
virtual void update(const bool force=false) override
Update periodic measurement.
Definition unit_RCWL9620.cpp:154
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_RCWL9620.hpp:145
void config(const config_t &cfg)
Set the configuration.
Definition unit_RCWL9620.hpp:111
Top level namespace of M5Stack.
For RCWL9620.
Unit-related namespace.
Settings for begin.
Definition unit_RCWL9620.hpp:70
uint32_t interval_ms
Interval time if start on begin (ms) (I2C:150-, GPIO:50-)
Definition unit_RCWL9620.hpp:74
bool start_periodic
Start periodic measurement on begin?
Definition unit_RCWL9620.hpp:72
Measurement data group.
Definition unit_RCWL9620.hpp:32
uint32_t raw_distance() const
Get raw distance (um)
Definition unit_RCWL9620.hpp:47
static constexpr float MAX_DISTANCE
Maximum distance (mm)
Definition unit_RCWL9620.hpp:35
std::array< uint8_t, 3 > raw
Raw data (3 bytes, MSB first)
Definition unit_RCWL9620.hpp:33
float distance() const
Get distance (mm)
Definition unit_RCWL9620.hpp:40
static constexpr float MIN_DISTANCE
Minimum distance (mm)
Definition unit_RCWL9620.hpp:36