24 Pad(uint8_t lsb, uint8_t msb)
25 : buf{ lsb, msb, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10, 0x00 }
31 static Pad x16(int16_t raw)
33 const auto u{
static_cast< uint16_t
>(raw) };
34 return Pad{
static_cast< uint8_t
>(u & 0xFF),
static_cast< uint8_t
>(u >> 8) };
38int16_t decodeX16(int16_t raw)
52 const uint8_t rom[]{ 0x02, 0x1C, 0xB8, 0x01, 0x00, 0x00, 0x00 };
59 const uint8_t pad[]{ 0x50, 0x05, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10 };
65 const uint8_t pad25[]{ 0x91, 0x01, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10 };
67 const uint8_t padNeg[]{ 0x5E, 0xFF, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10 };
73 const uint8_t none[]{ 0xAA };
79 const Pad good{ 0x91, 0x01 };
80 for (uint8_t
byte = 0;
byte != 9; ++byte)
82 for (uint8_t bit = 0; bit != 8; ++bit)
85 bad.buf[byte] =
static_cast< uint8_t
>(bad.buf[byte] ^ (1U << bit));
98 Pad pad{ 0x91, 0x01 };
99 pad.buf[8] =
static_cast< uint8_t
>(pad.buf[8] + 1);
108 TEST_ASSERT_EQUAL_INT16(0, decodeX16(0));
114 TEST_ASSERT_EQUAL_INT16(1000, decodeX16(160));
115 TEST_ASSERT_EQUAL_INT16(5000, decodeX16(800));
121 TEST_ASSERT_EQUAL_INT16(-100, decodeX16(-16));
122 TEST_ASSERT_EQUAL_INT16(-2500, decodeX16(-400));
123 TEST_ASSERT_EQUAL_HEX16(0xFE70,
static_cast< uint16_t
>(-400));
129 TEST_ASSERT_EQUAL_INT16(6, decodeX16(1));
130 TEST_ASSERT_EQUAL_INT16(12, decodeX16(2));
131 TEST_ASSERT_EQUAL_INT16(18, decodeX16(3));
132 TEST_ASSERT_EQUAL_INT16(25, decodeX16(4));
133 TEST_ASSERT_EQUAL_INT16(50, decodeX16(8));
134 TEST_ASSERT_EQUAL_INT16(93, decodeX16(15));
135 TEST_ASSERT_EQUAL_INT16(-7, decodeX16(-1));
136 TEST_ASSERT_EQUAL_INT16(-13, decodeX16(-2));
137 TEST_ASSERT_EQUAL_INT16(-50, decodeX16(-8));
143 for (int16_t raw = -879; raw != 2000; ++raw)
145 const int32_t x10000{
static_cast< int32_t
>(raw) * 625 };
146 int32_t expected{ x10000 / 100 };
147 if (x10000 % 100 < 0)
151 TEST_ASSERT_EQUAL_INT16(expected, decodeX16(raw));
159 TEST_ASSERT_EQUAL_INT16(12493, decodeX16(1999));
160 TEST_ASSERT_EQUAL_INT16(-5494, decodeX16(-879));
168 TEST_ASSERT_EQUAL_INT16(12500, decodeX16(2000));
169 TEST_ASSERT_EQUAL_INT16(-5500, decodeX16(-880));
252 TEST_ASSERT_EQUAL_INT16(8450, filter(8450));
254 TEST_ASSERT_EQUAL_INT16(8500, filter(8500));
255 TEST_ASSERT_EQUAL_INT16(8500, filter(8500));
262 TEST_ASSERT_EQUAL_INT16(2000, filter(2000));
265 TEST_ASSERT_EQUAL_INT16(2010, filter(2010));
271 TEST_ASSERT_EQUAL_INT16(2000, filter(2000));
276 TEST_ASSERT_EQUAL_INT16(8200, filter(8200));
277 TEST_ASSERT_EQUAL_INT16(8500, filter(8500));
The checks of a DS18B20 reading: CRC, conversion to hundredths of °C, range, power-on value.
constexpr int16_t TEMP_RANGE_LOW
constexpr int16_t DEVICE_DISCONNECTED_RAW
constexpr int16_t OUTOFRANGE_TEMPERATURE
constexpr bool isValidTemperature(int16_t temperature_x100)
Whether a temperature reading is valid, and so worth sending.
constexpr int16_t TEMP_RANGE_HIGH
constexpr uint8_t crc8(const uint8_t *data, uint8_t len)
The Dallas/Maxim CRC-8 (x^8 + x^5 + x^4 + 1), as OneWire::crc8.
constexpr int16_t skipPowerOnValue(int16_t reading, int16_t previous)
The reading to keep, the power-on value skipped.
constexpr int16_t decode(const ScratchPad &buf)
The reading of a scratchpad, its CRC checked.
constexpr int16_t POWER_ON_VALUE
constexpr int16_t toHundredths(uint8_t lsb, uint8_t msb)
The temperature of the register, in hundredths of °C.
uint8_t[9] ScratchPad
The scratchpad: temperature LSB and MSB, TH, TL, configuration, 3 reserved bytes, CRC.
void test_a_steady_85_after_an_invalid_reading_is_kept()
void test_crc_of_nothing_is_zero()
void test_invalid_readings_are_not_sent()
void test_85_after_a_small_jump_is_kept()
void test_a_skipped_or_out_of_range_value_is_no_reference()
void test_other_readings_are_kept_whatever_the_jump()
void test_a_skipped_power_on_value_is_not_sent()
void test_crc_catches_any_single_bit_error()
void test_good_crc_gives_the_temperature()
void test_85_after_a_big_jump_is_skipped()
void test_all_zeros_passes_the_crc()
void test_just_inside_the_range()
void test_a_first_reading_of_85_is_skipped()
void test_crc_of_known_scratchpads()
void test_beyond_the_range()
void test_85_after_an_invalid_reading_is_skipped()
void test_power_on_scratchpad_reads_85()
void test_85_after_an_invalid_reading_far_from_the_last_valid_one_is_skipped()
void test_crc_of_the_power_on_scratchpad()
void test_valid_readings_are_sent()
void test_crc_of_a_known_rom_code()
void test_the_bounds_of_the_sensor_are_in_range()
void test_the_invalid_values_cannot_be_temperatures()
void test_every_register_value_of_the_range()
void test_bad_crc_is_a_disconnected_probe()
void test_all_ones_is_a_disconnected_probe()
void test_fractions_of_a_degree()
The readings of one sensor, the power-on value skipped.