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

36 lines
787 B
C
Raw Normal View History

2019-08-22 21:33:36 +02:00
#include "HX711.h"
2020-06-05 20:55:01 +02:00
#include "ConfigHardware.h"
2019-08-22 21:33:36 +02:00
#include <cstdint>
template<int DIVIDER=128>
class Scale
{
public:
bool measure(uint16_t & measurementOut) {
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)
measurementOut = (int16_t)(value / DIVIDER);
else
measurementOut = 0;
2019-08-22 21:33:36 +02:00
return true;
}
else
return false;
}
void begin(uint32_t pinDOUT, uint32_t pinSCK) {
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
}
private:
HX711 hx711_;
2020-06-05 20:55:01 +02:00
long offset_ = 0;
2019-08-22 21:33:36 +02:00
};