GY-63_MS5611/libraries/HX711/HX711.h

172 lines
4.5 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: HX711.h
// AUTHOR: Rob Tillaart
2023-08-26 06:00:53 -04:00
// VERSION: 0.3.8
2022-06-02 08:32:49 -04:00
// PURPOSE: Library for load cells for Arduino
2021-01-29 06:31:58 -05:00
// URL: https://github.com/RobTillaart/HX711
//
2022-11-06 14:24:21 -05:00
// NOTES
// Superset of interface of HX711 class of Bogde
// float instead of long as float has 23 bits mantissa.
2021-01-29 06:31:58 -05:00
#include "Arduino.h"
2023-08-26 06:00:53 -04:00
#define HX711_LIB_VERSION (F("0.3.8"))
2021-05-16 13:08:20 -04:00
const uint8_t HX711_AVERAGE_MODE = 0x00;
// in median mode only between 3 and 15 samples are allowed.
2022-06-02 08:32:49 -04:00
const uint8_t HX711_MEDIAN_MODE = 0x01;
// medavg = average of the middle "half" of sorted elements
// in medavg mode only between 3 and 15 samples are allowed.
2022-06-02 08:32:49 -04:00
const uint8_t HX711_MEDAVG_MODE = 0x02;
// runavg = running average
2022-06-02 08:32:49 -04:00
const uint8_t HX711_RUNAVG_MODE = 0x03;
// causes read() to be called only once!
const uint8_t HX711_RAW_MODE = 0x04;
2021-05-16 13:08:20 -04:00
2021-01-29 06:31:58 -05:00
2022-11-06 14:24:21 -05:00
// supported values for set_gain()
2023-08-26 06:00:53 -04:00
const uint8_t HX711_CHANNEL_A_GAIN_128 = 128; // default
2022-11-06 14:24:21 -05:00
const uint8_t HX711_CHANNEL_A_GAIN_64 = 64;
const uint8_t HX711_CHANNEL_B_GAIN_32 = 32;
2021-01-29 06:31:58 -05:00
class HX711
{
public:
HX711();
~HX711();
// fixed gain 128 for now
2021-01-29 06:31:58 -05:00
void begin(uint8_t dataPin, uint8_t clockPin);
void reset();
// checks if load cell is ready to read.
2021-01-29 06:31:58 -05:00
bool is_ready();
// wait until ready,
// check every ms
2021-01-29 06:31:58 -05:00
void wait_ready(uint32_t ms = 0);
// max # retries
2021-01-29 06:31:58 -05:00
bool wait_ready_retry(uint8_t retries = 3, uint32_t ms = 0);
// max timeout
2021-01-29 06:31:58 -05:00
bool wait_ready_timeout(uint32_t timeout = 1000, uint32_t ms = 0);
// raw read
2021-01-29 06:31:58 -05:00
float read();
2022-03-16 07:11:42 -04:00
// get average of multiple raw reads
// times = 1 or more
2021-01-29 06:31:58 -05:00
float read_average(uint8_t times = 10);
2021-05-16 13:08:20 -04:00
// get median of multiple raw reads
// times = 3..15 - odd numbers preferred
float read_median(uint8_t times = 7);
2021-05-16 13:08:20 -04:00
// get average of "middle half" of multiple raw reads.
// times = 3..15 - odd numbers preferred
float read_medavg(uint8_t times = 7);
2021-05-16 13:08:20 -04:00
// get running average over times measurements.
// the weight alpha can be set to any value between 0 and 1
// times = 1 or more.
float read_runavg(uint8_t times = 7, float alpha = 0.5);
2021-05-26 09:54:34 -04:00
2021-05-16 13:08:20 -04:00
// get set mode for get_value() and indirect get_units().
// in median and medavg mode only 3..15 samples are allowed.
2023-06-28 06:53:28 -04:00
void set_raw_mode();
void set_average_mode();
void set_median_mode();
void set_medavg_mode();
// set_run_avg will use a default alpha of 0.5.
2023-06-28 06:53:28 -04:00
void set_runavg_mode();
uint8_t get_mode();
2022-06-02 08:32:49 -04:00
// corrected for offset.
// in HX711_RAW_MODE the parameter times will be ignored.
2021-05-16 13:08:20 -04:00
float get_value(uint8_t times = 1);
2023-06-28 06:53:28 -04:00
// converted to proper units, corrected for scale.
2022-06-02 08:32:49 -04:00
// in HX711_RAW_MODE the parameter times will be ignored.
2021-01-29 06:31:58 -05:00
float get_units(uint8_t times = 1);
2021-05-16 13:08:20 -04:00
// TARE
// call tare to calibrate zero
2023-06-28 06:53:28 -04:00
void tare(uint8_t times = 10);
float get_tare();
bool tare_set();
2021-01-29 06:31:58 -05:00
2021-05-16 13:08:20 -04:00
// CORE "CONSTANTS" -> read datasheet
2022-11-06 14:24:21 -05:00
// CHANNEL GAIN notes
// -------------------------------------
// A 128 default, tested
// A 64
// B 32
// returns true ==> parameter gain is valid
// returns false ==> parameter gain is invalid ==> no change.
// note that changing gain/channel takes up to 400 ms (page 3)
2023-06-28 06:53:28 -04:00
// if forced == true, the gain will be forced set
2022-11-06 14:24:21 -05:00
// even it is already the right value
bool set_gain(uint8_t gain = HX711_CHANNEL_A_GAIN_128, bool forced = false);
uint8_t get_gain();
2023-06-28 06:53:28 -04:00
// CALIBRATION & SETUP
// SCALE > 0
2023-06-28 06:53:28 -04:00
// returns false if scale == 0;
2023-08-26 06:00:53 -04:00
bool set_scale(float scale = 1.0);
2023-06-28 06:53:28 -04:00
float get_scale();
2022-11-06 14:24:21 -05:00
// OFFSET > 0
2023-06-28 06:53:28 -04:00
void set_offset(long offset = 0);
long get_offset();
2021-01-29 06:31:58 -05:00
// clear the scale
// call tare() to set the zero offset
// put a known weight on the scale
// call calibrate_scale(weight)
// scale is calculated.
2021-05-16 13:08:20 -04:00
void calibrate_scale(uint16_t weight, uint8_t times = 10);
2021-01-29 06:31:58 -05:00
// POWER MANAGEMENT
2021-01-29 06:31:58 -05:00
void power_down();
void power_up();
2021-05-16 13:08:20 -04:00
// TIME OF LAST READ
2023-06-28 06:53:28 -04:00
uint32_t last_read();
2021-01-29 06:31:58 -05:00
2021-05-16 13:08:20 -04:00
// PRICING (idem calories?)
2021-01-29 06:31:58 -05:00
float get_price(uint8_t times = 1) { return get_units(times) * _price; };
2021-05-16 13:08:20 -04:00
void set_unit_price(float price = 1.0) { _price = price; };
float get_unit_price() { return _price; };
2021-01-29 06:31:58 -05:00
private:
uint8_t _dataPin;
uint8_t _clockPin;
uint8_t _gain = 128; // default channel A
long _offset = 0;
float _scale = 1;
uint32_t _lastRead = 0;
float _price = 0;
2021-05-16 13:08:20 -04:00
uint8_t _mode = 0;
void _insertSort(float * array, uint8_t size);
2021-11-16 11:42:29 -05:00
uint8_t _shiftIn();
2021-01-29 06:31:58 -05:00
};
2022-11-06 14:24:21 -05:00
// -- END OF FILE --