M5Unit-DISTANCE 0.0.1 git rev:2dff6c0
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{}; // Raw data
34
35 static constexpr float MAX_DISTANCE{4500.f};
36 static constexpr float MIN_DISTANCE{20.f};
37
39 inline float distance() const
40 {
41 float fd = raw_distance() / 1000.f;
42 return std::fmax(std::fmin(fd, MAX_DISTANCE), MIN_DISTANCE);
43 }
44 inline uint32_t raw_distance() const
45 {
46 return ((uint32_t)raw[0] << 16) | ((uint32_t)raw[1] << 8) | (uint32_t)raw[2];
47 }
48};
49
50} // namespace rcwl9620
51
56class UnitRCWL9620 : public Component, public PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data> {
57 M5_UNIT_COMPONENT_HPP_BUILDER(UnitRCWL9620, 0x57);
58
59public:
64 struct config_t {
66 bool start_periodic{true};
68 uint32_t interval_ms{150};
69 };
70
71 explicit UnitRCWL9620(const uint8_t addr = DEFAULT_ADDRESS)
72 : Component(addr), _data{new m5::container::CircularBuffer<rcwl9620::Data>(1)}
73 {
74 auto ccfg = component_config();
75 ccfg.clock = 100 * 1000U;
76 component_config(ccfg);
77 }
78 virtual ~UnitRCWL9620()
79 {
80 }
81
82 virtual bool begin() override;
83 virtual void update(const bool force = false) override;
84
87
89 {
90 return _cfg;
91 }
93 inline void config(const config_t& cfg)
94 {
95 _cfg = cfg;
96 }
98
102 float distance() const
103 {
104 return !empty() ? oldest().distance() : std::numeric_limits<float>::quiet_NaN();
105 }
107
110
116 inline bool startPeriodicMeasurement(const uint32_t interval)
117 {
118 return PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data>::startPeriodicMeasurement(interval);
119 }
125 {
126 return PeriodicMeasurementAdapter<UnitRCWL9620, rcwl9620::Data>::stopPeriodicMeasurement();
127 }
129
132
140
141protected:
142 bool request_measurement();
143 bool read_measurement(rcwl9620::Data& d);
144
145 bool start_periodic_measurement(const uint32_t interval);
146 bool stop_periodic_measurement();
147
148 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitRCWL9620, rcwl9620::Data);
149
150private:
151 std::unique_ptr<m5::container::CircularBuffer<rcwl9620::Data>> _data{};
152 config_t _cfg{};
153};
154
156namespace rcwl9620 {
157namespace command {
158constexpr uint8_t MEASURE_DISTANCE{0x01};
159} // namespace command
160} // namespace rcwl9620
162
163} // namespace unit
164} // namespace m5
165#endif
An ultrasonic distance measuring sensor unit.
Definition unit_RCWL9620.hpp:56
config_t config()
Gets the configration.
Definition unit_RCWL9620.hpp:88
bool measureSingleshot(rcwl9620::Data &d)
Measurement single shot.
Definition unit_RCWL9620.cpp:75
bool startPeriodicMeasurement(const uint32_t interval)
Start periodic measurement.
Definition unit_RCWL9620.hpp:116
float distance() const
Oldest distance (mm)
Definition unit_RCWL9620.hpp:102
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_RCWL9620.hpp:124
void config(const config_t &cfg)
Set the configration.
Definition unit_RCWL9620.hpp:93
Top level namespace of M5stack.
For RCWL9620.
Unit-related namespace.
Settings for begin.
Definition unit_RCWL9620.hpp:64
uint32_t interval_ms
Interval time if start on begin (ms) (150-)
Definition unit_RCWL9620.hpp:68
bool start_periodic
Start periodic measurement on begin?
Definition unit_RCWL9620.hpp:66
Measurement data group.
Definition unit_RCWL9620.hpp:32
float distance() const
Get distance(mm)
Definition unit_RCWL9620.hpp:39