Skip to content

Commit

Permalink
add simple tests for sht3x and refactor the driver
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Nov 10, 2024
1 parent c8463d5 commit f618da5
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 62 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ code_style:
cpplint Src/modules/*/*pp Src/peripheral/*/*pp Src/platform/*/*pp

tests:
mkdir -p ${BUILD_DIR}/tests
cd ${BUILD_DIR}/tests && cmake ../../Src/drivers/as5600/tests && make && ./test_as5600
mkdir -p ${BUILD_DIR}/tests/as5600
mkdir -p ${BUILD_DIR}/tests/sht3x
cd ${BUILD_DIR}/tests/as5600 && cmake ../../../Src/drivers/as5600/tests && make && ./test_as5600
cd ${BUILD_DIR}/tests/sht3x && cmake ../../../Src/drivers/sht3x/tests && make && ./test_sht3x

upload:
LATEST_TARGET=$$(ls -td ${BUILD_DIR}/release/*.bin | head -1) && ./scripts/tools/stm32/flash.sh $$LATEST_TARGET
Expand Down
2 changes: 1 addition & 1 deletion Src/drivers/as5600/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project(as5600)

add_library(as5600
../as5600.cpp
../../Src/platform/ubuntu/i2c.cpp
../../../Src/platform/ubuntu/i2c.cpp
)

target_include_directories(as5600 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
75 changes: 38 additions & 37 deletions Src/drivers/sht3x/sht3x.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,19 @@
#include "peripheral/sht3x/sht3x.hpp"
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
*/

#include <cstdint>

#include "main.h"
#include "sht3x.hpp"
#include "peripheral/i2c/i2c.hpp"

static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb) {
return (uint16_t)((uint16_t)msb << 8u) | lsb;
}

static uint8_t calculate_crc(const uint8_t *data, size_t length) {
uint8_t crc = 0xff;
for (size_t i = 0; i < length; i++) {
crc ^= data[i];
for (size_t j = 0; j < 8; j++) {
if ((crc & 0x80u) != 0) {
crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u);
} else {
crc <<= 1u;
}
}
}
return crc;
}

bool SHT3XPeriphery::sendCommand(const SHT3XHandle &handle, SHT3XCommand command) {
uint8_t command_buffer[2] = {(uint8_t)((uint16_t)command >> 8u),
(uint8_t)((uint16_t)command & 0xffu)};

return HAL::I2C::transmit(handle.device_address << 1u, command_buffer,
sizeof(command_buffer));
}
namespace Driver {

bool SHT3XPeriphery::readTemperatureHumidity(const SHT3XHandle &handle,
float *temperature,
float *humidity) {
sendCommand(handle, SHT3XCommand::SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH);
HAL_Delay(1);
bool SHT3X::read(float *temperature, float *humidity) const {
sendCommand(device_address, SHT3XCommand::SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH);

uint8_t buffer[6];

if (!HAL::I2C::receive(handle.device_address << 1u, buffer,
sizeof(buffer))) {
if (!HAL::I2C::receive(device_address << 1u, buffer, sizeof(buffer))) {
return false;
}

Expand All @@ -59,3 +31,32 @@ bool SHT3XPeriphery::readTemperatureHumidity(const SHT3XHandle &handle,

return true;
}

bool SHT3X::sendCommand(uint8_t device_address, SHT3XCommand command) {
uint8_t command_buffer[2] = {(uint8_t)((uint16_t)command >> 8u),
(uint8_t)((uint16_t)command & 0xffu)};

return HAL::I2C::transmit(device_address << 1u, command_buffer,
sizeof(command_buffer));
}

uint16_t SHT3X::uint8_to_uint16(uint8_t msb, uint8_t lsb) {
return (uint16_t)((uint16_t)msb << 8u) | lsb;
}

uint8_t SHT3X::calculate_crc(const uint8_t *data, size_t length) {
uint8_t crc = 0xff;
for (size_t i = 0; i < length; i++) {
crc ^= data[i];
for (size_t j = 0; j < 8; j++) {
if ((crc & 0x80u) != 0) {
crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u);
} else {
crc <<= 1u;
}
}
}
return crc;
}

} // namespace Driver
52 changes: 30 additions & 22 deletions Src/drivers/sht3x/sht3x.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
*/

#ifndef SRC_DRIVERS_SHT3X_SHT3X_HPP_
#define SRC_DRIVERS_SHT3X_SHT3X_HPP_

#include <cstdint>
#include <cstddef>

#define SHT3X_I2C_DEVICE_ADDRESS_ADDR_PIN_LOW 0x44
#define SHT3X_I2C_DEVICE_ADDRESS_ADDR_PIN_HIGH 0x45
namespace Driver {

enum class SHT3XCommand {
SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH = 0x2c06,
Expand All @@ -18,34 +23,37 @@ enum class SHT3XCommand {
SHT3X_COMMAND_MEASURE_LOWREP_10HZ = 0x272a
};

struct SHT3XHandle {
/**
* The I2C device address.
* @see{PCA9865_I2C_DEVICE_ADDRESS_ADDR_PIN_LOW} and
* @see{SHT3X_I2C_DEVICE_ADDRESS_ADDR_PIN_HIGH}
*/
uint16_t device_address;
};

class SHT3XPeriphery {
public:
class SHT3X {
public:
static constexpr uint8_t DEV_ADDR_PIN_LOW = 0x44;
static constexpr uint8_t DEV_ADDR_PIN_HIGH = 0x45;

SHT3X(uint8_t dev_addr): device_address(dev_addr) {}

/**
* Execute a command defined in SHT3XCommand
* @param handle Handle to the SHT3x device.
* @param command SHT3XCommand
* @brief Takes a single temperature and humidity measurement.
* @param temperature Pointer to the storage location for the sampled temperature.
* @param humidity Pointer to the storage location for the sampled humidity.
* @return True on success, false otherwise.
*/
static bool sendCommand(const SHT3XHandle &handle, SHT3XCommand command);
bool read(float* temperature, float* humidity) const;

private:
/**
* Takes a single temperature and humidity measurement.
* @brief Execute a command defined in SHT3XCommand
* @param handle Handle to the SHT3x device.
* @param temperature Pointer to the storage location for the sampled
* temperature.
* @param humidity Pointer to the storage location for the sampled humidity.
* @param command SHT3XCommand
* @return True on success, false otherwise.
*/
static bool readTemperatureHumidity(const SHT3XHandle &handle, float *temperature,
float *humidity);
static bool sendCommand(uint8_t device_address, SHT3XCommand command);

static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb);
static uint8_t calculate_crc(const uint8_t* data, size_t length);

uint8_t device_address;
};

} // namespace Driver

#endif // SRC_DRIVERS_SHT3X_SHT3X_HPP_
28 changes: 28 additions & 0 deletions Src/drivers/sht3x/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (C) 2024 Dmitry Ponomarev <ponomarevda96@gmail.com>
# Distributed under the terms of the GPL v3 license, available in the file LICENSE.

cmake_minimum_required(VERSION 3.10)
project(sht3x)

add_library(sht3x
../sht3x.cpp
../../../Src/platform/ubuntu/i2c.cpp
)

target_include_directories(sht3x PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

find_package(GTest REQUIRED)
include_directories(
${GTEST_INCLUDE_DIRS}
../../../../Src
)

add_executable(test_sht3x
test_sht3x.cpp
)

target_link_libraries(test_sht3x
sht3x
GTest::GTest
GTest::Main
)
29 changes: 29 additions & 0 deletions Src/drivers/sht3x/tests/test_sht3x.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
*/

#include <gtest/gtest.h>
#include "drivers/sht3x/sht3x.hpp"

TEST(Shx3xTest, HelloWorld) {
EXPECT_EQ(1, 1);
}

TEST(Shx3xTest, init) {
Driver::SHT3X sht3x(Driver::SHT3X::DEV_ADDR_PIN_LOW);
}

TEST(Shx3xTest, read) {
Driver::SHT3X sht3x(Driver::SHT3X::DEV_ADDR_PIN_LOW);

float temperature;
float humidity;
sht3x.read(&temperature, &humidity);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit f618da5

Please # to comment.