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

40 lines
857 B
C
Raw 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>
template <int DIVIDER = 128>
2019-08-22 21:33:36 +02:00
class Scale
{
public:
bool measure(uint16_t &measurementOut)
{
2019-08-22 21:33:36 +02:00
if (hx711_.is_ready())
{
2020-06-05 20:55:01 +02:00
long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_;
if (value < 0)
2020-06-07 10:59:16 +02:00
measurementOut = (int16_t)(-value / DIVIDER);
2020-06-05 20:55:01 +02:00
else
measurementOut = 0;
2019-08-22 21:33:36 +02:00
return true;
}
else
return false;
}
void begin(uint32_t pinDOUT, uint32_t pinSCK)
{
2019-08-22 21:33:36 +02:00
hx711_.begin(pinDOUT, pinSCK);
};
void tare(uint32_t numMeasurementsToAverage = 50)
{
2020-06-05 20:55:01 +02:00
offset_ = hx711_.read_average(numMeasurementsToAverage);
2019-08-22 21:33:36 +02:00
}
long &offset() const { return offset_; }
2019-08-22 21:33:36 +02:00
private:
HX711 hx711_;
2020-06-05 20:55:01 +02:00
long offset_ = 0;
2019-08-22 21:33:36 +02:00
};