2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: unit_test_001.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// DATE: 2020-12-28
|
|
|
|
// PURPOSE: unit tests for the HX711 library (24 bit ADC for loadcells)
|
|
|
|
// https://github.com/RobTillaart/HX711
|
|
|
|
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
|
|
|
|
//
|
|
|
|
|
|
|
|
// supported assertions
|
|
|
|
// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42
|
|
|
|
// ----------------------------
|
|
|
|
// assertEqual(expected, actual)
|
|
|
|
// assertNotEqual(expected, actual)
|
|
|
|
// assertLess(expected, actual)
|
|
|
|
// assertMore(expected, actual)
|
|
|
|
// assertLessOrEqual(expected, actual)
|
|
|
|
// assertMoreOrEqual(expected, actual)
|
|
|
|
// assertTrue(actual)
|
|
|
|
// assertFalse(actual)
|
|
|
|
// assertNull(actual)
|
|
|
|
// assertNotNull(actual)
|
|
|
|
|
|
|
|
#include <ArduinoUnitTests.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "HX711.h"
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t dataPin = 6;
|
|
|
|
uint8_t clockPin = 7;
|
|
|
|
|
|
|
|
|
|
|
|
unittest_setup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest_teardown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_constructor)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "VERSION: %s\n", HX711_LIB_VERSION);
|
|
|
|
|
|
|
|
HX711 scale;
|
|
|
|
scale.begin(dataPin, clockPin);
|
|
|
|
|
2021-05-16 13:08:20 -04:00
|
|
|
assertTrue(scale.is_ready()); // pins are default LOW apparently.
|
|
|
|
// default not read
|
|
|
|
assertEqual(0, scale.last_read());
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unittest(test_gain)
|
|
|
|
{
|
|
|
|
HX711 scale;
|
|
|
|
scale.begin(dataPin, clockPin);
|
|
|
|
|
2021-05-16 13:08:20 -04:00
|
|
|
// default
|
2021-01-29 06:31:58 -05:00
|
|
|
assertEqual(128, scale.get_gain());
|
|
|
|
|
|
|
|
scale.set_gain(32);
|
|
|
|
assertEqual(32, scale.get_gain());
|
|
|
|
|
2021-05-16 13:08:20 -04:00
|
|
|
scale.set_gain();
|
|
|
|
assertEqual(128, scale.get_gain());
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
scale.set_gain(64);
|
|
|
|
assertEqual(64, scale.get_gain());
|
|
|
|
|
|
|
|
scale.set_gain(128);
|
|
|
|
assertEqual(128, scale.get_gain());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_scale)
|
|
|
|
{
|
|
|
|
HX711 scale;
|
|
|
|
scale.begin(dataPin, clockPin);
|
|
|
|
|
2021-05-16 13:08:20 -04:00
|
|
|
// default
|
2021-01-29 06:31:58 -05:00
|
|
|
assertEqualFloat(1.0, scale.get_scale(), 0.001);
|
|
|
|
|
|
|
|
for (float sc = 0.1; sc < 2.0; sc += 0.1)
|
|
|
|
{
|
|
|
|
scale.set_scale(sc);
|
|
|
|
assertEqualFloat(sc, scale.get_scale(), 0.001);
|
|
|
|
}
|
|
|
|
scale.set_scale();
|
|
|
|
assertEqualFloat(1.0, scale.get_scale(), 0.001);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_offset)
|
|
|
|
{
|
|
|
|
HX711 scale;
|
|
|
|
scale.begin(dataPin, clockPin);
|
|
|
|
|
2021-05-16 13:08:20 -04:00
|
|
|
// default offset
|
2021-01-29 06:31:58 -05:00
|
|
|
assertEqual(0, scale.get_offset());
|
|
|
|
|
|
|
|
for (long of = -100; of < 100; of += 13)
|
|
|
|
{
|
|
|
|
scale.set_offset(of);
|
|
|
|
assertEqual(of, scale.get_offset() );
|
|
|
|
}
|
|
|
|
scale.set_offset();
|
|
|
|
assertEqual(0, scale.get_offset());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_tare)
|
|
|
|
{
|
|
|
|
HX711 scale;
|
|
|
|
scale.begin(dataPin, clockPin);
|
|
|
|
|
2021-05-16 13:08:20 -04:00
|
|
|
// default tare
|
2021-01-29 06:31:58 -05:00
|
|
|
assertEqual(0, scale.get_tare());
|
|
|
|
assertFalse(scale.tare_set());
|
|
|
|
|
|
|
|
scale.set_offset(123);
|
|
|
|
assertTrue(scale.tare_set());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_unit_price)
|
|
|
|
{
|
|
|
|
HX711 scale;
|
|
|
|
scale.begin(dataPin, clockPin);
|
|
|
|
|
|
|
|
assertEqual(0, scale.get_unit_price());
|
|
|
|
|
2021-05-16 13:08:20 -04:00
|
|
|
for (float up = 0.10; up < 10; up += 1.23)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-05-16 13:08:20 -04:00
|
|
|
scale.set_unit_price(up);
|
|
|
|
assertEqualFloat(up, scale.get_unit_price(), 0.001);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2021-05-16 13:08:20 -04:00
|
|
|
scale.set_unit_price();
|
|
|
|
assertEqualFloat(1.0, scale.get_unit_price(), 0.001);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_operational_mode)
|
|
|
|
{
|
|
|
|
HX711 scale;
|
|
|
|
scale.begin(dataPin, clockPin);
|
|
|
|
|
|
|
|
assertEqual(0x00, scale.get_mode());
|
|
|
|
scale.set_medavg_mode();
|
|
|
|
assertEqual(0x02, scale.get_mode());
|
|
|
|
scale.set_median_mode();
|
|
|
|
assertEqual(0x01, scale.get_mode());
|
|
|
|
scale.set_average_mode();
|
|
|
|
assertEqual(0x00, scale.get_mode());
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest_main()
|
|
|
|
|
|
|
|
// --------
|