21constexpr uint8_t MAX_DEVICES{ 8 };
27 void add(
const uint8_t (&rom)[8])
29 if (count != MAX_DEVICES)
31 memcpy(roms[count++], rom, 8);
36 void replugAll(uint8_t devices)
44 for (uint8_t
i = 0;
i != count; ++
i)
49 readComplement =
false;
53 void write(uint8_t command)
55 lastCommand = command;
61 for (uint8_t
i = 0;
i != count; ++
i)
63 if (inSearch[
i] && (
bitOf(
i) == readComplement))
68 readComplement = !readComplement;
72 void write_bit(uint8_t value)
74 for (uint8_t
i = 0;
i != count; ++
i)
76 if (
bitOf(
i) != (value != 0))
93 uint8_t lastCommand{ 0 };
97 bool bitOf(uint8_t device)
const
99 return (roms[device][bit / 8] >> (bit % 8)) & 1U;
102 uint8_t roms[MAX_DEVICES][8]{};
103 bool inSearch[MAX_DEVICES]{};
106 bool readComplement{
false };
110uint8_t searchAll(SimulatedBus& bus, uint8_t (&found)[MAX_DEVICES + 1][8])
115 while ((n != MAX_DEVICES + 1) && search.next(rom))
117 memcpy(found[n++], rom, 8);
122bool contains(
const uint8_t (&found)[MAX_DEVICES + 1][8], uint8_t n,
const uint8_t* rom)
124 for (uint8_t
i = 0;
i != n; ++
i)
126 if (0 == memcmp(found[
i], rom, 8))
135constexpr uint8_t SENSOR_A[8]{ 0x28, 0xBE, 0x41, 0x6B, 0x09, 0x00, 0x00, 0xA4 };
136constexpr uint8_t SENSOR_B[8]{ 0x28, 0x1B, 0xD7, 0x6A, 0x09, 0x00, 0x00, 0xB7 };
137constexpr uint8_t SENSOR_C[8]{ 0x28, 0x1B, 0xD7, 0x6A, 0x09, 0x00, 0x00, 0x37 };
138constexpr uint8_t SENSOR_D[8]{ 0x28, 0xFF, 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A };
152 uint8_t found[MAX_DEVICES + 1][8]{};
154 TEST_ASSERT_EQUAL_UINT8(0, searchAll(bus, found));
161 uint8_t found[MAX_DEVICES + 1][8]{};
163 TEST_ASSERT_EQUAL_UINT8(1, searchAll(bus, found));
164 TEST_ASSERT_EQUAL_UINT8_ARRAY(SENSOR_A, found[0], 8);
165 TEST_ASSERT_EQUAL_HEX8(0xF0, bus.lastCommand);
173 uint8_t found[MAX_DEVICES + 1][8]{};
175 TEST_ASSERT_EQUAL_UINT8(2, searchAll(bus, found));
176 TEST_ASSERT_TRUE(contains(found, 2, SENSOR_A));
177 TEST_ASSERT_TRUE(contains(found, 2, SENSOR_B));
185 uint8_t found[MAX_DEVICES + 1][8]{};
187 TEST_ASSERT_EQUAL_UINT8(2, searchAll(bus, found));
188 TEST_ASSERT_TRUE(contains(found, 2, SENSOR_B));
189 TEST_ASSERT_TRUE(contains(found, 2, SENSOR_C));
199 uint8_t found[MAX_DEVICES + 1][8]{};
201 TEST_ASSERT_EQUAL_UINT8(4, searchAll(bus, found));
202 TEST_ASSERT_TRUE(contains(found, 4, SENSOR_A));
203 TEST_ASSERT_TRUE(contains(found, 4, SENSOR_B));
204 TEST_ASSERT_TRUE(contains(found, 4, SENSOR_C));
205 TEST_ASSERT_TRUE(contains(found, 4, SENSOR_D));
211 uint8_t roms[MAX_DEVICES][8]{};
212 for (uint8_t
i = 0;
i != MAX_DEVICES; ++
i)
215 const uint8_t rom[8]{ 0x28, 0x00, 0x00, 0x00, 0x00, 0x00,
static_cast< uint8_t
>(
i * 37),
static_cast< uint8_t
>(
i) };
216 memcpy(roms[
i], rom, 8);
219 uint8_t found[MAX_DEVICES + 1][8]{};
221 TEST_ASSERT_EQUAL_UINT8(MAX_DEVICES, searchAll(bus, found));
222 for (uint8_t
i = 0;
i != MAX_DEVICES; ++
i)
224 TEST_ASSERT_TRUE(contains(found, MAX_DEVICES, roms[
i]));
235 TEST_ASSERT_TRUE(search.next(rom));
236 const uint16_t resets{ bus.resets };
237 TEST_ASSERT_FALSE(search.next(rom));
238 TEST_ASSERT_EQUAL_UINT16(resets, bus.resets);
249 TEST_ASSERT_TRUE(search.next(rom));
251 TEST_ASSERT_FALSE(search.next(rom));
263 TEST_ASSERT_TRUE(search.next(first));
265 TEST_ASSERT_FALSE(search.next(rom));
266 bus.replugAll(bus.plugged);
267 TEST_ASSERT_TRUE(search.next(rom));
268 TEST_ASSERT_EQUAL_UINT8_ARRAY(first, rom, 8);
The successive searches of a 1-Wire bus, one device each.
constexpr uint8_t bitOf(const uint8_t(&map)[N], uint8_t loadNum)
Rank of a load among the loads of its own unit, in ascending map order.
char * line(char *dst, const char *tag, int16_t value, uint8_t index=0)
Writes the line of a signed value.
void test_no_search_after_the_last_device()
void test_search_starts_over_after_a_reset_without_presence()
void test_devices_unplugged_between_two_searches()
void test_four_devices_each_found_once()
void test_eight_devices()
void test_codes_differing_in_their_last_bit()
The ROM search of a 1-Wire bus, on the bit primitives of the bus.