mirror of
https://github.com/m5stack/M5Unit-WEIGHT.git
synced 2026-05-20 11:31:12 -07:00
Fixes Doxygen comments, MiniScales config handling and embedded tests
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
/*!
|
||||
@file M5UnitUnifiedWEIGHT.h
|
||||
@brief Main header for the M5Unit-WEIGHT library
|
||||
*/
|
||||
#ifndef M5_UNIT_UNIFIED_WEIGHT_H
|
||||
#define M5_UNIT_UNIFIED_WEIGHT_H
|
||||
|
||||
@@ -25,7 +25,7 @@ const types::attr_t UnitMiniScales::attr{attribute::AccessI2C};
|
||||
void UnitMiniScales::update(const bool force)
|
||||
{
|
||||
UnitWeightI2C::update(force);
|
||||
if (_cfg.manage_button_status) {
|
||||
if (_cfg_mini.manage_button_status) {
|
||||
_prev_button = _button;
|
||||
readButtonStatus(_button);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ bool UnitMiniScales::readLEDColor(uint32_t& rgb32)
|
||||
|
||||
uint8_t r{}, g{}, b{};
|
||||
if (readLEDColor(r, g, b)) {
|
||||
rgb32 = (r << 16) | (g << 8) | b;
|
||||
rgb32 = (static_cast<uint32_t>(r) << 16) | (static_cast<uint32_t>(g) << 8) | static_cast<uint32_t>(b);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -64,19 +64,13 @@ bool UnitMiniScales::writeLEDColor(const uint8_t r, const uint8_t g, const uint8
|
||||
bool UnitMiniScales::writeLEDColor(const uint16_t rgb16)
|
||||
{
|
||||
// Same as M5GFX colortype rgb565_t
|
||||
union {
|
||||
uint16_t raw{};
|
||||
struct {
|
||||
uint16_t b5 : 5;
|
||||
uint16_t g6 : 6;
|
||||
uint16_t r5 : 5;
|
||||
};
|
||||
} rgb565;
|
||||
const uint8_t r5 = static_cast<uint8_t>((rgb16 >> 11) & 0x1F);
|
||||
const uint8_t g6 = static_cast<uint8_t>((rgb16 >> 5) & 0x3F);
|
||||
const uint8_t b5 = static_cast<uint8_t>(rgb16 & 0x1F);
|
||||
|
||||
rgb565.raw = rgb16;
|
||||
uint8_t r = (rgb565.r5 << 3) + (rgb565.r5 >> 2);
|
||||
uint8_t g = (rgb565.g6 << 2) + (rgb565.g6 >> 4);
|
||||
uint8_t b = (rgb565.b5 << 3) + (rgb565.b5 >> 2);
|
||||
uint8_t r = static_cast<uint8_t>((r5 << 3) + (r5 >> 2));
|
||||
uint8_t g = static_cast<uint8_t>((g6 << 2) + (g6 >> 4));
|
||||
uint8_t b = static_cast<uint8_t>((b5 << 3) + (b5 >> 2));
|
||||
|
||||
return writeLEDColor(r, g, b);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,10 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief Update the cached measurement and button state
|
||||
@param force Force the update even when the normal timing check would skip it
|
||||
*/
|
||||
virtual void update(const bool force = false) override;
|
||||
|
||||
///@name Settings for begin
|
||||
@@ -46,13 +50,13 @@ public:
|
||||
/*! @brief Gets the configuration */
|
||||
inline config_t config()
|
||||
{
|
||||
return this->_cfg;
|
||||
return _cfg_mini;
|
||||
}
|
||||
//! @brief Set the configuration
|
||||
inline void config(const config_t& cfg)
|
||||
{
|
||||
_cfg = cfg;
|
||||
UnitWeightI2C::config((UnitWeightI2C::config_t)(this->_cfg));
|
||||
_cfg_mini = cfg;
|
||||
UnitWeightI2C::config(static_cast<UnitWeightI2C::config_t>(_cfg_mini));
|
||||
}
|
||||
///@}
|
||||
|
||||
@@ -136,7 +140,7 @@ public:
|
||||
|
||||
private:
|
||||
bool _button{}, _prev_button{};
|
||||
config_t _cfg{};
|
||||
config_t _cfg_mini{};
|
||||
};
|
||||
|
||||
namespace miniscales {
|
||||
|
||||
@@ -35,8 +35,13 @@ enum class Mode : uint8_t { Float, Int };
|
||||
struct Data {
|
||||
static_assert(sizeof(float) == 4, "Invalid float size"); // I2C protocol assumes IEEE 754 float (4 bytes)
|
||||
std::array<uint8_t, 4> raw{}; //!< RAW data
|
||||
//!< True if the payload should be interpreted with weight()
|
||||
bool is_float{};
|
||||
|
||||
/*!
|
||||
@brief Get the measured weight as a floating-point value
|
||||
@return Measured weight when `is_float` is true, otherwise `NaN`
|
||||
*/
|
||||
inline float weight() const
|
||||
{
|
||||
if (!is_float) {
|
||||
@@ -46,6 +51,10 @@ struct Data {
|
||||
std::memcpy(&val, raw.data(), raw.size());
|
||||
return val;
|
||||
}
|
||||
/*!
|
||||
@brief Get the measured weight as an integer value multiplied by 100
|
||||
@return Measured weight x100 when `is_float` is false, otherwise `INT32_MIN`
|
||||
*/
|
||||
inline int32_t iweight() const
|
||||
{
|
||||
return !is_float
|
||||
@@ -94,7 +103,15 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief Initialize the unit and apply the current configuration
|
||||
@return True if successful
|
||||
*/
|
||||
virtual bool begin() override;
|
||||
/*!
|
||||
@brief Update the cached periodic measurement data
|
||||
@param force Force the update even when the normal timing check would skip it
|
||||
*/
|
||||
virtual void update(const bool force = false) override;
|
||||
|
||||
///@name Settings for begin
|
||||
@@ -111,12 +128,12 @@ public:
|
||||
}
|
||||
///@}
|
||||
|
||||
///@warning Depends on Mode
|
||||
///@warning Float mode uses `weight()`, Int mode uses `iweight()`
|
||||
///@name Measurement data by periodic
|
||||
///@{
|
||||
/*!
|
||||
@brief Oldest measured weight (float)
|
||||
@warning Depends on Mode
|
||||
@warning Valid only when periodic measurement uses Float mode. Use `iweight()` for Int mode.
|
||||
*/
|
||||
inline float weight() const
|
||||
{
|
||||
@@ -124,7 +141,7 @@ public:
|
||||
}
|
||||
/*!
|
||||
@brief Oldest measured weight (integer)
|
||||
@warning Depends on Mode
|
||||
@warning Valid only when periodic measurement uses Int mode. Use `weight()` for Float mode.
|
||||
*/
|
||||
inline int32_t iweight() const
|
||||
{
|
||||
@@ -160,7 +177,7 @@ public:
|
||||
@brief Measurement single shot
|
||||
@param[out] data Measured data
|
||||
@param mode Measurement mode
|
||||
@warning During periodic detection runs, an error is returned
|
||||
@warning Returns an error while periodic measurement is running
|
||||
|
||||
*/
|
||||
bool measureSingleshot(weighti2c::Data& data, const weighti2c::Mode mode);
|
||||
@@ -169,7 +186,7 @@ public:
|
||||
@param[out] buf string buffer
|
||||
@return True if successful
|
||||
@warning Buffer length must be at least 16 bytes
|
||||
@warning During periodic detection runs, an error is returned
|
||||
@warning Returns an error while periodic measurement is running
|
||||
*/
|
||||
bool measureSingleshot(char* buf);
|
||||
///@}
|
||||
@@ -178,13 +195,13 @@ public:
|
||||
///@{
|
||||
/*!
|
||||
@brief Read the gap value
|
||||
@param[out] gap value
|
||||
@param[out] gap Calibration gap value in device-defined weight units
|
||||
@return True if successful
|
||||
*/
|
||||
bool readGap(float& gap);
|
||||
/*!
|
||||
@brief Write the gap value
|
||||
@param gap value
|
||||
@param gap Calibration gap value in device-defined weight units
|
||||
@param duration Max command duration(ms)
|
||||
@return True if successful
|
||||
*/
|
||||
@@ -246,7 +263,7 @@ public:
|
||||
*/
|
||||
bool readRawADC(int32_t& value);
|
||||
|
||||
///@warning Handling warning
|
||||
///@warning Changing the I2C address can disconnect the unit until the host uses the new address
|
||||
///@name I2C Address
|
||||
///@{
|
||||
/*!
|
||||
|
||||
@@ -30,8 +30,6 @@ void test()
|
||||
|
||||
void setup()
|
||||
{
|
||||
delay(1500);
|
||||
|
||||
M5.begin();
|
||||
|
||||
M5_LOGI("CPP %ld", __cplusplus);
|
||||
|
||||
@@ -91,6 +91,7 @@ TEST_F(TestWeightI2C, Settings)
|
||||
while (cnt--) {
|
||||
float gap{};
|
||||
gap = (static_cast<float>(esp_random()) / UINT32_MAX) * 200000.f - 100000.f;
|
||||
SCOPED_TRACE(testing::Message() << "gap=" << gap);
|
||||
EXPECT_TRUE(unit->writeGap(gap));
|
||||
float gap2{};
|
||||
EXPECT_TRUE(unit->readGap(gap2));
|
||||
@@ -110,6 +111,7 @@ TEST_F(TestWeightI2C, Settings)
|
||||
while (cnt--) {
|
||||
bool lp{}, tmp{};
|
||||
lp = (bool)(esp_random() & 1);
|
||||
SCOPED_TRACE(testing::Message() << "lp=" << lp);
|
||||
EXPECT_TRUE(unit->enableLPFilter(lp));
|
||||
EXPECT_TRUE(unit->isEnabledLPFilter(tmp));
|
||||
// M5_LOGI("%u/%u", lp, tmp);
|
||||
@@ -120,6 +122,7 @@ TEST_F(TestWeightI2C, Settings)
|
||||
while (cnt--) {
|
||||
uint8_t level{}, tmp{}, prev{};
|
||||
level = esp_random() & 0x7F; // 0-127
|
||||
SCOPED_TRACE(testing::Message() << "avg_filter_level=" << static_cast<unsigned>(level));
|
||||
// M5_LOGW("lv:%u", level);
|
||||
|
||||
EXPECT_TRUE(unit->readAvgFilterLevel(prev));
|
||||
@@ -141,6 +144,7 @@ TEST_F(TestWeightI2C, Settings)
|
||||
while (cnt--) {
|
||||
uint8_t alpha{}, tmp{}, prev{};
|
||||
alpha = esp_random() & 0x7F;
|
||||
SCOPED_TRACE(testing::Message() << "ema_filter_alpha=" << static_cast<unsigned>(alpha));
|
||||
// M5_LOGW("alpha:%u", alpha);
|
||||
|
||||
EXPECT_TRUE(unit->readEmaFilterAlpha(prev));
|
||||
|
||||
Reference in New Issue
Block a user