Skip to content

Add onewire #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ test_spi_connected1_loopback: TESTS=-DTEST_SPI_CONNECTED1_LOOPBACK
test_spi_connected2_masterpingpong: TESTS=-DTEST_SPI_CONNECTED2_MASTERPINGPONG
test_spi_connected2_slavepingpong: TESTS=-DTEST_SPI_CONNECTED2_SLAVEPINGPONG

## OneWire tests targets
test_onewire_sensor: TESTS=-DTEST_ONEWIRE_SENSOR

# Arduino-cli commands

# For WSL and Windows :
Expand Down
100 changes: 100 additions & 0 deletions src/corelibs/onewire/test_onewire_sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* test_onewire_sensor.cpp
*
* This test verifies if the OneWire sensor (e.g. in this case: DS18B20) is responding and providing valid data.
* It includes tests for device presence, chip description, and data retrieval.
*/

// std includes
#include <OneWire.h>
#include "test_common_includes.h"

// OneWire instance and variables
static OneWire oneWire(5); // Pin 5 is used for the OneWire bus
static uint8_t addr[8];
static uint8_t data[12];

// Test group definition
TEST_GROUP(onewire_sensor);

TEST_SETUP(onewire_sensor) {
// Reset the OneWire bus
oneWire.reset_search();
}

TEST_TEAR_DOWN(onewire_sensor) {
// Cleanup if necessary
}

// Test case: Verify if a device is present on the OneWire bus
TEST(onewire_sensor, test_device_presence) {
bool deviceFound = oneWire.search(addr);

// Print result and pass/fail message
if (deviceFound) {
Serial.println(" - Device Found");
} else {
TEST_FAIL_MESSAGE(" - No device found on the OneWire bus.");
}

// Verify the CRC of the device address
TEST_ASSERT_TRUE_MESSAGE(OneWire::crc8(addr, 7) == addr[7], " - Invalid CRC for device address");
}

// Test case: Verify if a chip description is returned
TEST(onewire_sensor, test_device_description) {
bool deviceFound = oneWire.search(addr);
TEST_ASSERT_TRUE_MESSAGE(deviceFound, " - No device found on the OneWire bus");

// Check if the first byte of the address corresponds to a known chip family
if (addr[0] == 0x10 || addr[0] == 0x28 || addr[0] == 0x22) {
Serial.println(" - Chip description found.");
} else {
TEST_FAIL_MESSAGE(" - No valid chip description found.");
}
}

// Test case: Verify if data was returned (basic functionality)
TEST(onewire_sensor, test_data_returned) {
bool deviceFound = oneWire.search(addr);
TEST_ASSERT_TRUE_MESSAGE(deviceFound, " - No device found on the OneWire bus");

// Start temperature conversion
oneWire.reset();
oneWire.select(addr);
oneWire.write(0x44, 1); // Start conversion with parasite power on

// Wait for conversion to complete
delay(1000);

// Read the scratchpad
oneWire.reset();
oneWire.select(addr);
oneWire.write(0xBE); // Read Scratchpad

for (uint8_t i = 0; i < 9; i++) {
data[i] = oneWire.read();
}

// Verify that data was returned (non-zero values in the scratchpad)
bool dataReturned = false;
for (uint8_t i = 0; i < 9; i++) {
if (data[i] != 0) {
dataReturned = true;
break;
}
}

// Print result and pass/fail message
if (dataReturned) {
Serial.println(" - Data received");
} else {
TEST_FAIL_MESSAGE(" - No valid data returned from the sensor.");
}
}

// Define test runner for the OneWire sensor test group
TEST_GROUP_RUNNER(onewire_sensor) {
RUN_TEST_CASE(onewire_sensor, test_device_presence);
RUN_TEST_CASE(onewire_sensor, test_device_description);
RUN_TEST_CASE(onewire_sensor, test_data_returned);
}
6 changes: 6 additions & 0 deletions src/test_main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ void RunAllTests(void)

#endif

#ifdef TEST_ONEWIRE_SENSOR

RUN_TEST_GROUP(onewire_sensor);

#endif

}


Expand Down