swimtracker-firmware/firmware/lib/scale/Scale.h

41 lines
1.0 KiB
C
Raw Permalink Normal View History

2019-08-22 21:33:36 +02:00
#include "HX711.h"
#include "SwimTrackerConfig.h"
2019-08-22 21:33:36 +02:00
#include <cstdint>
class Scale
{
public:
bool measure(uint16_t &measurementOut)
{
2021-08-10 22:47:47 +02:00
long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT);
2023-09-14 16:16:17 +02:00
LOG_TRACE("rv %ld", value);
2021-08-10 22:47:47 +02:00
value -= offset_;
if (value < 0)
measurementOut = (int16_t)(-(value >> valueRightShift_ ));
else
measurementOut = 0;
return true;
2019-08-22 21:33:36 +02:00
}
void begin(uint32_t pinDOUT, uint32_t pinSCK, int valueRightShift)
{
2019-08-22 21:33:36 +02:00
hx711_.begin(pinDOUT, pinSCK);
valueRightShift_ = valueRightShift;
2019-08-22 21:33:36 +02:00
};
void tare(uint32_t numMeasurementsToAverage = 50)
{
2021-08-10 22:47:47 +02:00
auto v1 = hx711_.read_average(3);
2020-06-05 20:55:01 +02:00
offset_ = hx711_.read_average(numMeasurementsToAverage);
2023-09-14 16:16:17 +02:00
LOG_INFO("Init reading %ld, Tare offset %ld", v1, offset_);
2019-08-22 21:33:36 +02:00
}
const long &offset() const { return offset_; }
int valueRightShift() const { return valueRightShift_; }
2019-08-22 21:33:36 +02:00
private:
HX711 hx711_;
2020-06-05 20:55:01 +02:00
long offset_ = 0;
int valueRightShift_;
2019-08-22 21:33:36 +02:00
};