GY-63_MS5611/libraries/HX711/test/unit_test_001.cpp

192 lines
3.9 KiB
C++
Raw Normal View History

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()
{
fprintf(stderr, "HX711_LIB_VERSION: %s\n", (char *) HX711_LIB_VERSION);
2021-01-29 06:31:58 -05:00
}
unittest_teardown()
{
}
2022-11-06 14:24:21 -05:00
unittest(test_constants)
{
assertEqual(0x00, HX711_AVERAGE_MODE);
assertEqual(0x01, HX711_MEDIAN_MODE);
assertEqual(0x02, HX711_MEDAVG_MODE);
assertEqual(0x03, HX711_RUNAVG_MODE);
assertEqual(0x04, HX711_RAW_MODE);
assertEqual(128, HX711_CHANNEL_A_GAIN_128);
assertEqual(64, HX711_CHANNEL_A_GAIN_64);
assertEqual(32, HX711_CHANNEL_B_GAIN_32);
}
2021-01-29 06:31:58 -05:00
unittest(test_constructor)
{
HX711 scale;
scale.begin(dataPin, clockPin);
2022-11-06 14:24:21 -05:00
// pins are default LOW apparently.
assertTrue(scale.is_ready());
// default not read
2021-05-16 13:08:20 -04:00
assertEqual(0, scale.last_read());
2021-01-29 06:31:58 -05:00
}
2021-11-16 11:42:29 -05:00
2021-01-29 06:31:58 -05:00
unittest(test_gain)
{
HX711 scale;
scale.begin(dataPin, clockPin);
2022-11-06 14:24:21 -05:00
// rewrite with constants?
// HX711_CHANNEL_A_GAIN_128
// HX711_CHANNEL_A_GAIN_64
// HX711_CHANNEL_B_GAIN_32
// default
2021-01-29 06:31:58 -05:00
assertEqual(128, scale.get_gain());
2022-11-06 14:24:21 -05:00
assertTrue(scale.set_gain(32));
2021-01-29 06:31:58 -05:00
assertEqual(32, scale.get_gain());
2022-11-06 14:24:21 -05:00
assertTrue(scale.set_gain());
2021-05-16 13:08:20 -04:00
assertEqual(128, scale.get_gain());
2022-11-06 14:24:21 -05:00
assertTrue(scale.set_gain(64));
2021-01-29 06:31:58 -05:00
assertEqual(64, scale.get_gain());
2022-11-06 14:24:21 -05:00
assertTrue(scale.set_gain(128));
assertEqual(128, scale.get_gain());
// failing invalid parameter
assertFalse(scale.set_gain(100));
2021-01-29 06:31:58 -05:00
assertEqual(128, scale.get_gain());
2022-11-06 14:24:21 -05:00
// failing invalid parameter
// 0x40 == 64 so it will fail to fail.
// assertFalse(scale.set_gain(0xFF40));
// assertEqual(128, scale.get_gain());
2021-01-29 06:31:58 -05:00
}
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()
// --------