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

46 lines
1.1 KiB
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)
{
//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 {
// long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_;
//
// Serial.printf("Measurement failed %ld\n", value);
// return false;
//}
2019-08-22 21:33:36 +02:00
}
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)
{
hx711_.read_average(3);
2020-06-05 20:55:01 +02:00
offset_ = hx711_.read_average(numMeasurementsToAverage);
Serial.printf("Tare offset %ld\n", offset_);
2019-08-22 21:33:36 +02:00
}
const 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
};